Skip to content

Commit

Permalink
fix(pdk) make kong.request.get_body() to return nil, err on simple js…
Browse files Browse the repository at this point in the history
…on values
  • Loading branch information
bungle authored and thibaultcha committed Dec 7, 2018
1 parent 55bf5dc commit 3b550b5
Show file tree
Hide file tree
Showing 2 changed files with 231 additions and 11 deletions.
2 changes: 1 addition & 1 deletion kong/pdk/request.lua
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ local function new(self)
cjson.decode_array_with_array_mt(true)
local json = cjson.decode(body)
cjson.decode_array_with_array_mt(false)
if not json then
if type(json) ~= "table" then
return nil, "invalid json body", CONTENT_TYPE_JSON
end

Expand Down
240 changes: 230 additions & 10 deletions t/01-pdk/04-request/16-get_body.t
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,227 @@ error: invalid json body
=== TEST 7: request.get_body() content type value is case-insensitive
=== TEST 7: request.get_body() returns error with null json body
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
access_by_lua_block {
local PDK = require "kong.pdk"
local pdk = PDK.new()
local json, err = pdk.request.get_body()
ngx.say("type: ", type(json))
ngx.say("error: ", err)
}
}
--- request
POST /t
null
--- more_headers
Content-Type: application/json
--- response_body
type: nil
error: invalid json body
--- no_error_log
[error]
=== TEST 8: request.get_body() returns error with string json body
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
access_by_lua_block {
local PDK = require "kong.pdk"
local pdk = PDK.new()
local json, err = pdk.request.get_body()
ngx.say("type: ", type(json))
ngx.say("error: ", err)
}
}
--- request
POST /t
"test"
--- more_headers
Content-Type: application/json
--- response_body
type: nil
error: invalid json body
--- no_error_log
[error]
=== TEST 9: request.get_body() returns error with number json body
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
access_by_lua_block {
local PDK = require "kong.pdk"
local pdk = PDK.new()
local json, err = pdk.request.get_body()
ngx.say("type: ", type(json))
ngx.say("error: ", err)
}
}
--- request
POST /t
123
--- more_headers
Content-Type: application/json
--- response_body
type: nil
error: invalid json body
--- no_error_log
[error]
=== TEST 10: request.get_body() returns error with number (float) json body
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
access_by_lua_block {
local PDK = require "kong.pdk"
local pdk = PDK.new()
local json, err = pdk.request.get_body()
ngx.say("type: ", type(json))
ngx.say("error: ", err)
}
}
--- request
POST /t
123.23
--- more_headers
Content-Type: application/json
--- response_body
type: nil
error: invalid json body
--- no_error_log
[error]
=== TEST 11: request.get_body() returns error with boolean true json body
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
access_by_lua_block {
local PDK = require "kong.pdk"
local pdk = PDK.new()
local json, err = pdk.request.get_body()
ngx.say("type: ", type(json))
ngx.say("error: ", err)
}
}
--- request
POST /t
true
--- more_headers
Content-Type: application/json
--- response_body
type: nil
error: invalid json body
--- no_error_log
[error]
=== TEST 12: request.get_body() returns error with boolean false json body
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
access_by_lua_block {
local PDK = require "kong.pdk"
local pdk = PDK.new()
local json, err = pdk.request.get_body()
ngx.say("type: ", type(json))
ngx.say("error: ", err)
}
}
--- request
POST /t
false
--- more_headers
Content-Type: application/json
--- response_body
type: nil
error: invalid json body
--- no_error_log
[error]
=== TEST 13: request.get_body() returns empty object json body as table without metatable
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
access_by_lua_block {
local PDK = require "kong.pdk"
local pdk = PDK.new()
local json, err = pdk.request.get_body()
ngx.say("type: ", type(json))
ngx.say("meta: ", type(getmetatable(json)))
ngx.say("error: ", type(err))
}
}
--- request
POST /t
{}
--- more_headers
Content-Type: application/json
--- response_body
type: table
meta: nil
error: nil
--- no_error_log
[error]
=== TEST 14: request.get_body() returns empty array json body as table with metatable
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
access_by_lua_block {
local PDK = require "kong.pdk"
local pdk = PDK.new()
local json, err = pdk.request.get_body()
ngx.say("type: ", type(json))
ngx.say("meta: ", getmetatable(json) == require "cjson".array_mt and "correct" or "incorrect")
ngx.say("error: ", type(err))
}
}
--- request
POST /t
[]
--- more_headers
Content-Type: application/json
--- response_body
type: table
meta: correct
error: nil
--- no_error_log
[error]
=== TEST 15: request.get_body() content type value is case-insensitive
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
Expand Down Expand Up @@ -203,7 +423,7 @@ mime=application/x-www-form-urlencoded
=== TEST 8: request.get_body() with application/x-www-form-urlencoded returns request post arguments
=== TEST 16: request.get_body() with application/x-www-form-urlencoded returns request post arguments
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
Expand Down Expand Up @@ -231,7 +451,7 @@ Accept: application/json, text/html
=== TEST 9: request.get_body() with application/x-www-form-urlencoded returns empty table with header Content-Length: 0
=== TEST 17: request.get_body() with application/x-www-form-urlencoded returns empty table with header Content-Length: 0
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
Expand All @@ -255,7 +475,7 @@ next: nil
=== TEST 10: request.get_body() with application/x-www-form-urlencoded returns request post arguments case-sensitive
=== TEST 18: request.get_body() with application/x-www-form-urlencoded returns request post arguments case-sensitive
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
Expand Down Expand Up @@ -283,7 +503,7 @@ fOO: Too
=== TEST 11: request.get_body() with application/x-www-form-urlencoded fetches 100 post arguments by default
=== TEST 19: request.get_body() with application/x-www-form-urlencoded fetches 100 post arguments by default
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
Expand Down Expand Up @@ -320,7 +540,7 @@ number of query arguments fetched: 100
=== TEST 12: request.get_body() with application/x-www-form-urlencoded fetches max_args argument
=== TEST 20: request.get_body() with application/x-www-form-urlencoded fetches max_args argument
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
Expand Down Expand Up @@ -357,7 +577,7 @@ number of query arguments fetched: 60
=== TEST 13: request.get_body() with application/x-www-form-urlencoded raises error when trying to fetch with max_args invalid value
=== TEST 21: request.get_body() with application/x-www-form-urlencoded raises error when trying to fetch with max_args invalid value
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
Expand All @@ -379,7 +599,7 @@ error: max_args must be a number
=== TEST 14: request.get_body() with application/x-www-form-urlencoded raises error when trying to fetch with max_args < 1
=== TEST 22: request.get_body() with application/x-www-form-urlencoded raises error when trying to fetch with max_args < 1
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
Expand All @@ -401,7 +621,7 @@ error: max_args must be >= 1
=== TEST 15: request.get_body() with application/x-www-form-urlencoded raises error when trying to fetch with max_args > 1000
=== TEST 23: request.get_body() with application/x-www-form-urlencoded raises error when trying to fetch with max_args > 1000
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
Expand All @@ -423,7 +643,7 @@ error: max_args must be <= 1000
=== TEST 16: request.get_body() with application/x-www-form-urlencoded returns nil + error when the body is too big
=== TEST 24: request.get_body() with application/x-www-form-urlencoded returns nil + error when the body is too big
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
Expand Down

0 comments on commit 3b550b5

Please sign in to comment.