-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #841 from forta-network/caner/forta-1483-add-json-…
…rpc-method-to-node-metrics Add JSON-RPC method to metrics
- Loading branch information
Showing
4 changed files
with
81 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package json_rpc | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
type jsonRpcReq struct { | ||
Method string `json:"method"` | ||
} | ||
|
||
func decodeAndReplaceBody(req *http.Request) (*jsonRpcReq, error) { | ||
b, _ := io.ReadAll(req.Body) | ||
req.Body.Close() | ||
req.Body = io.NopCloser(bytes.NewBuffer(b)) | ||
var decodedBody jsonRpcReq | ||
if err := json.Unmarshal(b, &decodedBody); err != nil { | ||
return nil, fmt.Errorf("failed to decode json-rpc request body") | ||
} | ||
return &decodedBody, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package json_rpc | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDecodeAndReplaceBody(t *testing.T) { | ||
r := require.New(t) | ||
|
||
bodyStr := `{"method":"eth_call"}` | ||
req, err := http.NewRequest("GET", "/", bytes.NewBufferString(bodyStr)) | ||
r.NoError(err) | ||
|
||
// decodes successfully | ||
decodedBody, err := decodeAndReplaceBody(req) | ||
r.NoError(err) | ||
r.Equal("eth_call", decodedBody.Method) | ||
|
||
// still can read body because it was replaced | ||
b, err := io.ReadAll(req.Body) | ||
r.Equal(bodyStr, string(b)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters