-
Notifications
You must be signed in to change notification settings - Fork 382
/
Copy pathcors_test.go
43 lines (36 loc) · 1.01 KB
/
cors_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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")
}