Skip to content

Commit

Permalink
[resty.url] allow parsed url to be serialized back
Browse files Browse the repository at this point in the history
  • Loading branch information
mikz committed Jul 4, 2017
1 parent 4c14d82 commit b0c9828
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 3 additions & 2 deletions apicast/src/resty/url.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local tostring = tostring
local re_match = ngx.re.match
local concat = table.concat
local tonumber = tonumber
local setmetatable = setmetatable

local _M = {
_VERSION = '0.1'
Expand Down Expand Up @@ -51,14 +52,14 @@ function _M.parse(url, protocol)
end

-- https://tools.ietf.org/html/rfc3986#section-3
return {
return setmetatable({
scheme = parts[1] or nil,
user = parts[2] or nil,
password = parts[3] or nil,
host = parts[4] or nil,
port = tonumber(parts[5]),
path = parts[6] or nil
}
}, { __tostring = function() return url end })
end

function _M.join(...)
Expand Down
6 changes: 6 additions & 0 deletions spec/resty/url_spec.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local url = require 'resty.url'
local tostring = tostring

describe('resty.url', function()

Expand Down Expand Up @@ -72,6 +73,11 @@ describe('resty.url', function()
it('works with redis DSN', function()
assert.same({ scheme = 'redis', user = 'user', password = 'pass', host = 'localhost', port = 6379, path = '/42' }, parse('redis://user:pass@localhost:6379/42', 'redis'))
end)

it('serializes back', function()
local uri = 'https://user:password@example.com:1234/path?query'
assert.equal(uri, tostring(parse(uri)))
end)
end)

describe('.join', function()
Expand Down

0 comments on commit b0c9828

Please sign in to comment.