Skip to content

Commit

Permalink
throws an error for invalid arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
vm-001 committed Apr 14, 2023
1 parent daf8caa commit 1d09858
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
9 changes: 7 additions & 2 deletions kong/tools/mime_type.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ local ipairs = ipairs
local lower = string.lower
local sub = string.sub
local find = string.find
local type = type
local error = error

local WILDCARD = "*"

Expand Down Expand Up @@ -97,8 +99,11 @@ end
-- @tparam table other Other mime-type
-- @treturn boolean Returns `true` if this mime-type includes other, `false` otherwise
local function includes(this, other)
if not other then
return false
if type(this) ~= "table" then
error("this must be a table", 2)
end
if type(other) ~= "table" then
error("other must be a table", 2)
end

local this_type = this.type
Expand Down
12 changes: 8 additions & 4 deletions spec/01-unit/26-mime-type_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local parse_mime_type = require "kong.tools.mime_type".parse_mime_type
local includes = require "kong.tools.mime_type".includes
local mime_type = require "kong.tools.mime_type"

describe("kong.tools.mime_type", function()
describe("parse_mime_type()", function()
Expand Down Expand Up @@ -72,7 +71,7 @@ describe("kong.tools.mime_type", function()
}
}
for i, case in ipairs(cases) do
local type, subtype, params = parse_mime_type(case.mime_type)
local type, subtype, params = mime_type.parse_mime_type(case.mime_type)
local result = { type = type, subtype = subtype, params = params }
assert.same(case.result, result, "case: " .. i .. " failed" )
end
Expand Down Expand Up @@ -155,9 +154,14 @@ describe("kong.tools.mime_type", function()
}

for i, case in ipairs(cases) do
assert.is_true(includes(case.this, case.other) == case.result, "case: " .. i .. " failed" )
assert.is_true(mime_type.includes(case.this, case.other) == case.result, "case: " .. i .. " failed" )
end
end)

it("throws an error for invalid arguments", function()
assert.has_error(function() mime_type.includes(nil, {}) end, "this must be a table")
assert.has_error(function() mime_type.includes({}, nil) end, "other must be a table")
end)
end)

end)

0 comments on commit 1d09858

Please sign in to comment.