|
|
@ -3,25 +3,40 @@ package rest
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"testing"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func TestCorsHandler(t *testing.T) {
|
|
|
|
func TestCorsHandlerWithOrigins(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
tests := []struct {
|
|
|
|
handler := CorsHandler()
|
|
|
|
name string
|
|
|
|
handler.ServeHTTP(w, nil)
|
|
|
|
origins []string
|
|
|
|
assert.Equal(t, http.StatusNoContent, w.Result().StatusCode)
|
|
|
|
expect string
|
|
|
|
assert.Equal(t, allOrigin, w.Header().Get(allowOrigin))
|
|
|
|
}{
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
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",
|
|
|
|
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestCorsHandlerWithOrigins(t *testing.T) {
|
|
|
|
for _, test := range tests {
|
|
|
|
origins := []string{"local", "remote"}
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
handler := CorsHandler(origins...)
|
|
|
|
handler := CorsHandler(test.origins...)
|
|
|
|
handler.ServeHTTP(w, nil)
|
|
|
|
handler.ServeHTTP(w, nil)
|
|
|
|
assert.Equal(t, http.StatusNoContent, w.Result().StatusCode)
|
|
|
|
assert.Equal(t, http.StatusNoContent, w.Result().StatusCode)
|
|
|
|
assert.Equal(t, strings.Join(origins, separator), w.Header().Get(allowOrigin))
|
|
|
|
assert.Equal(t, test.expect, w.Header().Get(allowOrigin))
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|