Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

soroban-rpc: add CORS test #944

Merged
merged 1 commit into from
Sep 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions cmd/soroban-rpc/internal/test/cors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package test

import (
"bytes"
"io"
"net/http"
"testing"

"github.com/stretchr/testify/require"
)

// TestCORS ensures that we receive the correct CORS headers as a response to an HTTP request.
// Specifically, when we include an Origin header in the request, a soroban-rpc should response
// with a corresponding Access-Control-Allow-Origin.
func TestCORS(t *testing.T) {
test := NewTest(t)

request, err := http.NewRequest("POST", test.sorobanRPCURL(), bytes.NewBufferString("{\"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"getHealth\"}"))

Check failure on line 18 in cmd/soroban-rpc/internal/test/cors_test.go

View workflow job for this annotation

GitHub Actions / golangci

should rewrite http.NewRequestWithContext or add (*Request).WithContext (noctx)
require.NoError(t, err)
request.Header.Set("Content-Type", "application/json")
origin := "testorigin.com"
request.Header.Set("Origin", origin)

var client http.Client
response, err := client.Do(request)

Check failure on line 25 in cmd/soroban-rpc/internal/test/cors_test.go

View workflow job for this annotation

GitHub Actions / golangci

response body must be closed (bodyclose)
require.NoError(t, err)
_, err = io.ReadAll(response.Body)
require.NoError(t, err)

accessControl := response.Header.Get("Access-Control-Allow-Origin")
require.Equal(t, origin, accessControl)
}
Loading