-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcorsmiddleware_test.go
131 lines (107 loc) · 3.28 KB
/
corsmiddleware_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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package corsmiddleware_test
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/SergioFloresG/corsmiddleware"
)
func TestStaticDomainSuccess(t *testing.T) {
testCases := []struct {
domain string
origin string
}{
// Unsecure
{domain: "http://localhost", origin: "http://localhost"},
{domain: "http://example.com", origin: "http://example.com"},
{domain: "http://example.com", origin: "http://example.com"},
{domain: "http://internal.example.com", origin: "http://internal.example.com"},
// Secure
{domain: "https://example.com", origin: "https://example.com"},
{domain: "https://example.com", origin: "https://example.com"},
{domain: "https://internal.example.com", origin: "https://internal.example.com"},
}
ctx := context.Background()
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {})
var req *http.Request
var conf *corsmiddleware.Config
var err error
setupTest := func(_ testing.TB) {
conf = corsmiddleware.CreateConfig()
req, err = http.NewRequestWithContext(ctx, http.MethodOptions, "https://localhost", nil)
if err != nil {
t.Fatal(err)
}
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("[%s]", tc.domain), func(st *testing.T) {
setupTest(t)
recorder := httptest.NewRecorder()
conf.AllowOrigins = []string{tc.domain}
req.Header.Set("Origin", tc.origin)
handler, err := corsmiddleware.New(ctx, next, conf, "statics")
if err != nil {
t.Fatal(err)
}
handler.ServeHTTP(recorder, req)
assertEqual(t, recorder.Code, http.StatusNoContent)
assertEqual(t, recorder.Header().Get("Access-Control-Allow-Origin"), tc.domain)
})
}
}
func TestWildcardDomainSuccess(t *testing.T) {
allowOrigins := []string{"https://*.foo.com", "https://*.example.com", "https://localhost"}
testCases := []string{
"https://localhost",
"https://bar.foo.com",
"https://loop.foo.com",
"https://netpay.foo.com",
"https://sub.example.com",
"https://foo.example.com",
"https://bar.example.com",
}
ctx := context.Background()
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {})
var req *http.Request
var err error
conf := corsmiddleware.CreateConfig()
conf.AllowOrigins = allowOrigins
setupTest := func(_ testing.TB) {
req, err = http.NewRequestWithContext(ctx, http.MethodOptions, "http://localhost", nil)
if err != nil {
t.Fatal(err)
}
}
testHelper := func(t *testing.T, originDomain string) {
t.Helper()
recorder := httptest.NewRecorder()
req.Header.Set("Origin", originDomain)
handler, err := corsmiddleware.New(ctx, next, conf, "wildcard")
if err != nil {
t.Fatal(err)
}
handler.ServeHTTP(recorder, req)
assertEqual(t, recorder.Code, http.StatusNoContent)
assertEqual(t, recorder.Header().Get("Access-Control-Allow-Origin"), originDomain)
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("[%s]", tc), func(t *testing.T) {
setupTest(t)
testHelper(t, tc)
})
}
conf.AllowOrigins = []string{"*"}
for _, tc := range testCases {
t.Run(fmt.Sprintf("AnyDomain[%s]", tc), func(t *testing.T) {
setupTest(t)
testHelper(t, tc)
})
}
}
func assertEqual(t *testing.T, value, expected interface{}) {
t.Helper()
if value != expected {
t.Errorf("assertion failed: %[1]v (%[1]T) != %[2]v (%[2]T)", value, expected)
}
}