Skip to content

Commit

Permalink
rest/cors_test: tests for empty Access-Control-Request-Headers in pre…
Browse files Browse the repository at this point in the history
…flight requests

Signed-off-by: Maciej Borzecki <maciej.borzecki@rndity.com>
  • Loading branch information
bboozzoo committed Nov 30, 2016
1 parent 9ec7f2b commit 7535cd0
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions rest/cors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package rest

import (
"net/http"
"testing"

"github.com/ant0ine/go-json-rest/rest/test"
)

func TestCorsMiddlewareEmptyAccessControlRequestHeaders(t *testing.T) {
api := NewApi()

// the middleware to test
api.Use(&CorsMiddleware{
OriginValidator: func(_ string, _ *Request) bool {
return true
},
AllowedMethods: []string{
"GET",
"POST",
"PUT",
},
AllowedHeaders: []string{
"Origin",
"Referer",
},
})

// wrap all
handler := api.MakeHandler()

req, _ := http.NewRequest("OPTIONS", "http://localhost", nil)
req.Header.Set("Origin", "http://another.host")
req.Header.Set("Access-Control-Request-Method", "PUT")
req.Header.Set("Access-Control-Request-Headers", "")

recorded := test.RunRequest(t, handler, req)
t.Logf("recorded: %+v\n", recorded.Recorder)
recorded.CodeIs(200)
recorded.HeaderIs("Access-Control-Allow-Methods", "GET,POST,PUT")
recorded.HeaderIs("Access-Control-Allow-Headers", "Origin,Referer")
recorded.HeaderIs("Access-Control-Allow-Origin", "http://another.host")
}

0 comments on commit 7535cd0

Please sign in to comment.