|
| 1 | +local FAPIPolicy = require('apicast.policy.fapi') |
| 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 | + stub(ngx, 'print') |
| 18 | + stub(ngx, 'exit') |
| 19 | + end) |
| 20 | + |
| 21 | + describe('.new', function() |
| 22 | + it('works without configuration', function() |
| 23 | + assert(FAPIPolicy.new({})) |
| 24 | + end) |
| 25 | + end) |
| 26 | + |
| 27 | + describe('.header_filter', function() |
| 28 | + it('Use value from request', function() |
| 29 | + ngx_req_headers['x-fapi-transaction-id'] = 'abc' |
| 30 | + local transaction_id_policy = FAPIPolicy.new({}) |
| 31 | + transaction_id_policy:header_filter() |
| 32 | + assert.same('abc', ngx.header['x-fapi-transaction-id']) |
| 33 | + end) |
| 34 | + |
| 35 | + it('Only use x-fapi-transaction-id from request if the header also exist in response from upstream', function() |
| 36 | + ngx_req_headers['x-fapi-transaction-id'] = 'abc' |
| 37 | + ngx_resp_headers['x-fapi-transaction-id'] = 'bdf' |
| 38 | + local transaction_id_policy = FAPIPolicy.new({}) |
| 39 | + transaction_id_policy:header_filter() |
| 40 | + assert.same('abc', ngx.header['x-fapi-transaction-id']) |
| 41 | + end) |
| 42 | + |
| 43 | + it('Use x-fapi-transaction-id from upstream response', function() |
| 44 | + ngx_resp_headers['x-fapi-transaction-id'] = 'abc' |
| 45 | + local transaction_id_policy = FAPIPolicy.new({}) |
| 46 | + transaction_id_policy:header_filter() |
| 47 | + assert.same('abc', ngx.header['x-fapi-transaction-id']) |
| 48 | + end) |
| 49 | + |
| 50 | + it('generate uuid if header does not exist in both request and response', function() |
| 51 | + local transaction_id_policy = FAPIPolicy.new({}) |
| 52 | + transaction_id_policy:header_filter() |
| 53 | + assert.is_true(uuid.is_valid(ngx.header['x-fapi-transaction-id'])) |
| 54 | + end) |
| 55 | + end) |
| 56 | + |
| 57 | + describe('x-fapi-customer-ip-address', function() |
| 58 | + it('Allow request with valid IPv4', function() |
| 59 | + ngx_req_headers['x-fapi-customer-ip-address'] = '127.0.0.1' |
| 60 | + local transaction_id_policy = FAPIPolicy.new({validate_x_fapi_customer_ip_address=true}) |
| 61 | + transaction_id_policy:access() |
| 62 | + assert.stub(ngx.exit).was_not.called_with(403) |
| 63 | + end) |
| 64 | + |
| 65 | + it('Allow request with valid IPv6', function() |
| 66 | + ngx_req_headers['x-fapi-customer-ip-address'] = '2001:db8::123:12:1' |
| 67 | + local transaction_id_policy = FAPIPolicy.new({validate_x_fapi_customer_ip_address=true}) |
| 68 | + transaction_id_policy:access() |
| 69 | + assert.stub(ngx.exit).was_not.called_with(403) |
| 70 | + end) |
| 71 | + |
| 72 | + it('Reject request if header contains more than 1 IP', function() |
| 73 | + ngx_req_headers['x-fapi-customer-ip-address'] = {"2001:db8::123:12:1", "127.0.0.1"} |
| 74 | + local transaction_id_policy = FAPIPolicy.new({validate_x_fapi_customer_ip_address=true}) |
| 75 | + transaction_id_policy:access() |
| 76 | + assert.same(ngx.status, 403) |
| 77 | + assert.stub(ngx.print).was.called_with('{"error": "invalid_request"}') |
| 78 | + assert.stub(ngx.exit).was.called_with(403) |
| 79 | + end) |
| 80 | + end) |
| 81 | +end) |
0 commit comments