forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcors_test.go
82 lines (60 loc) · 2.64 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
package api
import (
"testing"
"github.com/hybridgroup/gobot"
)
func TestNewCORS(t *testing.T) {
var cors interface{} = NewCORS([]string{})
// Does it return a pointer to an instance of CORS?
_, ok := cors.(*CORS)
if !ok {
t.Errorf("NewCORS() should have returned a *CORS")
}
}
func TestNewCorsSetsProperties(t *testing.T) {
allowedOrigins := []string{"http://*server:*", "http://localhost:*"}
allowedMethods := []string{"GET", "POST"}
allowedHeaders := []string{"Origin", "Content-Type"}
contentType := "application/json; charset=utf-8"
allowOriginPatterns := []string{"^http://.*server:.*$", "^http://localhost:.*$"}
cors := NewCORS(allowedOrigins)
gobot.Assert(t, cors.AllowOrigins, allowedOrigins)
gobot.Assert(t, cors.AllowMethods, allowedMethods)
gobot.Assert(t, cors.AllowHeaders, allowedHeaders)
gobot.Assert(t, cors.ContentType, contentType)
gobot.Assert(t, cors.allowOriginPatterns, allowOriginPatterns)
}
func TestCORSIsOriginAllowed(t *testing.T) {
cors := NewCORS([]string{"*"})
// When all the origins are accepted
gobot.Assert(t, cors.isOriginAllowed("http://localhost:8000"), true)
gobot.Assert(t, cors.isOriginAllowed("http://localhost:3001"), true)
gobot.Assert(t, cors.isOriginAllowed("http://server.com"), true)
// When one origin is accepted
cors = NewCORS([]string{"http://localhost:8000"})
gobot.Assert(t, cors.isOriginAllowed("http://localhost:8000"), true)
gobot.Assert(t, cors.isOriginAllowed("http://localhost:3001"), false)
gobot.Assert(t, cors.isOriginAllowed("http://server.com"), false)
// When several origins are accepted
cors = NewCORS([]string{"http://localhost:*", "http://server.com"})
gobot.Assert(t, cors.isOriginAllowed("http://localhost:8000"), true)
gobot.Assert(t, cors.isOriginAllowed("http://localhost:3001"), true)
gobot.Assert(t, cors.isOriginAllowed("http://server.com"), true)
// When several origins are accepted within the same domain
cors = NewCORS([]string{"http://*.server.com"})
gobot.Assert(t, cors.isOriginAllowed("http://localhost:8000"), false)
gobot.Assert(t, cors.isOriginAllowed("http://localhost:3001"), false)
gobot.Assert(t, cors.isOriginAllowed("http://foo.server.com"), true)
gobot.Assert(t, cors.isOriginAllowed("http://api.server.com"), true)
}
func TestCORSAllowedHeaders(t *testing.T) {
cors := NewCORS([]string{"*"})
cors.AllowHeaders = []string{"Header1", "Header2"}
gobot.Assert(t, cors.AllowedHeaders(), "Header1,Header2")
}
func TestCORSAllowedMethods(t *testing.T) {
cors := NewCORS([]string{"*"})
gobot.Assert(t, cors.AllowedMethods(), "GET,POST")
cors.AllowMethods = []string{"GET", "POST", "PUT"}
gobot.Assert(t, cors.AllowedMethods(), "GET,POST,PUT")
}