Skip to content

Commit

Permalink
Merge branch 'next' of https://github.com/Mashape/kong into fix/reque…
Browse files Browse the repository at this point in the history
…st-size
  • Loading branch information
Tieske committed Jul 18, 2016
2 parents 3bfa718 + cd758d2 commit 0b51f89
Show file tree
Hide file tree
Showing 8 changed files with 300 additions and 20 deletions.
17 changes: 17 additions & 0 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
### Summary

SUMMARY_GOES_HERE

### Steps To Reproduce

1.
2.
3.
4.

### Additional Details & Logs

- Kong version (`$ kong version`)
- Kong configuration (registered APIs/Plugins & configuration file)
- Kong error logs (`<KONG_PREFIX>/logs/error.log`)
- Operating System
15 changes: 15 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
**Note: new features are to be opened against next, hotfixes against master.**

### Summary

SUMMARY_GOES_HERE

### Full changelog

* [Implement ...]
* [Add related tests]
* ...

### Issues resolved

Fix #XXX
36 changes: 24 additions & 12 deletions kong/plugins/request-transformer/schema.lua
Original file line number Diff line number Diff line change
@@ -1,42 +1,54 @@
local find = string.find
-- entries must have colons to set the key and value apart
local function check_for_value(value)
for i, entry in ipairs(value) do
local ok = find(entry, ":")
if not ok then
return false, "key '"..entry.."' has no value"
end
end
return true
end

return {
fields = {
remove = {
type = "table",
schema = {
fields = {
body = {type = "array", default = {}},
headers = {type = "array", default = {}},
querystring = {type = "array", default = {}}
body = {type = "array", default = {}}, -- does not need colons
headers = {type = "array", default = {}}, -- does not need colons
querystring = {type = "array", default = {}} -- does not need colons
}
}
},
replace = {
type = "table",
schema = {
fields = {
body = {type = "array", default = {}},
headers = {type = "array", default = {}},
querystring = {type = "array", default = {}}
body = {type = "array", default = {}, func = check_for_value},
headers = {type = "array", default = {}, func = check_for_value},
querystring = {type = "array", default = {}, func = check_for_value}
}
}
},
add = {
type = "table",
schema = {
fields = {
body = {type = "array", default = {}},
headers = {type = "array", default = {}},
querystring = {type = "array", default = {}}
body = {type = "array", default = {}, func = check_for_value},
headers = {type = "array", default = {}, func = check_for_value},
querystring = {type = "array", default = {}, func = check_for_value}
}
}
},
append = {
type = "table",
schema = {
fields = {
body = {type = "array", default = {}},
headers = {type = "array", default = {}},
querystring = {type = "array", default = {}}
body = {type = "array", default = {}, func = check_for_value},
headers = {type = "array", default = {}, func = check_for_value},
querystring = {type = "array", default = {}, func = check_for_value}
}
}
}
Expand Down
28 changes: 20 additions & 8 deletions kong/plugins/response-transformer/schema.lua
Original file line number Diff line number Diff line change
@@ -1,39 +1,51 @@
local find = string.find
-- entries must have colons to set the key and value apart
local function check_for_value(value)
for i, entry in ipairs(value) do
local ok = find(entry, ":")
if not ok then
return false, "key '"..entry.."' has no value"
end
end
return true
end

return {
fields = {
-- add: Add a value (to response headers or response JSON body) only if the key does not already exist.
remove = {
type = "table",
schema = {
fields = {
json = {type = "array", default = {}},
headers = {type = "array", default = {}}
json = {type = "array", default = {}}, -- does not need colons
headers = {type = "array", default = {}} -- does not need colons
}
}
},
replace = {
type = "table",
schema = {
fields = {
json = {type = "array", default = {}},
headers = {type = "array", default = {}}
json = {type = "array", default = {}, func = check_for_value},
headers = {type = "array", default = {}, func = check_for_value}
}
}
},
add = {
type = "table",
schema = {
fields = {
json = {type = "array", default = {}},
headers = {type = "array", default = {}}
json = {type = "array", default = {}, func = check_for_value},
headers = {type = "array", default = {}, func = check_for_value}
}
}
},
append = {
type = "table",
schema = {
fields = {
json = {type = "array", default = {}},
headers = {type = "array", default = {}}
json = {type = "array", default = {}, func = check_for_value},
headers = {type = "array", default = {}, func = check_for_value}
}
}
}
Expand Down
113 changes: 113 additions & 0 deletions spec/03-plugins/request-transformer/02-api_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
local helpers = require "spec.helpers"
local cjson = require "cjson"

describe("Plugin: request-transformer (API)", function()
local admin_client
setup(function()
helpers.kill_all()
helpers.prepare_prefix()

assert(helpers.start_kong())
admin_client = helpers.admin_client()
end)
teardown(function()
if admin_client then
admin_client:close()
end
helpers.stop_kong()
end)

describe("POST", function()
setup(function()
assert(helpers.dao.apis:insert {
name = "test",
request_host = "test1.com",
upstream_url = "http://mockbin.com"
})
end)

describe("validate config parameters", function()
it("remove succeeds without colons", function()
local res = assert(admin_client:send {
method = "POST",
path = "/apis/test/plugins/",
body = {
name = "request-transformer",
config = {
remove = {
headers = "just_a_key",
body = "just_a_key",
querystring = "just_a_key",
},
},
},
headers = {
["Content-Type"] = "application/json",
},
})
assert.response(res).has.status(201)
local body = assert.response(res).has.jsonbody()
assert.equals("just_a_key", body.config.remove.headers[1])
assert.equals("just_a_key", body.config.remove.body[1])
assert.equals("just_a_key", body.config.remove.querystring[1])
end)
it("add fails with missing colons for key/value separation", function()
local res = assert(admin_client:send {
method = "POST",
path = "/apis/test/plugins/",
body = {
name = "request-transformer",
config = {
add = {
headers = "just_a_key",
},
},
},
headers = {
["Content-Type"] = "application/json",
},
})
local body = assert.response(res).has.status(400)
assert.equals([[{"config.add.headers":"key 'just_a_key' has no value"}]], body)
end)
it("replace fails with missing colons for key/value separation", function()
local res = assert(admin_client:send {
method = "POST",
path = "/apis/test/plugins/",
body = {
name = "request-transformer",
config = {
replace = {
headers = "just_a_key",
},
},
},
headers = {
["Content-Type"] = "application/json",
},
})
local body = assert.response(res).has.status(400)
assert.equals([[{"config.replace.headers":"key 'just_a_key' has no value"}]], body)
end)
it("append fails with missing colons for key/value separation", function()
local res = assert(admin_client:send {
method = "POST",
path = "/apis/test/plugins/",
body = {
name = "request-transformer",
config = {
append = {
headers = "just_a_key",
},
},
},
headers = {
["Content-Type"] = "application/json",
},
})
local body = assert.response(res).has.status(400)
assert.equals([[{"config.append.headers":"key 'just_a_key' has no value"}]], body)
end)
end)
end)
end)
111 changes: 111 additions & 0 deletions spec/03-plugins/response-transformer/03-api_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
local helpers = require "spec.helpers"
local cjson = require "cjson"

describe("Plugin: response-transformer (API)", function()
local admin_client
setup(function()
helpers.kill_all()
helpers.prepare_prefix()

assert(helpers.start_kong())
admin_client = helpers.admin_client()
end)
teardown(function()
if admin_client then
admin_client:close()
end
helpers.stop_kong()
end)

describe("POST", function()
setup(function()
assert(helpers.dao.apis:insert {
name = "test",
request_host = "test1.com",
upstream_url = "http://mockbin.com"
})
end)

describe("validate config parameters", function()
it("remove succeeds without colons", function()
local res = assert(admin_client:send {
method = "POST",
path = "/apis/test/plugins/",
body = {
name = "response-transformer",
config = {
remove = {
headers = "just_a_key",
json = "just_a_key",
},
},
},
headers = {
["Content-Type"] = "application/json",
},
})
assert.response(res).has.status(201)
local body = assert.response(res).has.jsonbody()
assert.equals("just_a_key", body.config.remove.headers[1])
assert.equals("just_a_key", body.config.remove.json[1])
end)
it("add fails with missing colons for key/value separation", function()
local res = assert(admin_client:send {
method = "POST",
path = "/apis/test/plugins/",
body = {
name = "response-transformer",
config = {
add = {
headers = "just_a_key",
},
},
},
headers = {
["Content-Type"] = "application/json",
},
})
local body = assert.response(res).has.status(400)
assert.equals([[{"config.add.headers":"key 'just_a_key' has no value"}]], body)
end)
it("replace fails with missing colons for key/value separation", function()
local res = assert(admin_client:send {
method = "POST",
path = "/apis/test/plugins/",
body = {
name = "response-transformer",
config = {
replace = {
headers = "just_a_key",
},
},
},
headers = {
["Content-Type"] = "application/json",
},
})
local body = assert.response(res).has.status(400)
assert.equals([[{"config.replace.headers":"key 'just_a_key' has no value"}]], body)
end)
it("append fails with missing colons for key/value separation", function()
local res = assert(admin_client:send {
method = "POST",
path = "/apis/test/plugins/",
body = {
name = "response-transformer",
config = {
append = {
headers = "just_a_key",
},
},
},
headers = {
["Content-Type"] = "application/json",
},
})
local body = assert.response(res).has.status(400)
assert.equals([[{"config.append.headers":"key 'just_a_key' has no value"}]], body)
end)
end)
end)
end)

0 comments on commit 0b51f89

Please sign in to comment.