-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathcors_tests.lua
More file actions
170 lines (135 loc) · 5.26 KB
/
cors_tests.lua
File metadata and controls
170 lines (135 loc) · 5.26 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
luaunit = require('luaunit')
-- stub the 'core' global variable that HAProxy creates
core = {}
core.register_action = function()end
core.Debug = function(s)end
cors = require('cors')
-- tests...
function test_wildcard_origin_allowed_when_contains_wildcard_returns_wildcard()
local allowed = {"*"}
local result = cors.wildcard_origin_allowed(allowed)
luaunit.assertEquals(result, "*")
end
function test_wildcard_origin_allowed_when_does_not_contain_wildcard_returns_nil()
local allowed = {"testcom", "localhost"}
local result = cors.wildcard_origin_allowed(allowed)
luaunit.assertNil(result)
end
function test_trim_removes_leading_and_trailing_whitespace()
local result = cors.trim(" test ")
luaunit.assertEquals(result, "test")
end
function test_specifies_scheme_when_scheme_given_returns_true()
local result = cors.specifies_scheme("http://localhost")
luaunit.assertTrue(result)
end
function test_specifies_scheme_when_scheme_not_given_returns_false()
local result = cors.specifies_scheme("localhost")
luaunit.assertFalse(result)
end
function test_specifies_scheme_when_generic_scheme_given_returns_false()
local result = cors.specifies_scheme("//localhost")
luaunit.assertFalse(result)
end
function test_specifies_generic_scheme_when_any_scheme_returns_true()
local result = cors.specifies_generic_scheme("//localhost")
luaunit.assertTrue(result)
end
function test_specifies_generic_scheme_when_scheme_returns_false()
local result = cors.specifies_generic_scheme("http://localhost")
luaunit.assertFalse(result)
end
function test_begins_with_dot_when_begins_with_dot_returns_true()
local result = cors.begins_with_dot(".test.com")
luaunit.assertTrue(result)
end
function test_begins_with_dot_when_does_not_begin_with_dot_returns_false()
local result = cors.begins_with_dot("test.com")
luaunit.assertFalse(result)
end
function test_build_pattern_1()
local result = cors.build_pattern("localhost")
luaunit.assertEquals(result, "//localhost$")
end
function test_build_pattern_2()
local result = cors.build_pattern("https://localhost")
luaunit.assertEquals(result, "https://localhost$")
end
function test_build_pattern_3()
local result = cors.build_pattern("http://localhost:8080")
luaunit.assertEquals(result, "http://localhost:8080$")
end
function test_build_pattern_4()
local result = cors.build_pattern("//localhost:8080")
luaunit.assertEquals(result, "//localhost:8080$")
end
function test_build_pattern_5()
local result = cors.build_pattern(".test.com")
luaunit.assertEquals(result, "%.test%.com$")
end
function test_build_pattern_6()
local result = cors.build_pattern(".test.com:8080")
luaunit.assertEquals(result, "%.test%.com:8080$")
end
function test_build_pattern_7()
local result = cors.build_pattern("http://test.com:*")
luaunit.assertEquals(result, "http://test%.com[:]+[0-9]+$")
end
function test_build_pattern_8()
local result = cors.build_pattern("te-st.com")
luaunit.assertEquals(result, "//te%-st%.com$")
end
function test_get_allowed_origin_case_1()
local result = cors.get_allowed_origin("http://test.com", {"http://test.com"})
luaunit.assertEquals(result, "http://test.com")
end
function test_get_allowed_origin_case_2()
local result = cors.get_allowed_origin("http://test.com:8080", {"http://test.com:8080"})
luaunit.assertEquals(result, "http://test.com:8080")
end
function test_get_allowed_origin_case_3()
local result = cors.get_allowed_origin("http://localhost", {"localhost"})
luaunit.assertEquals(result, "http://localhost")
end
function test_get_allowed_origin_case_4()
local result = cors.get_allowed_origin("http://sub.test.com", {".test.com"})
luaunit.assertEquals(result, "http://sub.test.com")
end
function test_get_allowed_origin_case_5()
local result = cors.get_allowed_origin("https://sub.test.com", {".test.com"})
luaunit.assertEquals(result, "https://sub.test.com")
end
function test_get_allowed_origin_case_6()
local result = cors.get_allowed_origin("https://localhost", {"//localhost"})
luaunit.assertEquals(result, "https://localhost")
end
function test_get_allowed_origin_case_7()
local result = cors.get_allowed_origin("https://sub.test.com:8080", {".test.com:8080"})
luaunit.assertEquals(result, "https://sub.test.com:8080")
end
function test_get_allowed_origin_case_8()
local result = cors.get_allowed_origin("https://test.com", {".test.com"})
luaunit.assertEquals(result, nil)
end
function test_get_allowed_origin_case_9()
local result = cors.get_allowed_origin("https://test.com", {"localhost"})
luaunit.assertEquals(result, nil)
end
function test_get_allowed_origin_case_10()
local result = cors.get_allowed_origin("https://sub.test.com", {"test.com"})
luaunit.assertEquals(result, nil)
end
function test_get_allowed_origin_case_11()
local result = cors.get_allowed_origin("https://test.com", {"localhost", "*"})
luaunit.assertEquals(result, "*")
end
function test_get_allowed_origin_case_12()
local result = cors.get_allowed_origin("http://test.com:8080", {"http://test.com:*"})
luaunit.assertEquals(result, "http://test.com:8080")
end
function test_get_allowed_origin_case_13()
local result = cors.get_allowed_origin("https://te-st.com", {"te-st.com"})
luaunit.assertEquals(result, "https://te-st.com")
end
-- this line must go at the end
os.exit(luaunit.LuaUnit.run())