diff --git a/gateway/src/apicast/policy/maintenance_mode/apicast-policy.json b/gateway/src/apicast/policy/maintenance_mode/apicast-policy.json index 59536fb89..79ab484c9 100644 --- a/gateway/src/apicast/policy/maintenance_mode/apicast-policy.json +++ b/gateway/src/apicast/policy/maintenance_mode/apicast-policy.json @@ -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" } } } diff --git a/gateway/src/apicast/policy/maintenance_mode/maintenance_mode.lua b/gateway/src/apicast/policy/maintenance_mode/maintenance_mode.lua index 4a85eacf1..d651fce07 100644 --- a/gateway/src/apicast/policy/maintenance_mode/maintenance_mode.lua +++ b/gateway/src/apicast/policy/maintenance_mode/maintenance_mode.lua @@ -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) diff --git a/spec/policy/maintenance_mode/maintenance_mode_spec.lua b/spec/policy/maintenance_mode/maintenance_mode_spec.lua index ae9a7ad0c..7721df2af 100644 --- a/spec/policy/maintenance_mode/maintenance_mode_spec.lua +++ b/spec/policy/maintenance_mode/maintenance_mode_spec.lua @@ -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() @@ -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() @@ -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) diff --git a/t/apicast-policy-maintenance-mode.t b/t/apicast-policy-maintenance-mode.t index cafc26d9a..7f3e8429a 100644 --- a/t/apicast-policy-maintenance-mode.t +++ b/t/apicast-policy-maintenance-mode.t @@ -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]