Skip to content

Commit

Permalink
feat(request-transformer) change HTTP method of upstream request (#1635)
Browse files Browse the repository at this point in the history
  • Loading branch information
subnetmarco authored Oct 28, 2016
1 parent 5bf8833 commit f6b4801
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
27 changes: 27 additions & 0 deletions kong/plugins/request-transformer/access.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ local req_read_body = ngx.req.read_body
local req_set_body_data = ngx.req.set_body_data
local req_get_body_data = ngx.req.get_body_data
local req_clear_header = ngx.req.clear_header
local req_set_method = ngx.req.set_method
local encode_args = ngx.encode_args
local ngx_decode_args = ngx.decode_args
local type = type
Expand Down Expand Up @@ -303,7 +304,33 @@ local function transform_body(conf)
end
end

local function transform_method(conf)
if conf.http_method then
req_set_method(ngx["HTTP_"..conf.http_method:upper()])
if conf.http_method == "GET" or conf.http_method == "HEAD" or conf.http_method == "TRACE" then
local content_type_value = req_get_headers()[CONTENT_TYPE]
local content_type = get_content_type(content_type_value)
if content_type == ENCODED then
-- Also put the body into querystring

-- Read the body
req_read_body()
local body = req_get_body_data()
local parameters = decode_args(body)

-- Append to querystring
local querystring = req_get_uri_args()
for name, value in pairs(parameters) do
querystring[name] = value
end
req_set_uri_args(querystring)
end
end
end
end

function _M.execute(conf)
transform_method(conf)
transform_body(conf)
transform_headers(conf)
transform_querystrings(conf)
Expand Down
11 changes: 11 additions & 0 deletions kong/plugins/request-transformer/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,19 @@ local function check_for_value(value)
return true
end

local function check_method(value)
if not value then return true end
local method = value:upper()
local ngx_method = ngx["HTTP_"..method]
if not ngx_method then
return false, method.." is not supported"
end
return true
end

return {
fields = {
http_method = {type = "string", func = check_method},
remove = {
type = "table",
schema = {
Expand Down
16 changes: 16 additions & 0 deletions spec/03-plugins/13-request-transformer/01-schema_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
local schemas = require "kong.dao.schemas_validation"
local request_transformer_schema = require "kong.plugins.request-transformer.schema"
local validate_entity = schemas.validate_entity

describe("Plugin: request-transformer (schema)", function()
it("validates http_method", function()
local ok, err = validate_entity({http_method = "GET"}, request_transformer_schema)
assert.is_nil(err)
assert.True(ok)
end)
it("errors invalid http_method", function()
local ok, err = validate_entity({http_method = "HELLO"}, request_transformer_schema)
assert.equal("HELLO is not supported", err.http_method)
assert.False(ok)
end)
end)
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ describe("Plugin: request-transformer (access)", function()
local api4 = assert(helpers.dao.apis:insert {request_host = "test4.com", upstream_url = "http://mockbin.com"})
local api5 = assert(helpers.dao.apis:insert {request_host = "test5.com", upstream_url = "http://mockbin.com"})
local api6 = assert(helpers.dao.apis:insert {request_host = "test6.com", upstream_url = "http://mockbin.com"})
local api7 = assert(helpers.dao.apis:insert {request_host = "test7.com", upstream_url = "http://mockbin.com"})
local api8 = assert(helpers.dao.apis:insert {request_host = "test8.com", upstream_url = "http://mockbin.com"})

assert(helpers.dao.plugins:insert {
api_id = api1.id,
Expand Down Expand Up @@ -89,6 +91,20 @@ describe("Plugin: request-transformer (access)", function()
}
}
})
assert(helpers.dao.plugins:insert {
api_id = api7.id,
name = "request-transformer",
config = {
http_method = "POST"
}
})
assert(helpers.dao.plugins:insert {
api_id = api8.id,
name = "request-transformer",
config = {
http_method = "GET"
}
})
end)
teardown(function()
helpers.stop_kong()
Expand All @@ -101,6 +117,41 @@ describe("Plugin: request-transformer (access)", function()
if client then client:close() end
end)

describe("http method", function()
it("changes the HTTP method from GET to POST", function()
local r = assert(client:send {
method = "GET",
path = "/request?hello=world&name=marco",
headers = {
host = "test7.com"
}
})
assert.response(r).has.status(200)
local json = assert.response(r).has.jsonbody()
assert.equal("POST", json.method)
assert.equal("world", json.queryString.hello)
assert.equal("marco", json.queryString.name)
end)
it("changes the HTTP method from POST to GET", function()
local r = assert(client:send {
method = "POST",
path = "/request?hello=world",
body = {
name = "marco"
},
headers = {
["Content-Type"] = "application/x-www-form-urlencoded",
host = "test8.com"
}
})
assert.response(r).has.status(200)
local json = assert.response(r).has.jsonbody()
assert.equal("GET", json.method)
assert.equal("marco", json.postData.params.name)
assert.equal("world", json.queryString.hello)
assert.equal("marco", json.queryString.name)
end)
end)
describe("remove", function()
it("specified header", function()
local r = assert(client:send {
Expand Down

0 comments on commit f6b4801

Please sign in to comment.