diff --git a/erpc/evm_json_rpc_cache_test.go b/erpc/evm_json_rpc_cache_test.go index 6e8f9c66..fb1d45af 100644 --- a/erpc/evm_json_rpc_cache_test.go +++ b/erpc/evm_json_rpc_cache_test.go @@ -71,6 +71,7 @@ func TestEvmJsonRpcCache_Set(t *testing.T) { assert.NoError(t, err) mockConnector.AssertNotCalled(t, "Set") + mockConnector.On("HasTTL", mock.AnythingOfType("string")).Return(false) }) t.Run("CacheIfBlockNumberIsFinalizedWhenBlockIsIrrelevantForPrimaryKey", func(t *testing.T) { @@ -107,6 +108,7 @@ func TestEvmJsonRpcCache_Set(t *testing.T) { t.Run("SkipWhenNoRefAndNoBlockNumberFound", func(t *testing.T) { mockConnector, _, cache := createCacheTestFixtures(10, 15, nil) + mockConnector.On("HasTTL", mock.AnythingOfType("string")).Return(false) req := common.NewNormalizedRequest([]byte(`{"jsonrpc":"2.0","method":"eth_getBalance","params":["0x123","latest"],"id":1}`)) resp := common.NewNormalizedResponse().WithBody(util.StringToReaderCloser(`{"result":"0x1234"}`)) @@ -119,6 +121,7 @@ func TestEvmJsonRpcCache_Set(t *testing.T) { t.Run("CacheIfBlockRefFoundWhetherBlockNumberExistsOrNot", func(t *testing.T) { mockConnector, mockNetwork, cache := createCacheTestFixtures(10, 15, nil) + mockConnector.On("HasTTL", mock.AnythingOfType("string")).Return(false) testCases := []struct { name string diff --git a/erpc/http_server_test.go b/erpc/http_server_test.go index 99ec2284..db7f0d29 100644 --- a/erpc/http_server_test.go +++ b/erpc/http_server_test.go @@ -842,182 +842,3 @@ 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") - }) -}