Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request #183 from 0xProject/marcin/bugfix
Browse files Browse the repository at this point in the history
missing test scenarios
  • Loading branch information
eitu5ami authored Feb 26, 2024
2 parents 790ea5b + b5a87dc commit b7ba0e4
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 23 deletions.
17 changes: 0 additions & 17 deletions internal/proxy/healthchecker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package proxy
import (
"context"
"log/slog"
"net/http"
"os"
"testing"
"time"
Expand Down Expand Up @@ -42,19 +41,3 @@ func TestBasicHealthchecker(t *testing.T) {
healthchecker.isHealthy = true
assert.True(t, healthchecker.IsHealthy())
}

func TestGasLeftCall(t *testing.T) {
client := &http.Client{}
url := env.GetDefault("RPC_GATEWAY_NODE_URL_1", "https://cloudflare-eth.com")

result, err := performGasLeftCall(context.TODO(), client, url)
assert.NoError(t, err)
assert.NotZero(t, result)

// testing the timeout
ctx, cancelFunc := context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancelFunc()

_, err = performGasLeftCall(ctx, client, url)
assert.Error(t, err)
}
13 changes: 7 additions & 6 deletions internal/proxy/healthchecker_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
Expand All @@ -25,7 +26,7 @@ func hexToUint(hexString string) (uint64, error) {
}

func performGasLeftCall(c context.Context, client *http.Client, url string) (uint64, error) {
var gasLeftCallRaw = []byte(`
var gasLeftCallRaw = bytes.NewBufferString(`
{
"method": "eth_call",
"params": [
Expand All @@ -48,28 +49,28 @@ func performGasLeftCall(c context.Context, client *http.Client, url string) (uin
}
`)

r, err := http.NewRequestWithContext(c, http.MethodPost, url, bytes.NewBuffer(gasLeftCallRaw))
r, err := http.NewRequestWithContext(c, http.MethodPost, url, gasLeftCallRaw)
if err != nil {
return 0, errors.Wrap(err, "new request failed")
return 0, fmt.Errorf("performGasLeftCall: NewRequestWithContext error: %w", err)
}

r.Header.Add(headers.ContentType, "application/json")
r.Header.Set(headers.UserAgent, userAgent)

resp, err := client.Do(r)
if err != nil {
return 0, errors.Wrap(err, "request failed")
return 0, fmt.Errorf("performGasLeftCall: client.Do error: %w", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return 0, errors.New("gas left check failed")
return 0, errors.New("performGasLeftCall: non-200 HTTP response")
}

result := &JSONRPCResponse{}
err = json.NewDecoder(resp.Body).Decode(result)
if err != nil {
return 0, errors.Wrap(err, "json response decode failed")
return 0, fmt.Errorf("performGasLeftCall: json.Decode error: %w", err)
}

return hexToUint(result.Result)
Expand Down
84 changes: 84 additions & 0 deletions internal/proxy/healthchecker_utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package proxy

import (
"context"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/go-http-utils/headers"
"github.com/stretchr/testify/assert"
)

func TestPerformGasLeftCallErrors(t *testing.T) {
t.Parallel()

t.Run("expect error when HTTP status is not 200", func(t *testing.T) {
t.Parallel()

server := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if assert.Contains(t, r.Header, headers.ContentType) {
assert.Equal(t, "application/json", r.Header.Get(headers.ContentType))
}

w.WriteHeader(http.StatusServiceUnavailable)
}),
)
defer server.Close()

gas, err := performGasLeftCall(context.TODO(), &http.Client{}, server.URL)

assert.Zero(t, gas)
assert.Error(t, err)
assert.ErrorContains(t, err, "non-200 HTTP response")
})

t.Run("expect error when JSON payload is invalid", func(t *testing.T) {
t.Parallel()

server := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if assert.Contains(t, r.Header, headers.ContentType) {
assert.Equal(t, "application/json", r.Header.Get(headers.ContentType))
}

w.Write([]byte(`{{}`))
w.WriteHeader(http.StatusOK)
}),
)
defer server.Close()

gas, err := performGasLeftCall(context.TODO(), &http.Client{}, server.URL)

assert.Zero(t, gas)
assert.Error(t, err)
assert.ErrorContains(t, err, "json.Decode error")
})

t.Run("expect error when server timeouts", func(t *testing.T) {
t.Parallel()

server := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if assert.Contains(t, r.Header, headers.ContentType) {
assert.Equal(t, "application/json", r.Header.Get(headers.ContentType))
}
<-time.After(time.Second * 3)

w.WriteHeader(http.StatusServiceUnavailable)
}),
)
defer server.Close()

timeout, cancel := context.WithTimeout(context.TODO(), time.Second*1)
defer cancel()

gas, err := performGasLeftCall(timeout, &http.Client{}, server.URL)

assert.Zero(t, gas)
assert.Error(t, err)
assert.ErrorIs(t, err, context.DeadlineExceeded)
})
}

0 comments on commit b7ba0e4

Please sign in to comment.