Skip to content

Commit

Permalink
feat(pdk): add kong.service.request.clear_query_arg
Browse files Browse the repository at this point in the history
Signed-off-by: Aapo Talvensaari <aapo.talvensaari@gmail.com>
  • Loading branch information
bungle committed Sep 4, 2024
1 parent 3a71c40 commit 0cf4569
Show file tree
Hide file tree
Showing 3 changed files with 264 additions and 0 deletions.
3 changes: 3 additions & 0 deletions changelog/unreleased/kong/feat-pdk-clear-query-arg.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: Added `kong.service.request.clear_query_arg(name)` to PDK.
type: feature
scope: PDK
27 changes: 27 additions & 0 deletions kong/pdk/service/request.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ local validate_header = checks.validate_header
local validate_headers = checks.validate_headers
local check_phase = phase_checker.check
local escape = require("kong.tools.uri").escape
local search_remove = require("resty.ada.search").remove


local PHASES = phase_checker.phases
Expand Down Expand Up @@ -272,11 +273,37 @@ local function new(self)
end


---
-- Removes all occurrences of the specified query string argument
-- from the request to the Service. The order of query string
-- arguments is retained.
--
-- @function kong.service.request.clear_query_arg
-- @phases `rewrite`, `access`
-- @tparam string name
-- @return Nothing; throws an error on invalid inputs.
-- @usage
-- kong.service.request.clear_query_arg("foo")
request.clear_query_arg = function(name)
check_phase(access_and_rewrite)

if type(name) ~= "string" then
error("query argument name must be a string", 2)
end

local args = ngx_var.args
if args and args ~= "" then
ngx_var.args = search_remove(args, name)
end
end


local set_authority
if ngx.config.subsystem ~= "stream" then
set_authority = require("resty.kong.grpc").set_authority
end


---
-- Sets a header in the request to the Service with the given value. Any existing header
-- with the same name will be overridden.
Expand Down
234 changes: 234 additions & 0 deletions t/01-pdk/06-service-request/14-clear_query_arg.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
use strict;
use warnings FATAL => 'all';
use Test::Nginx::Socket::Lua;
do "./t/Util.pm";

$ENV{TEST_NGINX_NXSOCK} ||= html_dir();

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

run_tests();

__DATA__
=== TEST 1: service.request.clear_query_arg() errors if arguments are not given
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
content_by_lua_block {
local PDK = require "kong.pdk"
local pdk = PDK.new()
local pok, err = pcall(pdk.service.request.clear_query_arg)
ngx.say(err)
}
}
--- request
GET /t
--- response_body
query argument name must be a string
--- no_error_log
[error]
=== TEST 2: service.request.clear_query_arg() errors if query argument name is not a string
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
content_by_lua_block {
local PDK = require "kong.pdk"
local pdk = PDK.new()
local pok, err = pcall(pdk.service.request.clear_query_arg, 127001)
ngx.say(err)
}
}
--- request
GET /t
--- response_body
query argument name must be a string
--- no_error_log
[error]
=== TEST 3: service.request.clear_query_arg() clears a given query argument
--- http_config eval
qq{
$t::Util::HttpConfig
server {
listen unix:$ENV{TEST_NGINX_NXSOCK}/nginx.sock;
location /t {
content_by_lua_block {
ngx.say("foo: {" .. tostring(ngx.req.get_uri_args()["foo"]) .. "}")
}
}
}
}
--- config
location = /t {
access_by_lua_block {
local PDK = require "kong.pdk"
local pdk = PDK.new()
pdk.service.request.clear_query_arg("foo")
}
proxy_pass http://unix:/$TEST_NGINX_NXSOCK/nginx.sock;
}
--- request
GET /t?foo=bar
--- response_body
foo: {nil}
--- no_error_log
[error]
=== TEST 4: service.request.clear_query_arg() clears multiple given query arguments
--- http_config eval
qq{
$t::Util::HttpConfig
server {
listen unix:$ENV{TEST_NGINX_NXSOCK}/nginx.sock;
location /t {
content_by_lua_block {
ngx.say("foo: {" .. tostring(ngx.req.get_uri_args()["foo"]) .. "}")
}
}
}
}
--- config
location = /t {
access_by_lua_block {
local PDK = require "kong.pdk"
local pdk = PDK.new()
pdk.service.request.clear_query_arg("foo")
}
proxy_pass http://unix:/$TEST_NGINX_NXSOCK/nginx.sock;
}
--- request
GET /t?foo=bar&foo=baz
--- response_body
foo: {nil}
--- no_error_log
[error]
=== TEST 5: service.request.clear_query_arg() clears query arguments set via set_query
--- http_config eval
qq{
$t::Util::HttpConfig
server {
listen unix:$ENV{TEST_NGINX_NXSOCK}/nginx.sock;
location /t {
content_by_lua_block {
ngx.say("foo: {" .. tostring(ngx.req.get_uri_args()["foo"]) .. "}")
}
}
}
}
--- config
location = /t {
access_by_lua_block {
local PDK = require "kong.pdk"
local pdk = PDK.new()
pdk.service.request.set_query({ foo = "bar" })
pdk.service.request.clear_query_arg("foo")
}
proxy_pass http://unix:/$TEST_NGINX_NXSOCK/nginx.sock;
}
--- request
GET /t
--- response_body
foo: {nil}
--- no_error_log
[error]
=== TEST 6: service.request.clear_query_arg() clears query arguments set via set_raw_query
--- http_config eval
qq{
$t::Util::HttpConfig
server {
listen unix:$ENV{TEST_NGINX_NXSOCK}/nginx.sock;
location /t {
content_by_lua_block {
ngx.say("foo: {" .. tostring(ngx.req.get_uri_args()["foo"]) .. "}")
}
}
}
}
--- config
location = /t {
access_by_lua_block {
local PDK = require "kong.pdk"
local pdk = PDK.new()
pdk.service.request.set_raw_query("foo=bar")
pdk.service.request.clear_query_arg("foo")
}
proxy_pass http://unix:/$TEST_NGINX_NXSOCK/nginx.sock;
}
--- request
GET /t
--- response_body
foo: {nil}
--- no_error_log
[error]
=== TEST 7: service.request.clear_query_arg() retains the order of query arguments
--- http_config eval
qq{
$t::Util::HttpConfig
server {
listen unix:$ENV{TEST_NGINX_NXSOCK}/nginx.sock;
location /t {
content_by_lua_block {
ngx.say("query: " .. tostring(ngx.var.args))
}
}
}
}
--- config
location = /t {
access_by_lua_block {
local PDK = require "kong.pdk"
local pdk = PDK.new()
pdk.service.request.clear_query_arg("a")
}
proxy_pass http://unix:/$TEST_NGINX_NXSOCK/nginx.sock;
}
--- request
GET /t?a=0&d=1&a=2&c=3&a=4&b=5&a=6&d=7&a=8
--- response_body
query: d=1&c=3&b=5&d=7
--- no_error_log
[error]

0 comments on commit 0cf4569

Please sign in to comment.