|
| 1 | +import { assert } from 'chai'; |
| 2 | +import fetchMock from 'fetch-mock'; |
| 3 | +import { RelayNetworkLayer } from '../../src'; |
| 4 | +import { mockReq } from '../testutils'; |
| 5 | +import authMiddleware from '../../src/middleware/auth'; |
| 6 | + |
| 7 | +describe('Middleware / auth', () => { |
| 8 | + describe('`token` option as string (with default `prefix` and `header`)', () => { |
| 9 | + const rnl = new RelayNetworkLayer([ |
| 10 | + authMiddleware({ |
| 11 | + token: '123', |
| 12 | + tokenRefreshPromise: () => 345, |
| 13 | + }), |
| 14 | + ]); |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + fetchMock.restore(); |
| 18 | + |
| 19 | + fetchMock.mock({ |
| 20 | + matcher: '/graphql', |
| 21 | + response: { |
| 22 | + status: 200, |
| 23 | + body: { data: 'PAYLOAD' }, |
| 24 | + sendAsJson: true, |
| 25 | + }, |
| 26 | + method: 'POST', |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should work with query', () => { |
| 31 | + const req1 = mockReq(); |
| 32 | + return rnl.sendQueries([req1]).then(() => { |
| 33 | + assert.equal(req1.payload.response, 'PAYLOAD'); |
| 34 | + const reqs = fetchMock.calls('/graphql'); |
| 35 | + assert.equal(reqs.length, 1); |
| 36 | + assert.equal(reqs[0][1].headers.Authorization, 'Bearer 123'); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should work with mutation', () => { |
| 41 | + const req1 = mockReq(); |
| 42 | + return assert.isFulfilled( |
| 43 | + rnl.sendMutation(req1).then(() => { |
| 44 | + assert.equal(req1.payload.response, 'PAYLOAD'); |
| 45 | + const reqs = fetchMock.calls('/graphql'); |
| 46 | + assert.equal(reqs.length, 1); |
| 47 | + assert.equal(reqs[0][1].headers.Authorization, 'Bearer 123'); |
| 48 | + }) |
| 49 | + ); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('`token` option as thunk (with custom `prefix` and `header`)', () => { |
| 54 | + const rnl = new RelayNetworkLayer([ |
| 55 | + authMiddleware({ |
| 56 | + token: () => '333', |
| 57 | + tokenRefreshPromise: () => 345, |
| 58 | + prefix: 'MyBearer ', |
| 59 | + header: 'MyAuthorization', |
| 60 | + }), |
| 61 | + ]); |
| 62 | + |
| 63 | + beforeEach(() => { |
| 64 | + fetchMock.restore(); |
| 65 | + |
| 66 | + fetchMock.mock({ |
| 67 | + matcher: '/graphql', |
| 68 | + response: { |
| 69 | + status: 200, |
| 70 | + body: { data: 'PAYLOAD' }, |
| 71 | + sendAsJson: true, |
| 72 | + }, |
| 73 | + method: 'POST', |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should work with query', () => { |
| 78 | + const req1 = mockReq(); |
| 79 | + return rnl.sendQueries([req1]).then(() => { |
| 80 | + assert.equal(req1.payload.response, 'PAYLOAD'); |
| 81 | + const reqs = fetchMock.calls('/graphql'); |
| 82 | + assert.equal(reqs.length, 1); |
| 83 | + assert.equal(reqs[0][1].headers.MyAuthorization, 'MyBearer 333'); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should work with mutation', () => { |
| 88 | + const req1 = mockReq(); |
| 89 | + return assert.isFulfilled( |
| 90 | + rnl.sendMutation(req1).then(() => { |
| 91 | + assert.equal(req1.payload.response, 'PAYLOAD'); |
| 92 | + const reqs = fetchMock.calls('/graphql'); |
| 93 | + assert.equal(reqs.length, 1); |
| 94 | + assert.equal(reqs[0][1].headers.MyAuthorization, 'MyBearer 333'); |
| 95 | + }) |
| 96 | + ); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + describe('`tokenRefreshPromise` should be called on 401 response', () => { |
| 101 | + beforeEach(() => { |
| 102 | + fetchMock.restore(); |
| 103 | + |
| 104 | + fetchMock.mock({ |
| 105 | + matcher: '/graphql', |
| 106 | + response: { |
| 107 | + status: 401, |
| 108 | + body: { data: 'PAYLOAD' }, |
| 109 | + sendAsJson: true, |
| 110 | + }, |
| 111 | + method: 'POST', |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should work with query (provided promise)', () => { |
| 116 | + const rnl = new RelayNetworkLayer([ |
| 117 | + authMiddleware({ |
| 118 | + token: '123', |
| 119 | + tokenRefreshPromise: () => Promise.resolve(345), |
| 120 | + }), |
| 121 | + ]); |
| 122 | + |
| 123 | + const req1 = mockReq(); |
| 124 | + return rnl.sendQueries([req1]).then(() => { |
| 125 | + const reqs = fetchMock.calls('/graphql'); |
| 126 | + assert.equal(reqs.length, 2); |
| 127 | + assert.equal(reqs[1][1].headers.Authorization, 'Bearer 345'); |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should work with mutation (provided regular value)', () => { |
| 132 | + const rnl = new RelayNetworkLayer([ |
| 133 | + authMiddleware({ |
| 134 | + token: '123', |
| 135 | + tokenRefreshPromise: () => 456, |
| 136 | + }), |
| 137 | + ]); |
| 138 | + |
| 139 | + const req1 = mockReq(); |
| 140 | + return assert.isFulfilled( |
| 141 | + rnl.sendMutation(req1).then(() => { |
| 142 | + const reqs = fetchMock.calls('/graphql'); |
| 143 | + assert.equal(reqs.length, 2); |
| 144 | + assert.equal(reqs[1][1].headers.Authorization, 'Bearer 456'); |
| 145 | + }) |
| 146 | + ); |
| 147 | + }); |
| 148 | + }); |
| 149 | +}); |
0 commit comments