diff --git a/kong/tools/mime_type.lua b/kong/tools/mime_type.lua index d9f730a401cc..e4772919e603 100644 --- a/kong/tools/mime_type.lua +++ b/kong/tools/mime_type.lua @@ -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 = "*" @@ -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 diff --git a/spec/01-unit/26-mime-type_spec.lua b/spec/01-unit/26-mime-type_spec.lua index 90df31ab8765..6b4d3cbb6a29 100644 --- a/spec/01-unit/26-mime-type_spec.lua +++ b/spec/01-unit/26-mime-type_spec.lua @@ -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() @@ -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 @@ -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)