Skip to content

Commit

Permalink
Accept devnet/localhost node URLs (#10)
Browse files Browse the repository at this point in the history
* Accept connections to devnet and localhost nodes

* Format: gofmt
  • Loading branch information
dcorral authored Apr 26, 2023
1 parent 519051f commit 6ea5a4a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rpc-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ concurrency:
cancel-in-progress: true

env:
ETHLIBS_TEST_URL: https://goerli.infura.io/v3/
NODE_URL: ${{ secrets.NODE_URL }}
AUTH_ID: ${{ secrets.INFURA_PROJECT_ID }}
AUTH_PASS: ${{ secrets.INFURA_API_KEY }}

Expand Down
23 changes: 18 additions & 5 deletions node/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,39 @@ import (
)

func getClient(t *testing.T, ctx context.Context) node.Client {
base_url := os.Getenv("ETHLIBS_TEST_URL")

// Save base URL. Fail if not set.
// NODE_URL must end with trailing '/' i.e http://url:port/
base_url := os.Getenv("NODE_URL")
if base_url == "" {
t.Skip("ETHLIBS_TEST_URL not set, skipping test. Set to a valid http/ws URL to execute this test.")
t.Skip("NODE_URL not set, skipping test.")
}

// If AUTH_ID is not set, return connection without Basic Auth header
auth_id := os.Getenv("AUTH_ID")
if auth_id == "" {
t.Skip("AUTH_ID not set, skipping test.")
conn, err := node.NewClient(ctx, base_url, http.Header{})
require.NoError(t, err, "creating websocket connection should not fail")
return conn
}
url := fmt.Sprintf("%s%s", base_url, auth_id)

// If AUTH_ID is present then check AUTH_PASS and create Basic Auth http header
// This is ment for INFURA test nodes to work in a secure way
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)))
// format URL -> base_url/auth_id
url := fmt.Sprintf("%s%s", base_url, auth_id)

// Create Basic Auth token base64(id:pass) for http header
base64Header := ob64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", auth_id, auth_pass)))
header := http.Header{
"Authorization": {fmt.Sprintf("Basic %s", base64Header)},
}

// Create connection
conn, err := node.NewClient(ctx, url, header)
require.NoError(t, err, "creating websocket connection should not fail")
return conn
Expand Down

0 comments on commit 6ea5a4a

Please sign in to comment.