|
1 | 1 | const chai = require('chai');
|
2 | 2 | const sinonChai = require('sinon-chai');
|
3 | 3 | const proxyquire = require('proxyquire');
|
| 4 | +const chaiAsPromise = require('chai-as-promised'); |
| 5 | +const sinon = require('sinon'); |
| 6 | +const sandbox = sinon.createSandbox(); |
| 7 | + |
4 | 8 | chai.use(sinonChai);
|
| 9 | +chai.use(chaiAsPromise); |
5 | 10 |
|
6 |
| -const { expect } = chai; |
| 11 | +const { expect, assert } = chai; |
7 | 12 |
|
8 |
| -const { getContext, setContext } = require('../'); |
| 13 | +const { httpContext, validHooks } = require('../'); |
9 | 14 |
|
10 | 15 | const { buildFastify } = require('./mock-server.js');
|
11 | 16 |
|
12 | 17 | describe('Testing fastify-http-context', () => {
|
13 |
| - let fastify; |
14 |
| - |
15 |
| - describe('when setContext is called', () => { |
16 |
| - let setContext; |
17 |
| - let getContext; |
18 |
| - let namespace; |
19 |
| - |
20 |
| - describe('and the namespace is defined but not active', () => { |
21 |
| - let unactiveNamespaceFastify; |
22 |
| - |
23 |
| - afterEach(() => { |
24 |
| - fastify.close(); |
25 |
| - }); |
26 |
| - |
27 |
| - beforeEach(() => { |
28 |
| - fastify = buildFastify(); |
29 |
| - }); |
30 |
| - |
31 |
| - it('then the call returns undefined because it is out of scope even with defaults set', async() => { |
32 |
| - await fastify.inject({ method: 'GET', url: '/' }); |
33 |
| - getContext = require('../').getContext; |
34 |
| - expect(getContext('user')).to.be.undefined; |
35 |
| - }) |
36 |
| - }); |
37 |
| - describe('and the namespace is undeifned', () => { |
38 |
| - beforeEach(() => { |
39 |
| - const imports = require('../index.js'); |
40 |
| - setContext = imports.setContext; |
41 |
| - getContext = imports.getContext; |
42 |
| - }); |
43 |
| - |
44 |
| - it('then the getContext returns undefined', () => { |
45 |
| - setContext('value', 'value'); |
46 |
| - expect(getContext('value')).to.be.undefined; |
47 |
| - }); |
48 |
| - }); |
49 |
| - }); |
50 |
| - describe('When intialized', () => { |
51 |
| - afterEach(() => { |
52 |
| - fastify.close(); |
| 18 | + let fastify; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + sandbox.restore(); |
53 | 22 | });
|
54 | 23 |
|
55 |
| - describe('And there are no defaults', () => { |
56 |
| - beforeEach(() => { |
57 |
| - fastify = buildFastify(); |
58 |
| - }); |
| 24 | + describe('when httpContext.set is called', () => { |
| 25 | + let set; |
| 26 | + let get; |
| 27 | + let namespace; |
| 28 | + |
| 29 | + describe('and the namespace is defined but not active', () => { |
| 30 | + let unactiveNamespaceFastify; |
| 31 | + |
| 32 | + afterEach(() => { |
| 33 | + fastify.close(); |
| 34 | + }); |
59 | 35 |
|
60 |
| - describe('And no context was set', () => { |
61 |
| - it('then undefined is returned', async() => { |
62 |
| - const result = await fastify.inject({ method: 'GET', url: '/' }); |
63 |
| - expect(JSON.parse(result.body).user).to.be.undefined; |
| 36 | + beforeEach(async () => { |
| 37 | + fastify = await buildFastify(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('then the call returns undefined because it is out of scope even with defaults set', async () => { |
| 41 | + await fastify.inject({ method: 'GET', url: '/' }); |
| 42 | + get = require('../').httpContext.get; |
| 43 | + expect(get('user')).to.be.undefined; |
| 44 | + }) |
| 45 | + }); |
| 46 | + describe('and the namespace is undeifned', () => { |
| 47 | + beforeEach(() => { |
| 48 | + const imports = require('../index.js'); |
| 49 | + set = imports.httpContext.set; |
| 50 | + get = imports.httpContext.get; |
| 51 | + }); |
| 52 | + |
| 53 | + it('then the httpContext.get returns undefined', () => { |
| 54 | + set('value', 'value'); |
| 55 | + expect(httpContext.get('value')).to.be.undefined; |
| 56 | + }); |
64 | 57 | });
|
65 |
| - }); |
66 | 58 | });
|
67 |
| - |
68 |
| - describe('And there are defaults', () => { |
69 |
| - beforeEach(() => { |
70 |
| - fastify = buildFastify(true); |
71 |
| - }); |
72 |
| - |
73 |
| - describe('And the context is not set', () => { |
74 |
| - const defaultUser = { id: 'system' }; |
75 |
| - |
76 |
| - it('then the returned value is the default context value', async() => { |
77 |
| - const result = await fastify.inject({ method: 'GET', url: '/' }); |
78 |
| - expect(JSON.parse(result.body).user).to.deep.equal(defaultUser); |
| 59 | + describe('When intialized', () => { |
| 60 | + afterEach(() => { |
| 61 | + fastify.close(); |
79 | 62 | });
|
80 |
| - |
81 |
| - }); |
82 |
| - describe('And the context is set', () => { |
83 |
| - const user = { id: 'helloUser' }; |
84 |
| - |
85 |
| - beforeEach(() => { |
86 |
| - fastify.addHook('preHandler', async (req, reply) => { |
87 |
| - const id = req.headers['x-header-user']; |
88 |
| - if (id) { |
89 |
| - setContext('user', { id }); |
90 |
| - } |
91 |
| - return; |
92 |
| - }); |
| 63 | + |
| 64 | + describe('And an invalid hook is supplied', () => { |
| 65 | + beforeEach(async () => { |
| 66 | + fastify = await buildFastify(true, true); |
| 67 | + }); |
| 68 | + |
| 69 | + it('then an error is thrown', async () => { |
| 70 | + const logErrorStub = sandbox.stub(fastify.log, 'error'); |
| 71 | + await fastify.inject({ method: 'GET', url: '/' }); |
| 72 | + expect(logErrorStub).to.have.been.calledWith(`invalidHook is not a valid fastify hook. Please use one of the following ${validHooks}`); |
| 73 | + }); |
93 | 74 | });
|
94 | 75 |
|
95 |
| - it('then the returned value is what is in the context', async() => { |
96 |
| - const defaultUser = { id: 'system' }; |
97 |
| - const result = await fastify.inject({ method: 'GET', url: '/', headers: { 'x-header-user': 'helloUser' } }); |
98 |
| - expect(JSON.parse(result.body).user).to.deep.equal(user); |
99 |
| - |
100 |
| - const resultNoHeader = await fastify.inject({ method: 'GET', url: '/'}); |
101 |
| - expect(JSON.parse(resultNoHeader.body).user).to.deep.equal(defaultUser); |
| 76 | + describe('And there are no defaults', () => { |
| 77 | + beforeEach(async () => { |
| 78 | + fastify = await buildFastify(); |
| 79 | + }); |
| 80 | + |
| 81 | + describe('And no context was set', () => { |
| 82 | + it('then undefined is returned', async () => { |
| 83 | + const result = await fastify.inject({ method: 'GET', url: '/' }); |
| 84 | + expect(JSON.parse(result.body).user).to.be.undefined; |
| 85 | + }); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + describe('And there are defaults', () => { |
| 90 | + beforeEach(async () => { |
| 91 | + fastify = await buildFastify(true); |
| 92 | + }); |
| 93 | + |
| 94 | + describe('And the context is not set', () => { |
| 95 | + const defaultUser = { id: 'system' }; |
| 96 | + |
| 97 | + it('then the returned value is the default context value', async () => { |
| 98 | + const result = await fastify.inject({ method: 'GET', url: '/' }); |
| 99 | + expect(JSON.parse(result.body).user).to.deep.equal(defaultUser); |
| 100 | + }); |
| 101 | + |
| 102 | + }); |
| 103 | + describe('And the context is set', () => { |
| 104 | + const user = { id: 'helloUser' }; |
| 105 | + |
| 106 | + beforeEach(() => { |
| 107 | + fastify.addHook('preHandler', async (req, reply) => { |
| 108 | + const id = req.headers['x-header-user']; |
| 109 | + if (id) { |
| 110 | + httpContext.set('user', { id }); |
| 111 | + } |
| 112 | + return; |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + it('then the returned value is what is in the context', async () => { |
| 117 | + const defaultUser = { id: 'system' }; |
| 118 | + const result = await fastify.inject({ method: 'GET', url: '/', headers: { 'x-header-user': 'helloUser' } }); |
| 119 | + expect(JSON.parse(result.body).user).to.deep.equal(user); |
| 120 | + |
| 121 | + const resultNoHeader = await fastify.inject({ method: 'GET', url: '/' }); |
| 122 | + expect(JSON.parse(resultNoHeader.body).user).to.deep.equal(defaultUser); |
| 123 | + |
| 124 | + const resultRedo = await fastify.inject({ method: 'GET', url: '/', headers: { 'x-header-user': 'iAmUser' } }); |
| 125 | + expect(JSON.parse(resultRedo.body).user).to.deep.equal({ id: 'iAmUser' }); |
| 126 | + }); |
102 | 127 |
|
103 |
| - const resultRedo = await fastify.inject({ method: 'GET', url: '/', headers: { 'x-header-user': 'iAmUser' } }); |
104 |
| - expect(JSON.parse(resultRedo.body).user).to.deep.equal({ id: 'iAmUser' }); |
| 128 | + }); |
105 | 129 | });
|
106 |
| - |
107 |
| - }); |
108 | 130 | });
|
109 |
| - }); |
110 | 131 | });
|
0 commit comments