Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into e2e_passing
Browse files Browse the repository at this point in the history
  • Loading branch information
sreuland committed Sep 8, 2023
2 parents 800fdd5 + 60e9539 commit 211d91f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
9 changes: 8 additions & 1 deletion cmd/soroban-rpc/internal/jsonrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/creachadair/jrpc2/jhttp"
"github.com/go-chi/chi/middleware"
"github.com/prometheus/client_golang/prometheus"
"github.com/rs/cors"
"github.com/stellar/go/support/log"

"github.com/stellar/soroban-tools/cmd/soroban-rpc/internal/config"
Expand Down Expand Up @@ -277,9 +278,15 @@ func NewJSONRPCHandler(cfg *config.Config, params HandlerParams) Handler {
// Limit request sizes to 10MB
handler = http.MaxBytesHandler(handler, 1024*1024*10)

corsMiddleware := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedHeaders: []string{"*"},
AllowedMethods: []string{"GET", "PUT", "POST", "PATCH", "DELETE", "HEAD", "OPTIONS"},
})

return Handler{
bridge: bridge,
logger: params.Logger,
Handler: handler,
Handler: corsMiddleware.Handler(handler),
}
}
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\"}"))
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)
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)
}

0 comments on commit 211d91f

Please sign in to comment.