Skip to content

Commit

Permalink
fix: fix code
Browse files Browse the repository at this point in the history
  • Loading branch information
oowl committed Dec 20, 2023
1 parent 1218075 commit 68504ca
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
2 changes: 1 addition & 1 deletion kong/pdk/private/checks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function checks.validate_header(name, value)
value = tostring(value)
else
error(fmt("invalid header value for %q: got %s, expected " ..
"string, number or boolean, table", name, tvalue), 3)
"table, string, number or boolean", name, tvalue), 3)
end
end
return value
Expand Down
3 changes: 0 additions & 3 deletions kong/plugins/response-transformer/header_transformer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@ function _M.transform_headers(conf, headers)
if headers[old_name] ~= nil and new_name then
local value = headers[old_name]
-- header value can be a table, in which case we take the first value
if type(value) == "table" then
value = value[1]
end
set_header(new_name, value)
clear_header(old_name)
end
Expand Down
43 changes: 39 additions & 4 deletions t/01-pdk/08-response/05-set_header.t
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ invalid header name "127001": got number, expected string
=== TEST 3: response.set_header() errors if value is not a string
=== TEST 3: response.set_header() errors if value is not a table contain array of string
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
Expand All @@ -89,8 +89,9 @@ invalid header name "127001": got number, expected string
local PDK = require "kong.pdk"
local pdk = PDK.new()
local set_header = { {} }
local ok, err = pcall(pdk.response.set_header, "foo", {})
local ok, err = pcall(pdk.response.set_header, "foo", set_header)
if not ok then
ngx.ctx.err = err
end
Expand All @@ -104,7 +105,7 @@ invalid header name "127001": got number, expected string
--- request
GET /t
--- response_body chop
invalid header value for "foo": got table, expected string, number or boolean
invalid header value in array "foo": got table, expected string
--- no_error_log
[error]
Expand Down Expand Up @@ -137,7 +138,7 @@ invalid header value for "foo": got table, expected string, number or boolean
--- request
GET /t
--- response_body chop
invalid header value for "foo": got nil, expected string, number or boolean
invalid header value for "foo": got nil, expected table, string, number or boolean
--- no_error_log
[error]
Expand Down Expand Up @@ -277,3 +278,37 @@ GET /t
Transfer-Encoding: chunked
--- error_log
manually setting Transfer-Encoding. Ignored.
=== TEST 8: response.set_header() with header table
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
content_by_lua_block {
}
header_filter_by_lua_block {
ngx.header.content_length = nil
local PDK = require "kong.pdk"
local pdk = PDK.new()
local set_header = {"a", "b"}
pdk.response.set_header("X-Foo", set_header)
}
body_filter_by_lua_block {
local new_headers = ngx.resp.get_headers()
local cjson = require("cjson")
ngx.arg[1] = "X-Foo: {" .. new_headers["X-Foo"][1] .. "," .. new_headers["X-Foo"][2] .. "}"
ngx.arg[2] = true
}
}
--- request
GET /t
--- response_body chop
X-Foo: {a,b}
--- no_error_log
[error]

0 comments on commit 68504ca

Please sign in to comment.