-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption_test.go
97 lines (73 loc) · 1.71 KB
/
option_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
package cors
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/go-http-utils/headers"
"github.com/stretchr/testify/suite"
)
type OptionSuite struct {
suite.Suite
o *options
}
func (s *OptionSuite) SetupTest() {
s.o = &options{allowOrigin: true}
}
func (s *OptionSuite) TestExpose() {
err := SetExposeHeaders([]string{
headers.Accept,
headers.Accept,
headers.Authorization,
})(s.o)
s.Nil(err)
s.Equal([]string{headers.Accept, headers.Authorization}, s.o.exposeHeaders)
}
func (s *OptionSuite) TestAllowHeaders() {
err := SetAllowHeaders([]string{
headers.Accept,
headers.Accept,
headers.Authorization,
})(s.o)
s.Nil(err)
s.Equal([]string{headers.Accept, headers.Authorization}, s.o.allowHeaders)
}
func (s *OptionSuite) TestMethods() {
err := SetMethods([]string{
http.MethodGet,
http.MethodHead,
"Get",
})(s.o)
s.Nil(err)
s.Equal([]string{http.MethodGet, http.MethodHead}, s.o.methods)
}
func (s *OptionSuite) TestMaxAge() {
err := SetMaxAge(-1)(s.o)
s.Error(err, "cors: maxAge should > 0")
err = SetMaxAge(600)(s.o)
s.Nil(err)
s.Equal(600, s.o.maxAge)
err = SetMaxAge(601)(s.o)
s.Nil(err)
s.Equal(600, s.o.maxAge)
}
func (s *OptionSuite) TestCredentials() {
err := SetCredentials(true)(s.o)
s.Nil(err)
s.Equal(true, s.o.credentials)
}
func (s *OptionSuite) TestAllowOrigin() {
err := SetAllowOrigin(true)(s.o)
s.Nil(err)
s.Equal(true, s.o.allowOrigin)
}
func (s *OptionSuite) TestAllowOriginsValidator() {
err := SetAllowOriginValidator(func(_ *http.Request) string {
return "test"
})(s.o)
s.Nil(err)
s.Equal("test",
s.o.allowOriginValidator(httptest.NewRequest(http.MethodGet, "/", nil)))
}
func TestOption(t *testing.T) {
suite.Run(t, new(OptionSuite))
}