-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcors_test.go
More file actions
104 lines (92 loc) · 2.87 KB
/
cors_test.go
File metadata and controls
104 lines (92 loc) · 2.87 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package api_test
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/bjaus/api"
)
func TestCORS(t *testing.T) {
t.Parallel()
tests := map[string]struct {
cfg []api.CORSConfig
method string
wantStatus int
wantHeader map[string]string
}{
"default headers on GET": {
method: http.MethodGet,
wantStatus: http.StatusOK,
wantHeader: map[string]string{
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, PUT, PATCH, DELETE, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
"Vary": "Origin",
},
},
"preflight OPTIONS returns 204": {
method: http.MethodOptions,
wantStatus: http.StatusNoContent,
wantHeader: map[string]string{
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, PUT, PATCH, DELETE, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
"Vary": "Origin",
},
},
"custom config overrides defaults": {
cfg: []api.CORSConfig{{
AllowOrigins: []string{"https://example.com"},
AllowMethods: []string{"GET", "POST"},
AllowHeaders: []string{"X-Custom"},
ExposeHeaders: []string{"X-Exposed"},
MaxAge: 3600,
}},
method: http.MethodGet,
wantStatus: http.StatusOK,
wantHeader: map[string]string{
"Access-Control-Allow-Origin": "https://example.com",
"Access-Control-Allow-Methods": "GET, POST",
"Access-Control-Allow-Headers": "X-Custom",
"Access-Control-Expose-Headers": "X-Exposed",
"Access-Control-Max-Age": "3600",
"Vary": "Origin",
},
},
"credentials header when AllowCredentials is true": {
cfg: []api.CORSConfig{{
AllowOrigins: []string{"*"},
AllowMethods: []string{"GET"},
AllowHeaders: []string{"Content-Type"},
AllowCredentials: true,
}},
method: http.MethodGet,
wantStatus: http.StatusOK,
wantHeader: map[string]string{
"Access-Control-Allow-Credentials": "true",
},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()
mw := api.CORS(tc.cfg...)
handler := mw(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
}))
srv := httptest.NewServer(handler)
t.Cleanup(srv.Close)
req, err := http.NewRequestWithContext(context.Background(), tc.method, srv.URL+"/", nil)
require.NoError(t, err)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer func() { require.NoError(t, resp.Body.Close()) }()
assert.Equal(t, tc.wantStatus, resp.StatusCode)
for header, want := range tc.wantHeader {
assert.Equal(t, want, resp.Header.Get(header), "header %s", header)
}
})
}
}