Skip to content

Commit

Permalink
policy/maintenance: make content-type header configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
davidor committed Aug 6, 2019
1 parent f0cb126 commit bcee0a9
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
"type": "string",
"description": "HTTP response to return",
"default": "Service Unavailable - Maintenance"
},
"message_content_type": {
"type": "string",
"description": "Content-Type header for the response",
"default": "text/plain; charset=utf-8"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,26 @@ local new = _M.new

local default_status_code = 503
local default_message = "Service Unavailable - Maintenance"
local default_message_content_type = "text/plain; charset=utf-8"

function _M.new(configuration)
local policy = new(configuration)

policy.status_code = default_status_code
policy.message = default_message
policy.message_content_type = default_message_content_type

if configuration then
policy.status_code = tonumber(configuration.status) or policy.status_code
policy.message = configuration.message or policy.message
policy.message_content_type = configuration.message_content_type or policy.message_content_type
end

return policy
end

function _M:rewrite()
ngx.header['Content-Type'] = self.message_content_type
ngx.status = self.status_code
ngx.say(self.message)

Expand Down
24 changes: 24 additions & 0 deletions spec/policy/maintenance_mode/maintenance_mode_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ describe('Maintenance mode policy', function()
before_each(function()
stub(ngx, 'say')
stub(ngx, 'exit')
ngx.header = {}
end)

context('when using the defaults', function()
Expand All @@ -21,6 +22,12 @@ describe('Maintenance mode policy', function()

assert.stub(ngx.say).was_called_with('Service Unavailable - Maintenance')
end)

it('returns the default Content-Type header', function()
maintenance_policy:rewrite()

assert.equals('text/plain; charset=utf-8', ngx.header['Content-Type'])
end)
end)

context('when using a custom status code', function()
Expand Down Expand Up @@ -48,5 +55,22 @@ describe('Maintenance mode policy', function()
assert.stub(ngx.say).was_called_with(custom_msg)
end)
end)

context('when using a custom content type', function()
it('sets the Content-Type header accordingly', function()
local custom_content_type = 'application/json'
local maintenance_policy = MaintenancePolicy.new(
{
message = '{ "msg": "some_msg" }',
message_content_type = custom_content_type
}
)


maintenance_policy:rewrite()

assert.equals('application/json', ngx.header['Content-Type'])
end)
end)
end)
end)
45 changes: 45 additions & 0 deletions t/apicast-policy-maintenance-mode.t
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,48 @@ Service Unavailable - Maintenance
--- error_code: 503
--- no_error_log
[error]
=== TEST 4: custom content-type
--- configuration
{
"services": [
{
"id": 42,
"backend_version": 1,
"backend_authentication_type": "service_token",
"backend_authentication_value": "token-value",
"proxy": {
"policy_chain": [
{
"name": "apicast.policy.maintenance_mode",
"configuration": {
"message": "{ \"msg\": \"Be back soon\" }",
"message_content_type": "application/json"
}
},
{ "name": "apicast.policy.apicast" }
],
"api_backend": "http://test:$TEST_NGINX_SERVER_PORT/",
"proxy_rules": [
{ "pattern": "/", "http_method": "GET", "metric_system_name": "hits", "delta": 2 }
]
}
}
]
}
--- upstream
location / {
content_by_lua_block {
local assert = require('luassert')
assert.is_true(false)
}
}
--- request
GET /
--- response_body
{ "msg": "Be back soon" }
--- response_headers
Content-Type: application/json
--- error_code: 503
--- no_error_log
[error]

0 comments on commit bcee0a9

Please sign in to comment.