Skip to content

Commit

Permalink
fix(admin) handle empty 'url' sugar attribute in /services
Browse files Browse the repository at this point in the history
Fix #3448
From #3452
  • Loading branch information
bungle authored and thibaultcha committed May 11, 2018
1 parent 28bf448 commit fd4db25
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
30 changes: 19 additions & 11 deletions kong/api/api_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,26 @@ end

function _M.resolve_url_params(self)
local sugar_url = self.args.post.url
if sugar_url then
local parsed_url = url.parse(sugar_url)
self.args.post.protocol = parsed_url.scheme
self.args.post.host = parsed_url.host
self.args.post.port = tonumber(parsed_url.port) or
parsed_url.port or
(parsed_url.scheme == "http" and 80) or
(parsed_url.scheme == "https" and 443) or
nil
self.args.post.path = parsed_url.path
self.args.post.url = nil

self.args.post.url = nil

if type(sugar_url) ~= "string" then
return
end

local parsed_url = url.parse(sugar_url)
if not parsed_url then
return
end

self.args.post.protocol = parsed_url.scheme
self.args.post.host = parsed_url.host
self.args.post.port = tonumber(parsed_url.port) or
parsed_url.port or
(parsed_url.scheme == "http" and 80) or
(parsed_url.scheme == "https" and 443) or
nil
self.args.post.path = parsed_url.path
end


Expand Down
24 changes: 24 additions & 0 deletions spec/02-integration/04-admin_api/21-services_routes_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,30 @@ for _, strategy in helpers.each_strategy() do
assert.equals(60000, json.read_timeout)
end
end)

it_content_types("client error with with empty url", function(content_type)
return function()
local res = client:post("/services", {
body = {
url = "",
},
headers = { ["Content-Type"] = content_type },
})
assert.res_status(400, res)
end
end)

it_content_types("client error with invalid url", function(content_type)
return function()
local res = client:post("/services", {
body = {
url = " ",
},
headers = { ["Content-Type"] = content_type },
})
assert.res_status(400, res)
end
end)
end)

describe("GET", function()
Expand Down

0 comments on commit fd4db25

Please sign in to comment.