1 | 'use strict';
|
2 |
|
3 | const asJsonPlugin = require('../../lib/middleware/asJson');
|
4 | const assert = require('chai').assert;
|
5 | const sinon = require('sinon');
|
6 | const sandbox = sinon.sandbox.create();
|
7 |
|
8 | const ctx = {
|
9 | res: {
|
10 | body: '{}',
|
11 | headers: {
|
12 | 'content-type': 'application/json'
|
13 | }
|
14 | }
|
15 | };
|
16 |
|
17 | function stubPromise() {
|
18 | return () => {
|
19 | return new Promise((resolve) => {
|
20 | resolve();
|
21 | });
|
22 | };
|
23 | }
|
24 |
|
25 | describe('asJson', () => {
|
26 | afterEach(() => {
|
27 | sandbox.restore();
|
28 | });
|
29 |
|
30 | it('parses json responses', async () => {
|
31 | const asJson = asJsonPlugin();
|
32 |
|
33 | await asJson(ctx, stubPromise());
|
34 | assert.deepEqual(ctx.res.body, {});
|
35 | });
|
36 |
|
37 | it('supports alternative json content types', async () => {
|
38 | const asJson = asJsonPlugin();
|
39 |
|
40 | const ctxAlternativeContentType = Object.assign({}, ctx);
|
41 | ctxAlternativeContentType.res.headers['content-type'] = 'application/other+json';
|
42 |
|
43 | await asJson(ctxAlternativeContentType, stubPromise());
|
44 | assert.deepEqual(ctxAlternativeContentType.res.body, {});
|
45 | });
|
46 |
|
47 | it('always parses response when force is set true', async () => {
|
48 | const asJson = asJsonPlugin({
|
49 | force: true
|
50 | });
|
51 |
|
52 | const ctxSimpleBody = {
|
53 | res: {
|
54 | body: 'a simple string',
|
55 | headers: {}
|
56 | }
|
57 | };
|
58 |
|
59 | try {
|
60 | await asJson(ctxSimpleBody, stubPromise());
|
61 | } catch (e) {
|
62 | return assert.match(e.message, /JSON parsing failure:.*/);
|
63 | }
|
64 | assert.fail('Expected to throw!');
|
65 | });
|
66 |
|
67 | it('ignores non-json content types.', async () => {
|
68 | const asJson = asJsonPlugin();
|
69 |
|
70 | const ctxSimpleBody = {
|
71 | res: {
|
72 | body: 'a simple string',
|
73 | headers: {}
|
74 | }
|
75 | };
|
76 |
|
77 | await asJson(ctxSimpleBody, stubPromise());
|
78 | assert.deepEqual(ctxSimpleBody.res.body, 'a simple string');
|
79 | });
|
80 |
|
81 | it('does not parse the response if the body is already an object', async () => {
|
82 | sandbox.stub(JSON, 'parse').returns({});
|
83 |
|
84 | const asJson = asJsonPlugin();
|
85 |
|
86 | const ctxWithJsonBody = {
|
87 | res: {
|
88 | body: {},
|
89 | headers: {}
|
90 | }
|
91 | };
|
92 |
|
93 | await asJson(ctxWithJsonBody, stubPromise());
|
94 | sinon.assert.notCalled(JSON.parse);
|
95 | assert.deepEqual(ctxWithJsonBody.res.body, {});
|
96 | });
|
97 |
|
98 | it('does not parse the response if there is no body', async () => {
|
99 | sandbox.stub(JSON, 'parse').throws();
|
100 |
|
101 | const asJson = asJsonPlugin();
|
102 |
|
103 | const ctxWithJsonBody = {
|
104 | res: {
|
105 | body: undefined,
|
106 | headers: {
|
107 | 'content-type': 'application/json'
|
108 | }
|
109 | }
|
110 | };
|
111 |
|
112 | await asJson(ctxWithJsonBody, stubPromise());
|
113 | sinon.assert.notCalled(JSON.parse);
|
114 | assert.deepEqual(ctxWithJsonBody.res.body, undefined);
|
115 | });
|
116 |
|
117 | it('does not parse the response if the body is empty', async () => {
|
118 | sandbox.stub(JSON, 'parse').throws();
|
119 |
|
120 | const asJson = asJsonPlugin();
|
121 |
|
122 | const ctxWithJsonBody = {
|
123 | res: {
|
124 | body: '',
|
125 | headers: {
|
126 | 'content-type': 'application/json'
|
127 | }
|
128 | }
|
129 | };
|
130 |
|
131 | await asJson(ctxWithJsonBody, stubPromise());
|
132 | sinon.assert.notCalled(JSON.parse);
|
133 | assert.deepEqual(ctxWithJsonBody.res.body, '');
|
134 | });
|
135 |
|
136 | it('throws an error when throw property is set true', async () => {
|
137 | const asJson = asJsonPlugin({
|
138 | throwOnConflict: true
|
139 | });
|
140 |
|
141 | const ctxWithSimpleBody = {
|
142 | res: {
|
143 | body: 'some string',
|
144 | headers: {
|
145 | 'content-type': 'text/html'
|
146 | }
|
147 | }
|
148 | };
|
149 |
|
150 | try {
|
151 | await asJson(ctxWithSimpleBody, stubPromise());
|
152 | } catch (err) {
|
153 | return assert.equal(err.message, 'expected a json content type got text/html');
|
154 | }
|
155 | assert.fail('Expected to throw!');
|
156 | });
|
157 | });
|