From 4a5827d8a1316a22aae82e33fba71e256074378d Mon Sep 17 00:00:00 2001 From: Vinicius Mignot Date: Tue, 25 Jun 2019 16:04:47 -0300 Subject: [PATCH] fix(request-transformer) header transf called before body (#4) Header transform must be executed before body transform, as the content-type must be correctly set before changing body. This change fixes issue #1 --- kong/plugins/request-transformer/access.lua | 9 ++++-- spec/02-access_spec.lua | 33 +++++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/kong/plugins/request-transformer/access.lua b/kong/plugins/request-transformer/access.lua index 61e7aa5e514..b0eab3294d1 100644 --- a/kong/plugins/request-transformer/access.lua +++ b/kong/plugins/request-transformer/access.lua @@ -260,8 +260,11 @@ local function transform_json_body(conf, body, content_length) local removed, renamed, replaced, added, appended = false, false, false, false, false local content_length = (body and #body) or 0 local parameters = parse_json(body) - if parameters == nil and content_length > 0 then - return false, nil + if parameters == nil then + if content_length > 0 then + return false, nil + end + parameters = {} end if content_length > 0 and #conf.remove.body > 0 then @@ -484,8 +487,8 @@ function _M.execute(conf) clear_environment() transform_uri(conf) transform_method(conf) - transform_body(conf) transform_headers(conf) + transform_body(conf) transform_querystrings(conf) end diff --git a/spec/02-access_spec.lua b/spec/02-access_spec.lua index 1bedeaec885..0aa6bb71bc0 100644 --- a/spec/02-access_spec.lua +++ b/spec/02-access_spec.lua @@ -86,6 +86,9 @@ describe("Plugin: request-transformer(access) [#" .. strategy .. "]", function() paths = { "/requests/user1/(?\\w+)/user2/(?\\S+)" }, strip_path = false }) + local route20 = bp.routes:insert({ + hosts = { "test20.com" } + }) bp.plugins:insert { route = { id = route1.id }, @@ -307,6 +310,20 @@ describe("Plugin: request-transformer(access) [#" .. strategy .. "]", function() } } + bp.plugins:insert { + route = { id = route20.id }, + name = "request-transformer", + config = { + http_method = "POST", + add = { + headers = { + "Content-Type:application/json" + }, + body = { "body:somecontent" } + }, + } + } + assert(helpers.start_kong({ database = strategy, plugins = "bundled, request-transformer", @@ -359,6 +376,22 @@ describe("Plugin: request-transformer(access) [#" .. strategy .. "]", function() assert.equal("world", json.uri_args.hello) assert.equal("marco", json.uri_args.name) end) + it("changes the HTTP method from GET to POST and adds JSON body", function() + local r = assert(client:send { + method = "GET", + path = "/request", + headers = { + host = "test20.com", + } + }) + assert.response(r).has.status(200) + local json = assert.response(r).has.jsonbody() + assert.request(r).has.jsonbody() + assert.equal("POST", json.vars.request_method) + assert.is_nil(json.post_data.error) + local header_content_type = assert.request(r).has.header("Content-Type") + assert.equals("application/json", header_content_type) + end) end) describe("remove", function() it("specified header", function()