Skip to content

Commit

Permalink
Merge pull request #177 from stone-payments/release/v2.0.0
Browse files Browse the repository at this point in the history
release/v2.0.0
  • Loading branch information
jonathanlazaro1 committed Jan 15, 2024
2 parents 396726c + 7825210 commit 29753a0
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 7 deletions.
32 changes: 32 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,38 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

## [2.0.0](https://github.com/stone-payments/kong-plugin-template-transformer/compare/v1.4.1...v2.0.0) (2024-01-09)


### ⚠ BREAKING CHANGES

* Disables templating when response comes from kong itself

### Features

* Disables templating when response comes from kong itself ([a4d0a6a](https://github.com/stone-payments/kong-plugin-template-transformer/commit/a4d0a6aace5260fdfe7f31e5373d4c3b1ed613a4))


### Bug Fixes

* Replaces ngx error call with kong corresponding one ([091e0f0](https://github.com/stone-payments/kong-plugin-template-transformer/commit/091e0f02f81ae2b60c655535883e201363bd35e7))

## [2.0.0](https://github.com/stone-payments/kong-plugin-template-transformer/compare/v1.4.1...v2.0.0) (2024-01-09)


### ⚠ BREAKING CHANGES

* Disables templating when response comes from kong itself

### Features

* Disables templating when response comes from kong itself ([a4d0a6a](https://github.com/stone-payments/kong-plugin-template-transformer/commit/a4d0a6aace5260fdfe7f31e5373d4c3b1ed613a4))


### Bug Fixes

* Replaces ngx error call with kong corresponding one ([091e0f0](https://github.com/stone-payments/kong-plugin-template-transformer/commit/091e0f02f81ae2b60c655535883e201363bd35e7))

### [1.4.1](https://github.com/stone-payments/kong-plugin-template-transformer/compare/v1.4.0...v1.4.1) (2023-12-12)


Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
DEV_ROCKS = "lua-cjson 2.1.0.10-1" "kong 3.0.2" "luacov 0.12.0" "busted 2.0.0-1" "luacov-cobertura 0.2-1" "luacheck 0.20.0" "lua-resty-template 1.9-1"
PROJECT_FOLDER = template-transformer
LUA_PROJECT = kong-plugin-template-transformer
VERSION = 1.4.1-0
VERSION = 2.0.0-0

rockspec:
cp rockspec.template kong-plugin-template-transformer-$(VERSION).rockspec
Expand Down
4 changes: 2 additions & 2 deletions rockspec.template
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package = "kong-plugin-template-transformer"
version = "1.4.1-0"
version = "2.0.0-0"
source = {
url = "git://github.com/stone-payments/kong-plugin-template-transformer",
branch = "master",
tag = "v1.4.1",
tag = "v2.0.0",
}
description = {
summary = "A Kong plugin that enables template transforming",
Expand Down
9 changes: 7 additions & 2 deletions template-transformer/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ local gsub = string.gsub
local gmatch = string.gmatch
local TemplateTransformerHandler = {
PRIORITY = 801,
VERSION = "1.4.1"
VERSION = "2.0.0"
}

local template_transformer = require 'kong.plugins.kong-plugin-template-transformer.template_transformer'
Expand Down Expand Up @@ -137,6 +137,11 @@ end
function TemplateTransformerHandler:body_filter(config)
if config.response_template and config.response_template ~= "" then

if kong.response.get_source() ~= "service" then
kong.log.debug("Response is from kong itself or an error ocurred. Not applying any transformations.")
return
end

local cache_response = kong.ctx.shared.proxy_cache_hit
if cache_response ~= nil then
-- No need to do anything. Cache response is already transformed.
Expand Down Expand Up @@ -166,7 +171,7 @@ function TemplateTransformerHandler:body_filter(config)
if gmatch(content_type, "(application/json)")() then
body = read_json_body(raw_body)
if body == nil then
return ngx.ERROR
return kong.response.error(ngx.HTTP_INTERNAL_SERVER_ERROR)
end
local req_query_string = req_get_uri_args()
local router_matches = ngx.ctx.router_matches
Expand Down
26 changes: 24 additions & 2 deletions template-transformer/spec/handler_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ local kong = {
shared = {
proxy_cache_hit = nil
}
},
response = {
get_source = function () return "service" end,
error = spy.new(function () return "ERROR" end)
}
}

Expand Down Expand Up @@ -46,6 +50,7 @@ local ngx = {
custom_data = { important_stuff = 123 }
},
status = 200,
HTTP_INTERNAL_SERVER_ERROR = 500
}
_G.ngx = ngx
_G.kong = kong
Expand Down Expand Up @@ -199,6 +204,22 @@ describe("Test TemplateTransformerHandler body_filter", function()
assert.equal('{ "key" : "value" }', ngx.arg[1])
end)

it("should not run when response source is not service", function()
local config = {
response_template = "{ \"template\": 123 }"
}
local old_get_source = kong.response.get_source
kong.response.get_source = function ()
return "exit"
end

TemplateTransformerHandler:body_filter(config)
assert.spy(ngx.resp.get_headers).was_not_called()
assert.equal('{ "key" : "value" }', ngx.arg[1])

kong.response.get_source = old_get_source
end)

it("should not run when there is a cached response", function()
local config = {
response_template = "{ \"template\": 123 }"
Expand Down Expand Up @@ -406,13 +427,14 @@ describe("Test TemplateTransformerHandler body_filter", function()
it("should call and return ngx error when body is ready and not JSON", function()
mock_resp_headers = {}
ngx.arg[1] = nil
ngx.ERROR = "error"
local expected = "ERROR"
local config = {
response_template = '{ "bar" : "{{body.foo}}" }'
}
_G.ngx.ctx.buffer = '<html>'
actual = TemplateTransformerHandler:body_filter(config)
assert.equal(ngx.ERROR, actual)
assert.equal(expected, actual)
assert.spy(kong.response.error).was_called_with(500)
assert.is_nil(ngx.arg[1])
end)

Expand Down

0 comments on commit 29753a0

Please sign in to comment.