Skip to content

Commit

Permalink
Merge branch 'polymer-develop' into oblackmon3/rebase-polymer-develop…
Browse files Browse the repository at this point in the history
…-to-match-v0.0.25
  • Loading branch information
OBlackmon3 authored Oct 22, 2024
2 parents 0b1555e + 76d16ed commit 73787a0
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ jobs:
uses: securego/gosec@master
with:
args: -exclude-dir=test -tests=false ./...
- name: Generate coverage report
run: make coverage
- name: Upload coverage reports to Codecov
uses: codecov/[email protected]
with:
args: -exclude-dir=test -tests=false ./...
3 changes: 2 additions & 1 deletion common/json_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -473,7 +474,7 @@ func (r *JsonRpcRequest) UnmarshalJSON(data []byte) error {
return nil
}

func (r *JsonRpcRequest) MarshalZerologObject(e *zerolog.Event) {
func (r *JsonRpcResponse) MarshalZerologObject(e *zerolog.Event) {
if r == nil {
return
}
Expand Down
180 changes: 179 additions & 1 deletion erpc/http_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,6 @@ func TestHttpServer_SingleUpstream(t *testing.T) {
statusCode, body := sendRequest(`{"jsonrpc":"2.0","method":"eth_getBlockNumber","params":[],"id":1}`, nil, nil)

assert.Equal(t, http.StatusGatewayTimeout, statusCode)

var errorResponse map[string]interface{}
err := sonic.Unmarshal([]byte(body), &errorResponse)
require.NoError(t, err)
Expand Down Expand Up @@ -843,3 +842,182 @@ func createServerTestFixtures(cfg *common.Config, t *testing.T) (

return sendRequest, baseURL
}

func createServerTestFixtures(cfg *common.Config, t *testing.T) (
func(body string, headers map[string]string, queryParams map[string]string) (int, string),
string,
) {
gock.EnableNetworking()
gock.NetworkingFilter(func(req *http.Request) bool {
shouldMakeRealCall := strings.Split(req.URL.Host, ":")[0] == "localhost"
return shouldMakeRealCall
})
defer gock.Off()

logger := zerolog.New(zerolog.NewConsoleWriter())
ctx := context.Background()
// ctx, cancel := context.WithCancel(context.Background())
// defer cancel()

erpcInstance, err := NewERPC(ctx, &logger, nil, cfg)
require.NoError(t, err)

httpServer := NewHttpServer(ctx, &logger, cfg.Server, erpcInstance)

listener, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
port := listener.Addr().(*net.TCPAddr).Port

go func() {
err := httpServer.server.Serve(listener)
if err != nil && err != http.ErrServerClosed {
t.Errorf("Server error: %v", err)
}
}()
// defer httpServer.server.Shutdown()

time.Sleep(1000 * time.Millisecond)

baseURL := fmt.Sprintf("http://localhost:%d", port)

sendRequest := func(body string, headers map[string]string, queryParams map[string]string) (int, string) {
req, err := http.NewRequest("POST", baseURL+"/test_project/evm/1", strings.NewReader(body))
require.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
for k, v := range headers {
req.Header.Set(k, v)
}
q := req.URL.Query()
for k, v := range queryParams {
q.Add(k, v)
}
req.URL.RawQuery = q.Encode()

client := &http.Client{
Timeout: 10 * time.Second,
}
resp, err := client.Do(req)
if err != nil {
return 0, err.Error()
}
defer resp.Body.Close()

respBody, err := io.ReadAll(resp.Body)
if err != nil {
return resp.StatusCode, err.Error()
}
return resp.StatusCode, string(respBody)
}

return sendRequest, baseURL
}

func TestHttpServer_MultipleUpstreams(t *testing.T) {
cfg := &common.Config{
Server: &common.ServerConfig{
MaxTimeout: "5s",
},
Projects: []*common.ProjectConfig{
{
Id: "test_project",
Networks: []*common.NetworkConfig{
{
Architecture: common.ArchitectureEvm,
Evm: &common.EvmNetworkConfig{
ChainId: 1,
},
},
},
Upstreams: []*common.UpstreamConfig{
{
Id: "rpc1",
Type: common.UpstreamTypeEvm,
Endpoint: "http://rpc1.localhost",
Evm: &common.EvmUpstreamConfig{
ChainId: 1,
},
VendorName: "llama",
},
{
Id: "rpc2",
Type: common.UpstreamTypeEvm,
Endpoint: "http://rpc2.localhost",
Evm: &common.EvmUpstreamConfig{
ChainId: 1,
},
VendorName: "",
},
},
},
},
RateLimiters: &common.RateLimiterConfig{},
}

sendRequest, _ := createServerTestFixtures(cfg, t)

t.Run("UpstreamNotAllowedByDirectiveViaHeaders", func(t *testing.T) {
gock.New("http://rpc1.localhost").
Post("/").
Reply(200).
JSON(map[string]interface{}{
"jsonrpc": "2.0",
"id": 111,
"result": "0x1111111",
})
gock.New("http://rpc2.localhost").
Post("/").
Reply(200).
JSON(map[string]interface{}{
"jsonrpc": "2.0",
"id": 222,
"result": "0x2222222",
})

statusCode2, body2 := sendRequest(`{"jsonrpc":"2.0","method":"eth_getBlockNumber","params":[],"id":1}`, map[string]string{
"X-ERPC-Use-Upstream": "rpc2",
}, nil)

assert.Equal(t, http.StatusOK, statusCode2)
assert.Contains(t, body2, "0x2222222")

statusCode1, body1 := sendRequest(`{"jsonrpc":"2.0","method":"eth_getBlockNumber","params":[],"id":1}`, nil, nil)

assert.Equal(t, http.StatusOK, statusCode1)
assert.Contains(t, body1, "0x1111111")

assert.True(t, gock.IsDone(), "All mocks should have been called")
})

t.Run("UpstreamNotAllowedByDirectiveViaQueryParams", func(t *testing.T) {
gock.New("http://rpc1.localhost").
Post("/").
Reply(200).
JSON(map[string]interface{}{
"jsonrpc": "2.0",
"id": 111,
"result": "0x1111111",
})
gock.New("http://rpc2.localhost").
Post("/").
Reply(200).
JSON(map[string]interface{}{
"jsonrpc": "2.0",
"id": 222,
"result": "0x2222222",
})

statusCode2, body2 := sendRequest(`{"jsonrpc":"2.0","method":"eth_getBlockNumber","params":[],"id":1}`, nil, map[string]string{
"use-upstream": "rpc2",
})

assert.Equal(t, http.StatusOK, statusCode2)
assert.Contains(t, body2, "0x2222222")

statusCode1, body1 := sendRequest(`{"jsonrpc":"2.0","method":"eth_getBlockNumber","params":[],"id":1}`, nil, nil)

assert.Equal(t, http.StatusOK, statusCode1)
assert.Contains(t, body1, "0x1111111")

assert.True(t, gock.IsDone(), "All mocks should have been called")
})
}

0 comments on commit 73787a0

Please sign in to comment.