Skip to content

Commit

Permalink
fix(pdk) response.exit to error when using table body with content-type
Browse files Browse the repository at this point in the history
Also logs warning when using table body that is not the common:
```
{
  message = "<string>"
}
```

and then basically ignores that body for better compatibility with
current Kong core and plugins that are not necessarily build for grpc
in mind.
  • Loading branch information
bungle authored and hishamhm committed Aug 12, 2019
1 parent 7876bfc commit 4e5544f
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 3 deletions.
10 changes: 8 additions & 2 deletions kong/pdk/response.lua
Original file line number Diff line number Diff line change
Expand Up @@ -532,10 +532,16 @@ local function new(self, major_version)
local json
if type(body) == "table" then
if is_grpc then
if type(body.message) == "string" then
if is_grpc_output then
error("table body encoding with gRPC is not supported", 2)

elseif type(body.message) == "string" then
body = body.message

else
body = nil -- grpc table encoding not supported currently
self.log.warn("body was removed because table body encoding with " ..
"gRPC is not supported")
body = nil
end

else
Expand Down
141 changes: 140 additions & 1 deletion t/01-pdk/08-response/11-exit.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use warnings FATAL => 'all';
use Test::Nginx::Socket::Lua;
use t::Util;

plan tests => repeat_each() * (blocks() * 4) + 13;
plan tests => repeat_each() * (blocks() * 4) + 9;

run_tests();

Expand Down Expand Up @@ -915,3 +915,142 @@ grpc-message: Unauthenticated
--- response_body chop
--- no_error_log
[error]
=== TEST 34: response.exit() errors with grpc using table body with content-type specified (explicit)
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
default_type 'text/test';
access_by_lua_block {
local PDK = require "kong.pdk"
local pdk = PDK.new()
pdk.response.exit(401, {}, {
["Content-Type"] = "application/grpc"
})
}
}
--- request
GET /t
--- error_code: 500
--- error_log: table body encoding with gRPC is not supported
=== TEST 35: response.exit() errors with grpc using table body with content-type specified (implicit)
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
default_type 'text/test';
access_by_lua_block {
local PDK = require "kong.pdk"
local pdk = PDK.new()
pdk.response.set_header("Content-Type", "application/grpc")
pdk.response.exit(401, {})
}
}
--- request
GET /t
--- error_code: 500
--- error_log: table body encoding with gRPC is not supported
=== TEST 36: response.exit() errors with grpc using special table body with content-type specified (explicit)
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
default_type 'text/test';
access_by_lua_block {
local PDK = require "kong.pdk"
local pdk = PDK.new()
pdk.response.exit(401, { message = "I am special" }, {
["Content-Type"] = "application/grpc"
})
}
}
--- request
GET /t
--- error_code: 500
--- error_log: table body encoding with gRPC is not supported
=== TEST 37: response.exit() errors with grpc using special table body with content-type specified (implicit)
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
default_type 'text/test';
access_by_lua_block {
local PDK = require "kong.pdk"
local pdk = PDK.new()
pdk.response.set_header("Content-Type", "application/grpc")
pdk.response.exit(401, { message = "I am special" })
}
}
--- request
GET /t
--- error_code: 500
--- error_log: table body encoding with gRPC is not supported
=== TEST 38: response.exit() logs warning with grpc using table body without content-type specified
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
default_type 'text/test';
access_by_lua_block {
ngx.req.http_version = function() return "2" end
local PDK = require "kong.pdk"
local pdk = PDK.new()
pdk.response.exit(401, {})
}
}
--- request
GET /t
--- more_headers
Content-Type: application/grpc
--- response_headers_like
Content-Length: 0
grpc-status: 16
grpc-message: Unauthenticated
--- response_body chop
--- error_code: 401
--- error_log: body was removed because table body encoding with gRPC is not supported
=== TEST 39: response.exit() does not log warning with grpc using special table body without content-type specified
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
default_type 'text/test';
access_by_lua_block {
ngx.req.http_version = function() return "2" end
local PDK = require "kong.pdk"
local pdk = PDK.new()
pdk.response.exit(401, { message = "Hello" })
}
}
--- request
GET /t
--- more_headers
Content-Type: application/grpc
--- response_headers_like
Content-Length: 0
grpc-status: 16
grpc-message: Hello
--- response_body chop
--- error_code: 401
--- no_error_log
[error]

0 comments on commit 4e5544f

Please sign in to comment.