forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cors_test.go
83 lines (63 loc) · 2.92 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
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
package api
import (
"net/http"
"net/http/httptest"
"testing"
"gobot.io/x/gobot/gobottest"
)
func TestCORSIsOriginAllowed(t *testing.T) {
cors := &CORS{AllowOrigins: []string{"*"}}
cors.generatePatterns()
// When all the origins are accepted
gobottest.Assert(t, cors.isOriginAllowed("http://localhost:8000"), true)
gobottest.Assert(t, cors.isOriginAllowed("http://localhost:3001"), true)
gobottest.Assert(t, cors.isOriginAllowed("http://server.com"), true)
// When one origin is accepted
cors = &CORS{AllowOrigins: []string{"http://localhost:8000"}}
cors.generatePatterns()
gobottest.Assert(t, cors.isOriginAllowed("http://localhost:8000"), true)
gobottest.Assert(t, cors.isOriginAllowed("http://localhost:3001"), false)
gobottest.Assert(t, cors.isOriginAllowed("http://server.com"), false)
// When several origins are accepted
cors = &CORS{AllowOrigins: []string{"http://localhost:*", "http://server.com"}}
cors.generatePatterns()
gobottest.Assert(t, cors.isOriginAllowed("http://localhost:8000"), true)
gobottest.Assert(t, cors.isOriginAllowed("http://localhost:3001"), true)
gobottest.Assert(t, cors.isOriginAllowed("http://server.com"), true)
// When several origins are accepted within the same domain
cors = &CORS{AllowOrigins: []string{"http://*.server.com"}}
cors.generatePatterns()
gobottest.Assert(t, cors.isOriginAllowed("http://localhost:8000"), false)
gobottest.Assert(t, cors.isOriginAllowed("http://localhost:3001"), false)
gobottest.Assert(t, cors.isOriginAllowed("http://foo.server.com"), true)
gobottest.Assert(t, cors.isOriginAllowed("http://api.server.com"), true)
}
func TestCORSAllowedHeaders(t *testing.T) {
cors := &CORS{AllowOrigins: []string{"*"}, AllowHeaders: []string{"Header1", "Header2"}}
gobottest.Assert(t, cors.AllowedHeaders(), "Header1,Header2")
}
func TestCORSAllowedMethods(t *testing.T) {
cors := &CORS{AllowOrigins: []string{"*"}, AllowMethods: []string{"GET", "POST"}}
gobottest.Assert(t, cors.AllowedMethods(), "GET,POST")
cors.AllowMethods = []string{"GET", "POST", "PUT"}
gobottest.Assert(t, cors.AllowedMethods(), "GET,POST,PUT")
}
func TestCORS(t *testing.T) {
api := initTestAPI()
// Accepted origin
allowedOrigin := []string{"http://server.com"}
api.AddHandler(AllowRequestsFrom(allowedOrigin[0]))
request, _ := http.NewRequest("GET", "/api/", nil)
request.Header.Set("Origin", allowedOrigin[0])
response := httptest.NewRecorder()
api.ServeHTTP(response, request)
gobottest.Assert(t, response.Header()["Access-Control-Allow-Origin"], allowedOrigin)
// Not accepted Origin
disallowedOrigin := []string{"http://disallowed.com"}
request, _ = http.NewRequest("GET", "/api/", nil)
request.Header.Set("Origin", disallowedOrigin[0])
response = httptest.NewRecorder()
api.ServeHTTP(response, request)
gobottest.Refute(t, response.Header()["Access-Control-Allow-Origin"], disallowedOrigin)
gobottest.Refute(t, response.Header()["Access-Control-Allow-Origin"], allowedOrigin)
}