This repository has been archived by the owner on Dec 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmiddleware_cors_test.go
76 lines (63 loc) · 2.23 KB
/
middleware_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
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
package rye
import (
"net/http"
"net/http/httptest"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("CORS Middleware", func() {
var (
request *http.Request
response *httptest.ResponseRecorder
)
BeforeEach(func() {
response = httptest.NewRecorder()
request = &http.Request{
Header: make(map[string][]string, 0),
}
})
Describe("handle", func() {
Context("when origin header is not set", func() {
It("should return nil", func() {
resp := MiddlewareCORS()(response, request)
Expect(resp).To(BeNil())
})
})
Context("when origin header is set", func() {
Context("and CORS was instantiated with params", func() {
var (
testOrigin = "*.invisionapp.com"
testHeaders = "TestHeader"
testMethods = "GET, POST, TESTMETHOD"
)
It("should set all CORS headers from params", func() {
request.Header.Add("Origin", "*.invisionapp.com")
resp := NewMiddlewareCORS(testOrigin, testMethods, testHeaders)(response, request)
Expect(resp).To(BeNil())
Expect(response.Header().Get("Access-Control-Allow-Origin")).To(Equal(testOrigin))
Expect(response.Header().Get("Access-Control-Allow-Methods")).To(Equal(testMethods))
Expect(response.Header().Get("Access-Control-Allow-Headers")).To(Equal(testHeaders))
})
})
Context("and CORS was instantiated with defaults", func() {
It("should set all CORS headers using defaults", func() {
request.Header.Add("Origin", "*.invisionapp.com")
resp := MiddlewareCORS()(response, request)
Expect(resp).To(BeNil())
Expect(response.Header().Get("Access-Control-Allow-Origin")).To(Equal(DEFAULT_CORS_ALLOW_ORIGIN))
Expect(response.Header().Get("Access-Control-Allow-Methods")).To(Equal(DEFAULT_CORS_ALLOW_METHODS))
Expect(response.Header().Get("Access-Control-Allow-Headers")).To(Equal(DEFAULT_CORS_ALLOW_HEADERS))
})
})
Context("and we got a preflight request (OPTIONS)", func() {
It("should return a response with StopExecution", func() {
request.Method = "OPTIONS"
request.Header.Add("Origin", "*.invisionapp.com")
resp := MiddlewareCORS()(response, request)
Expect(resp).ToNot(BeNil())
Expect(resp.StopExecution).To(BeTrue())
})
})
})
})
})