feat: support CORS by using rest.WithCors(...) (#1212)
* feat: support CORS by using rest.WithCors(...) * chore: add comments * refactor: lowercase unexported methods * ci: fix lint errors
This commit is contained in:
76
rest/internal/cors/handlers_test.go
Normal file
76
rest/internal/cors/handlers_test.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package cors
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCorsHandlerWithOrigins(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
origins []string
|
||||
expect string
|
||||
}{
|
||||
{
|
||||
name: "allow all origins",
|
||||
expect: allOrigins,
|
||||
},
|
||||
{
|
||||
name: "allow one origin",
|
||||
origins: []string{"local"},
|
||||
expect: "local",
|
||||
},
|
||||
{
|
||||
name: "allow many origins",
|
||||
origins: []string{"local", "remote"},
|
||||
expect: "local",
|
||||
},
|
||||
}
|
||||
|
||||
methods := []string{
|
||||
http.MethodOptions,
|
||||
http.MethodGet,
|
||||
http.MethodPost,
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
for _, method := range methods {
|
||||
test := test
|
||||
t.Run(test.name+"-handler", func(t *testing.T) {
|
||||
r := httptest.NewRequest(method, "http://localhost", nil)
|
||||
w := httptest.NewRecorder()
|
||||
handler := Handler(test.origins...)
|
||||
handler.ServeHTTP(w, r)
|
||||
if method == http.MethodOptions {
|
||||
assert.Equal(t, http.StatusNoContent, w.Result().StatusCode)
|
||||
} else {
|
||||
assert.Equal(t, http.StatusNotFound, w.Result().StatusCode)
|
||||
}
|
||||
assert.Equal(t, test.expect, w.Header().Get(allowOrigin))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
for _, method := range methods {
|
||||
test := test
|
||||
t.Run(test.name+"-middleware", func(t *testing.T) {
|
||||
r := httptest.NewRequest(method, "http://localhost", nil)
|
||||
w := httptest.NewRecorder()
|
||||
handler := Middleware(test.origins...)(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
handler.ServeHTTP(w, r)
|
||||
if method == http.MethodOptions {
|
||||
assert.Equal(t, http.StatusNoContent, w.Result().StatusCode)
|
||||
} else {
|
||||
assert.Equal(t, http.StatusOK, w.Result().StatusCode)
|
||||
}
|
||||
assert.Equal(t, test.expect, w.Header().Get(allowOrigin))
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user