Skip to content

Commit

Permalink
add test that CORS is enabled correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
kian99 committed Sep 4, 2024
1 parent bc70c8c commit cdcc91a
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions cmd/jimmsrv/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"

Expand Down Expand Up @@ -483,3 +484,46 @@ func TestCleanupDoesNotPanic_SessionStoreRelatedCleanups(t *testing.T) {

svc.Cleanup()
}

func TestCORS(t *testing.T) {
c := qt.New(t)

_, _, cofgaParams, err := jimmtest.SetupTestOFGAClient(c.Name())
c.Assert(err, qt.IsNil)
p := jimmtest.NewTestJimmParams(c)
p.OpenFGAParams = cofgaParamsToJIMMOpenFGAParams(*cofgaParams)
allowedOrigin := "http://my-referrer.com"
p.CorsAllowedOrigins = []string{allowedOrigin}
p.InsecureSecretStorage = true

svc, err := jimmsvc.NewService(context.Background(), p)
c.Assert(err, qt.IsNil)
defer svc.Cleanup()

srv := httptest.NewServer(svc)
c.Cleanup(srv.Close)

url, err := url.Parse(srv.URL + "/debug/info")
c.Assert(err, qt.IsNil)
// Invalid origin won't receive CORS headers.
req := http.Request{
Method: "GET",
URL: url,
Header: http.Header{"Origin": []string{"123"}},
}
response, err := srv.Client().Do(&req)
c.Assert(err, qt.IsNil)
defer response.Body.Close()
c.Assert(response.StatusCode, qt.Equals, http.StatusOK)
c.Assert(response.Header.Get("Access-Control-Allow-Credentials"), qt.Equals, "")
c.Assert(response.Header.Get("Access-Control-Allow-Origin"), qt.Equals, "")

// Valid origin should receive CORS headers.
req.Header = http.Header{"Origin": []string{allowedOrigin}}
response, err = srv.Client().Do(&req)
c.Assert(err, qt.IsNil)
defer response.Body.Close()
c.Assert(response.StatusCode, qt.Equals, http.StatusOK)
c.Assert(response.Header.Get("Access-Control-Allow-Credentials"), qt.Equals, "true")
c.Assert(response.Header.Get("Access-Control-Allow-Origin"), qt.Equals, allowedOrigin)
}

0 comments on commit cdcc91a

Please sign in to comment.