|
| 1 | +local TransactionIDPolicy = require('apicast.policy.transaction_id') |
| 2 | +local uuid = require('resty.jit-uuid') |
| 3 | + |
| 4 | +describe('fapi_1_baseline_profile policy', function() |
| 5 | + local ngx_req_headers = {} |
| 6 | + local ngx_resp_headers = {} |
| 7 | + local context = {} |
| 8 | + before_each(function() |
| 9 | + ngx.header = {} |
| 10 | + ngx_req_headers = {} |
| 11 | + ngx_resp_headers = {} |
| 12 | + context = {} |
| 13 | + stub(ngx.req, 'get_headers', function() return ngx_req_headers end) |
| 14 | + stub(ngx.req, 'set_header', function(name, value) ngx_req_headers[name] = value end) |
| 15 | + stub(ngx.resp, 'get_headers', function() return ngx_resp_headers end) |
| 16 | + stub(ngx.resp, 'set_header', function(name, value) ngx_resp_headers[name] = value end) |
| 17 | + end) |
| 18 | + |
| 19 | + describe('.new', function() |
| 20 | + it('works without configuration', function() |
| 21 | + assert(TransactionIDPolicy.new()) |
| 22 | + end) |
| 23 | + end) |
| 24 | + |
| 25 | + describe('.rewrite', function() |
| 26 | + it('do not overwrite existing header', function() |
| 27 | + ngx_req_headers['transaction-id'] = 'abc' |
| 28 | + local config = {header_name='transaction-id'} |
| 29 | + local transaction_id_policy = TransactionIDPolicy.new(config) |
| 30 | + transaction_id_policy:rewrite() |
| 31 | + assert.same('abc', ngx.req.get_headers()['transaction-id']) |
| 32 | + end) |
| 33 | + |
| 34 | + it('generate uuid if header does not exist', function() |
| 35 | + local config = {header_name='transaction-id'} |
| 36 | + local transaction_id_policy = TransactionIDPolicy.new(config) |
| 37 | + transaction_id_policy:rewrite() |
| 38 | + assert.is_true(uuid.is_valid(ngx.req.get_headers()['transaction-id'])) |
| 39 | + end) |
| 40 | + |
| 41 | + it('generate uuid if header is empty', function() |
| 42 | + ngx_req_headers['transaction-id'] = '' |
| 43 | + local config = {header_name='transaction-id'} |
| 44 | + local transaction_id_policy = TransactionIDPolicy.new(config) |
| 45 | + transaction_id_policy:rewrite() |
| 46 | + assert.is_true(uuid.is_valid(ngx.req.get_headers()['transaction-id'])) |
| 47 | + end) |
| 48 | + end) |
| 49 | + |
| 50 | + describe('.header_filter', function() |
| 51 | + it('set response transaction-id if configured', function() |
| 52 | + ngx_req_headers['transaction-id'] = 'abc' |
| 53 | + local config = {header_name='transaction-id', include_in_response=true} |
| 54 | + local transaction_id_policy = TransactionIDPolicy.new(config) |
| 55 | + transaction_id_policy:rewrite(context) |
| 56 | + transaction_id_policy:header_filter(context) |
| 57 | + assert.same('abc', ngx.header['transaction-id']) |
| 58 | + end) |
| 59 | + |
| 60 | + it('set response transaction-id if configured - uuid', function() |
| 61 | + ngx_req_headers['transaction-id'] = '' |
| 62 | + local config = {header_name='transaction-id', include_in_response=true} |
| 63 | + local transaction_id_policy = TransactionIDPolicy.new(config) |
| 64 | + transaction_id_policy:rewrite(context) |
| 65 | + local id = ngx.req.get_headers()['transaction-id'] |
| 66 | + transaction_id_policy:header_filter(context) |
| 67 | + assert.same(id, ngx.header['transaction-id']) |
| 68 | + end) |
| 69 | + |
| 70 | + it('do not override if response contain the exisitng header', function() |
| 71 | + ngx_req_headers['transaction-id'] = 'abc' |
| 72 | + ngx_resp_headers['transaction-id'] = 'edf' |
| 73 | + local config = {header_name='transaction-id', include_in_response=true} |
| 74 | + local transaction_id_policy = TransactionIDPolicy.new(config) |
| 75 | + transaction_id_policy:rewrite(context) |
| 76 | + transaction_id_policy:header_filter(context) |
| 77 | + assert.same('edf', ngx.header['transaction-id']) |
| 78 | + end) |
| 79 | + end) |
| 80 | +end) |
0 commit comments