From 24a953ff9ff3210cf4036ad14cab1c821eed4617 Mon Sep 17 00:00:00 2001 From: canonbrother Date: Wed, 29 Mar 2023 13:52:10 +0800 Subject: [PATCH] switch rpc ci to http (#8) --- .github/workflows/rpc-test.yaml | 2 +- node/client.go | 4 ++-- node/http.go | 5 ++++- node/rpc_test.go | 25 ++++++++++++------------- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/.github/workflows/rpc-test.yaml b/.github/workflows/rpc-test.yaml index 1294d13..3b6fa0b 100644 --- a/.github/workflows/rpc-test.yaml +++ b/.github/workflows/rpc-test.yaml @@ -11,7 +11,7 @@ concurrency: cancel-in-progress: true env: - ETHLIBS_TEST_WS_URL: wss://goerli.infura.io/ws/v3/ + ETHLIBS_TEST_URL: https://goerli.infura.io/v3/ AUTH_ID: ${{ secrets.INFURA_PROJECT_ID }} AUTH_PASS: ${{ secrets.INFURA_API_KEY }} diff --git a/node/client.go b/node/client.go index b4838fb..3e8f0bc 100644 --- a/node/client.go +++ b/node/client.go @@ -4,8 +4,8 @@ import ( "bytes" "context" "encoding/json" - "net/url" "net/http" + "net/url" "github.com/pkg/errors" @@ -30,7 +30,7 @@ func NewClient(ctx context.Context, rawURL string, requestHeader http.Header) (C switch parsedURL.Scheme { case "http", "https": - transport, err = newHTTPTransport(ctx, parsedURL) + transport, err = newHTTPTransport(ctx, parsedURL, requestHeader) case "wss", "ws": transport, err = newWebsocketTransport(ctx, parsedURL, requestHeader) default: diff --git a/node/http.go b/node/http.go index 34a3fb4..68ff302 100644 --- a/node/http.go +++ b/node/http.go @@ -14,13 +14,15 @@ import ( "github.com/pkg/errors" ) -func newHTTPTransport(ctx context.Context, parsedURL *url.URL) (transport, error) { +func newHTTPTransport(ctx context.Context, parsedURL *url.URL, header http.Header) (transport, error) { return &httpTransport{ + header: header, rawURL: parsedURL.String(), }, nil } type httpTransport struct { + header http.Header rawURL string client *http.Client once sync.Once @@ -72,6 +74,7 @@ func (t *httpTransport) dispatchBytes(ctx context.Context, input []byte) ([]byte } r = r.WithContext(ctx) + r.Header = t.header r.Header.Add("Content-Type", "application/json") resp, err := t.client.Do(r) diff --git a/node/rpc_test.go b/node/rpc_test.go index ec84a6a..36dcd2f 100644 --- a/node/rpc_test.go +++ b/node/rpc_test.go @@ -2,11 +2,11 @@ package node_test import ( "context" + ob64 "encoding/base64" + "fmt" + "net/http" "os" "testing" - "net/http" - ob64 "encoding/base64" - "fmt" "github.com/stretchr/testify/require" @@ -15,27 +15,26 @@ import ( ) func getClient(t *testing.T, ctx context.Context) node.Client { - // These test require a ropsten websocket URL to test with, for example ws://localhost:8546 or wss://ropsten.infura.io/ws/v3/:YOUR_PROJECT_ID - base_url := os.Getenv("ETHLIBS_TEST_WS_URL") + base_url := os.Getenv("ETHLIBS_TEST_URL") if base_url == "" { - t.Skip("ETHLIBS_TEST_WS_URL not set, skipping test. Set to a valid websocket URL to execute this test.") + t.Skip("ETHLIBS_TEST_URL not set, skipping test. Set to a valid websocket URL to execute this test.") } auth_id := os.Getenv("AUTH_ID") if auth_id == "" { t.Skip("AUTH_ID not set, skipping test.") } - url := fmt.Sprintf("%s%s", base_url, auth_id) + url := fmt.Sprintf("%s%s", base_url, auth_id) - auth_pass := os.Getenv("AUTH_PASS") - if auth_pass == "" { + auth_pass := os.Getenv("AUTH_PASS") + if auth_pass == "" { t.Skip("AUTH_PASS not set, skipping test.") } - base64Header := ob64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", auth_id, auth_pass))) + base64Header := ob64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", auth_id, auth_pass))) - header := http.Header{ - "Authorization": {fmt.Sprintf("Basic %s", base64Header)}, - } + header := http.Header{ + "Authorization": {fmt.Sprintf("Basic %s", base64Header)}, + } conn, err := node.NewClient(ctx, url, header) require.NoError(t, err, "creating websocket connection should not fail") return conn