Skip to content

Commit

Permalink
feat(pdk) implement kong.response.get_path_with_query()
Browse files Browse the repository at this point in the history
When the path + querystring is needed, this prevents having to manually
check wether the querystring is present and concatenating the path + "?"
+ the query string every time.

From #3842

Signed-off-by: Thibault Charbonnier <[email protected]>
  • Loading branch information
kikito authored and thibaultcha committed Oct 17, 2018
1 parent 5f6a1d3 commit 68af538
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
17 changes: 17 additions & 0 deletions kong/pdk/request.lua
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,23 @@ local function new(self)
end


---
-- Returns the path, including the querystring if any. No
-- transformations/normalizations are done.
--
-- @function kong.request.get_path_with_query()
-- @phases rewrite, access, header_filter, body_filter, log, admin_api
-- @treturn string the path with the querystring
-- @usage
-- -- Given a request to https://example.com:1234/v1/movies?movie=foo
--
-- kong.request.get_raw_path_and_query() -- "/v1/movies?movie=foo"
function _REQUEST.get_path_with_query()
check_phase(PHASES.request)
return ngx.var.request_uri
end


---
-- Returns the query component of the request's URL. It is not normalized in
-- any way (not even URL-decoding of special characters) and does not
Expand Down
11 changes: 11 additions & 0 deletions t/01-pdk/04-request/00-phase_checks.t
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ qq{
body_filter = true,
log = true,
admin_api = true,
}, {
method = "get_path_with_query",
args = {},
init_worker = false,
certificate = "pending",
rewrite = true,
access = true,
header_filter = true,
body_filter = true,
log = true,
admin_api = true,
}, {
method = "get_raw_query",
args = {},
Expand Down
52 changes: 52 additions & 0 deletions t/01-pdk/04-request/17-get_path_with_query.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use strict;
use warnings FATAL => 'all';
use Test::Nginx::Socket::Lua;
use t::Util;

plan tests => repeat_each() * (blocks() * 3);

run_tests();

__DATA__
=== TEST 1: request.get_path_with_query() returns the path if no querystring
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
access_by_lua_block {
local PDK = require "kong.pdk"
local pdk = PDK.new()
local path_and_querystring = pdk.request.get_path_with_query()
ngx.say("path_and_querystring=", path_and_querystring)
}
}
--- request
GET /t
--- response_body
path_and_querystring=/t
--- no_error_log
[error]
=== TEST 2: request.get_path_with_query() returns path + ? + querystring
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
access_by_lua_block {
local PDK = require "kong.pdk"
local pdk = PDK.new()
local path_and_querystring = pdk.request.get_path_with_query()
ngx.say("path_and_querystring=", path_and_querystring)
}
}
--- request
GET /t?foo=1&bar=2
--- response_body
path_and_querystring=/t?foo=1&bar=2
--- no_error_log
[error]

0 comments on commit 68af538

Please sign in to comment.