Skip to content

Commit 27a5539

Browse files
tests
1 parent 4204f7f commit 27a5539

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

lib/cors.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ M.build_pattern = function(pattern)
5656
pattern = "//" .. pattern
5757
end
5858

59+
-- escape dots and dashes in pattern
60+
pattern = pattern:gsub("%.", "%%.")
61+
pattern = pattern:gsub("%-", "%%-")
62+
5963
-- an asterisk for the port means allow all ports
6064
if string.find(pattern, "[:]+%*$") ~= nil then
6165
pattern = pattern:gsub("[:]+%*$", "[:]+[0-9]+")
6266
end
6367

64-
-- escape dots and dashes in pattern
65-
pattern = pattern:gsub("%.", "%%.")
66-
pattern = pattern:gsub("%-", "%%-")
67-
6868
-- append end character
6969
pattern = pattern .. "$"
7070
return pattern

tests/cors_tests.lua

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ function test_build_pattern_7()
9696
luaunit.assertEquals(result, "http://test%.com[:]+[0-9]+$")
9797
end
9898

99+
function test_build_pattern_8()
100+
local result = cors.build_pattern("te-st.com")
101+
luaunit.assertEquals(result, "//te%-st%.com$")
102+
end
103+
99104
function test_get_allowed_origin_case_1()
100105
local result = cors.get_allowed_origin("http://test.com", {"http://test.com"})
101106
luaunit.assertEquals(result, "http://test.com")
@@ -151,11 +156,15 @@ function test_get_allowed_origin_case_11()
151156
luaunit.assertEquals(result, "*")
152157
end
153158

154-
155159
function test_get_allowed_origin_case_12()
156160
local result = cors.get_allowed_origin("http://test.com:8080", {"http://test.com:*"})
157161
luaunit.assertEquals(result, "http://test.com:8080")
158162
end
159163

164+
function test_get_allowed_origin_case_13()
165+
local result = cors.get_allowed_origin("https://te-st.com", {"te-st.com"})
166+
luaunit.assertEquals(result, "https://te-st.com")
167+
end
168+
160169
-- this line must go at the end
161-
os.exit(luaunit.LuaUnit.run())
170+
os.exit(luaunit.LuaUnit.run())

0 commit comments

Comments
 (0)