authorizer/server/test/cors_test.go

46 lines
1.2 KiB
Go
Raw Normal View History

2021-12-22 10:01:45 +00:00
package test
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/authorizerdev/authorizer/server/constants"
2021-12-22 10:01:45 +00:00
"github.com/authorizerdev/authorizer/server/db"
"github.com/authorizerdev/authorizer/server/env"
2021-12-22 10:01:45 +00:00
"github.com/authorizerdev/authorizer/server/router"
"github.com/authorizerdev/authorizer/server/session"
"github.com/stretchr/testify/assert"
)
func TestCors(t *testing.T) {
2021-12-22 10:01:45 +00:00
constants.ENV_PATH = "../../.env.sample"
constants.DATABASE_URL = "../../data.db"
env.InitEnv()
2021-12-22 10:01:45 +00:00
db.InitDB()
session.InitSession()
router := router.InitRouter()
allowedOrigin := "http://localhost:8080" // The allowed origin that you want to check
notAllowedOrigin := "http://myapp.com"
2021-12-22 10:01:45 +00:00
server := httptest.NewServer(router)
defer server.Close()
client := &http.Client{}
req, _ := http.NewRequest(
"GET",
2021-12-22 10:01:45 +00:00
"http://"+server.Listener.Addr().String()+"/graphql",
nil,
)
req.Header.Add("Origin", allowedOrigin)
get, _ := client.Do(req)
// You should get your origin (or a * depending on your config) if the
// passed origin is allowed.
o := get.Header.Get("Access-Control-Allow-Origin")
assert.NotEqual(t, o, notAllowedOrigin, "Origins should not match")
assert.Equal(t, o, allowedOrigin, "Origins don't match")
}