faas/gateway/tests/cors_test.go
John McCabe fac3345668 Use http package consts for http methods
This commit replaces occurences of http method strings with the
corresponding consts from the http package.

*Note* UPDATE is not strictly speaking a valid method and as such isn't
part of the http package (should be a PUT or PATCH?)

Signed-off-by: John McCabe <john@johnmccabe.net>
2018-03-23 16:37:33 +00:00

37 lines
860 B
Go

package tests
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/openfaas/faas/gateway/handlers"
)
type customHandler struct {
}
func (h customHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
func Test_HeadersAdded(t *testing.T) {
rr := httptest.NewRecorder()
handler := customHandler{}
host := "store.openfaas.com"
decorated := handlers.DecorateWithCORS(handler, host)
request, _ := http.NewRequest(http.MethodGet, "/", nil)
decorated.ServeHTTP(rr, request)
actual := rr.Header().Get("Access-Control-Allow-Origin")
if actual != host {
t.Errorf("Access-Control-Allow-Origin: want: %s got: %s", host, actual)
}
actualMethods := rr.Header().Get("Access-Control-Allow-Methods")
if actualMethods != http.MethodGet {
t.Errorf("Access-Control-Allow-Methods: want: %s got: %s", http.MethodGet, actualMethods)
}
}