1 | 'use strict';
|
2 |
|
3 | const frisby = require('../src/frisby');
|
4 | const Joi = frisby.Joi;
|
5 | const mocks = require('./fixtures/http_mocks');
|
6 |
|
7 | const testHost = 'http://api.example.com';
|
8 |
|
9 | describe('expect(\'jsonTypes\')', function() {
|
10 |
|
11 | it('should match exact JSON', function(doneFn) {
|
12 | mocks.use(['getUser1']);
|
13 |
|
14 | frisby.fetch(testHost + '/users/1')
|
15 | .expect('jsonTypes', {
|
16 | id: Joi.number(),
|
17 | email: Joi.string()
|
18 | })
|
19 | .done(doneFn);
|
20 | });
|
21 |
|
22 | it('should error with extra key', function(doneFn) {
|
23 | mocks.use(['getUser1']);
|
24 |
|
25 | frisby.fetch(testHost + '/users/1')
|
26 | .expectNot('jsonTypes', {
|
27 | id: Joi.number().required(),
|
28 | id2: Joi.number().required(),
|
29 | email: Joi.string().email().required()
|
30 | })
|
31 | .done(doneFn);
|
32 | });
|
33 |
|
34 | it('should error with missing key', function(doneFn) {
|
35 | mocks.use(['getUser1']);
|
36 |
|
37 | frisby.fetch(testHost + '/users/1')
|
38 | .expectNot('jsonTypes', {
|
39 | email: Joi.number()
|
40 | })
|
41 | .done(doneFn);
|
42 | });
|
43 |
|
44 | it('should error with matching keys, but incorrect value types', function(doneFn) {
|
45 | mocks.use(['getUser1']);
|
46 |
|
47 | frisby.fetch(testHost + '/users/1')
|
48 | .expectNot('json', {
|
49 | id: Joi.string(),
|
50 | email: Joi.number()
|
51 | })
|
52 | .done(doneFn);
|
53 | });
|
54 |
|
55 | it('should match from data via fromJSON', function(doneFn) {
|
56 | frisby.fromJSON({
|
57 | foo: 'bar'
|
58 | })
|
59 | .expect('jsonTypes', {
|
60 | foo: Joi.string()
|
61 | })
|
62 | .done(doneFn);
|
63 | });
|
64 |
|
65 | it('should match JSON in using provided path', function(doneFn) {
|
66 | frisby.fromJSON({
|
67 | one: {
|
68 | two: {
|
69 | three: 3
|
70 | }
|
71 | }
|
72 | })
|
73 | .expect('jsonTypes', 'one.two', {
|
74 | three: Joi.number()
|
75 | })
|
76 | .done(doneFn);
|
77 | });
|
78 |
|
79 | it('should match JSON with nested structure and no path', function(doneFn) {
|
80 | frisby.fromJSON({
|
81 | one: {
|
82 | foo: 'bar',
|
83 | two: {
|
84 | three: 3
|
85 | }
|
86 | }
|
87 | })
|
88 | .expect('jsonTypes', {
|
89 | one: {
|
90 | foo: Joi.string(),
|
91 | two: {
|
92 | three: Joi.number()
|
93 | }
|
94 | }
|
95 | })
|
96 | .done(doneFn);
|
97 | });
|
98 |
|
99 | it('should match JSON with nested structure and single path', function(doneFn) {
|
100 | frisby.fromJSON({
|
101 | one: {
|
102 | foo: 'bar',
|
103 | two: {
|
104 | three: 3
|
105 | }
|
106 | }
|
107 | })
|
108 | .expect('jsonTypes', 'one', {
|
109 | foo: Joi.string(),
|
110 | two: {
|
111 | three: Joi.number()
|
112 | }
|
113 | })
|
114 | .done(doneFn);
|
115 | });
|
116 |
|
117 | it('should fail JSON with nested structure and incorrect type', function(doneFn) {
|
118 | frisby.fromJSON({
|
119 | one: {
|
120 | foo: 'bar',
|
121 | }
|
122 | })
|
123 | .expectNot('jsonTypes', 'one', {
|
124 | foo: Joi.number()
|
125 | })
|
126 | .done(doneFn);
|
127 | });
|
128 |
|
129 | it('should validate JSON with single value', function(doneFn) {
|
130 | frisby.fromJSON({
|
131 | one: {
|
132 | foo: 'bar',
|
133 | two: {
|
134 | three: 3
|
135 | }
|
136 | }
|
137 | })
|
138 | .expect('jsonTypes', 'one.two.three', Joi.number())
|
139 | .done(doneFn);
|
140 | });
|
141 |
|
142 | it('should match JSON with array of objects and asterisk path (each)', function (doneFn) {
|
143 | frisby.fromJSON({
|
144 | offers: [{name: 'offer1'}, {name: 'offer2'}]
|
145 | })
|
146 | .expect('jsonTypes', 'offers', Joi.array())
|
147 | .expect('jsonTypes', 'offers.*', {
|
148 | name: Joi.string()
|
149 | })
|
150 | .done(doneFn);
|
151 | });
|
152 |
|
153 | it('should ignore additional JSON keys', function (doneFn) {
|
154 | frisby.fromJSON({
|
155 | name: 'john',
|
156 | foo: 'bar'
|
157 | })
|
158 | .expect('jsonTypes', {
|
159 | name: Joi.string()
|
160 | })
|
161 | .done(doneFn);
|
162 | });
|
163 |
|
164 | it('should override joi global options', function (doneFn) {
|
165 | frisby.setup({ request: { inspectOnFailure: false } })
|
166 | .fromJSON('1')
|
167 | .expect('jsonTypes', Joi.number())
|
168 | .catch(function (err) {
|
169 | fail('this function will never be called.');
|
170 | })
|
171 | .expect('jsonTypes', Joi.number().options({
|
172 | convert: false
|
173 | }))
|
174 | .then(function (res) {
|
175 | fail('this function will never be called.');
|
176 | })
|
177 | .catch(function (err) {
|
178 | })
|
179 | .done(doneFn);
|
180 | });
|
181 |
|
182 | it('should output path in error message (1)', function (doneFn) {
|
183 | frisby.setup({ request: { inspectOnFailure: false } })
|
184 | .fromJSON({
|
185 | name: 'john'
|
186 | })
|
187 | .expect('jsonTypes', 'name', Joi.number())
|
188 | .catch(function (err) {
|
189 | expect(err.message).toMatch(/\bname\b/);
|
190 | expect(err.message).not.toMatch(/\bvalue\b/);
|
191 | })
|
192 | .done(doneFn);
|
193 | });
|
194 |
|
195 | it('should output path in error message (2)', function (doneFn) {
|
196 | frisby.setup({ request: { inspectOnFailure: false } })
|
197 | .fromJSON({
|
198 | user: {
|
199 | name: 'john'
|
200 | }
|
201 | })
|
202 | .expect('jsonTypes', 'user.name', Joi.number())
|
203 | .catch(function (err) {
|
204 | expect(err.message).toMatch(/\buser\.name\b/);
|
205 | expect(err.message).not.toMatch(/\bvalue\b/);
|
206 | })
|
207 | .done(doneFn);
|
208 | });
|
209 |
|
210 | it('should output default label in error message', function (doneFn) {
|
211 | frisby.setup({ request: { inspectOnFailure: false } })
|
212 | .fromJSON('john')
|
213 | .expect('jsonTypes', Joi.number())
|
214 | .catch(function (err) {
|
215 | expect(err.message).toMatch(/\bvalue\b/);
|
216 | })
|
217 | .done(doneFn);
|
218 | });
|
219 |
|
220 | it('should error in undefined JSON', function (doneFn) {
|
221 | frisby.setup({ request: { inspectOnFailure: false } })
|
222 | .fromJSON(undefined)
|
223 | .expect('jsonTypes', Joi.string())
|
224 | .catch(function (err) {
|
225 | expect(err.name).toBe('Error');
|
226 | expect(err.message).toContain('jsonBody is undefined');
|
227 | })
|
228 | .done(doneFn);
|
229 | });
|
230 | });
|
231 |
|
232 | describe('expect(\'jsonTypesStrict\')', function() {
|
233 |
|
234 | it('should error on additional JSON keys not accounted for', function (doneFn) {
|
235 | frisby.fromJSON({
|
236 | name: 'john',
|
237 | foo: 'bar'
|
238 | })
|
239 | .expect('jsonTypesStrict', {
|
240 | name: Joi.string(),
|
241 | foo: Joi.string()
|
242 | })
|
243 | .expectNot('jsonTypesStrict', {
|
244 | name: Joi.string()
|
245 | })
|
246 | .done(doneFn);
|
247 | });
|
248 | });
|