@athenna/http 3.3.0 → 3.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,98 @@
1
+ /**
2
+ * @athenna/http
3
+ *
4
+ * (c) João Lenon <lenon@athenna.io>
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+ import { Assert } from '@japa/assert';
10
+ import { InjectOptions } from 'fastify';
11
+ import { TestResponse } from '#src/Testing/Plugins/Request/TestResponse';
12
+ export declare class TestRequest {
13
+ /**
14
+ * Japa assert class instance.
15
+ */
16
+ assert: Assert;
17
+ /**
18
+ * Instantiate TestResponse class from API response.
19
+ */
20
+ createResponse(response: any): TestResponse;
21
+ /**
22
+ * Make a GET request to the given URL and options.
23
+ *
24
+ * @example
25
+ * ```js
26
+ * const response = await request.get('/users')
27
+ *
28
+ * response.assertStatusCode(200)
29
+ * ```
30
+ */
31
+ get(url: string, options?: InjectOptions): Promise<TestResponse>;
32
+ /**
33
+ * Make a HEAD request to the given URL and options.
34
+ *
35
+ * @example
36
+ * ```js
37
+ * const response = await request.head('/users')
38
+ *
39
+ * response.assertStatusCode(200)
40
+ * ```
41
+ */
42
+ head(url: string, options?: InjectOptions): Promise<TestResponse>;
43
+ /**
44
+ * Make a OPTIONS request to the given URL and options.
45
+ *
46
+ * @example
47
+ * ```js
48
+ * const response = await request.options('/users')
49
+ *
50
+ * response.assertStatusCode(200)
51
+ * ```
52
+ */
53
+ options(url: string, options?: InjectOptions): Promise<TestResponse>;
54
+ /**
55
+ * Make a POST request to the given URL and options.
56
+ *
57
+ * @example
58
+ * ```js
59
+ * const response = await request.post('/users')
60
+ *
61
+ * response.assertStatusCode(200)
62
+ * ```
63
+ */
64
+ post(url: string, options?: InjectOptions): Promise<TestResponse>;
65
+ /**
66
+ * Make a PUT request to the given URL and options.
67
+ *
68
+ * @example
69
+ * ```js
70
+ * const response = await request.put('/users')
71
+ *
72
+ * response.assertStatusCode(200)
73
+ * ```
74
+ */
75
+ put(url: string, options?: InjectOptions): Promise<TestResponse>;
76
+ /**
77
+ * Make a PATCH request to the given URL and options.
78
+ *
79
+ * @example
80
+ * ```js
81
+ * const response = await request.patch('/users')
82
+ *
83
+ * response.assertStatusCode(200)
84
+ * ```
85
+ */
86
+ patch(url: string, options?: InjectOptions): Promise<TestResponse>;
87
+ /**
88
+ * Make a DELETE request to the given URL and options.
89
+ *
90
+ * @example
91
+ * ```js
92
+ * const response = await request.delete('/users')
93
+ *
94
+ * response.assertStatusCode(200)
95
+ * ```
96
+ */
97
+ delete(url: string, options?: InjectOptions): Promise<TestResponse>;
98
+ }
@@ -0,0 +1,114 @@
1
+ /**
2
+ * @athenna/http
3
+ *
4
+ * (c) João Lenon <lenon@athenna.io>
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+ import { Server } from '#src';
10
+ import { Assert } from '@japa/assert';
11
+ import { TestResponse } from '#src/Testing/Plugins/Request/TestResponse';
12
+ export class TestRequest {
13
+ /**
14
+ * Japa assert class instance.
15
+ */
16
+ assert = new Assert();
17
+ /**
18
+ * Instantiate TestResponse class from API response.
19
+ */
20
+ createResponse(response) {
21
+ return new TestResponse(this.assert, response);
22
+ }
23
+ /**
24
+ * Make a GET request to the given URL and options.
25
+ *
26
+ * @example
27
+ * ```js
28
+ * const response = await request.get('/users')
29
+ *
30
+ * response.assertStatusCode(200)
31
+ * ```
32
+ */
33
+ async get(url, options = {}) {
34
+ return Server.request({ url, method: 'GET', ...options }).then(res => this.createResponse(res));
35
+ }
36
+ /**
37
+ * Make a HEAD request to the given URL and options.
38
+ *
39
+ * @example
40
+ * ```js
41
+ * const response = await request.head('/users')
42
+ *
43
+ * response.assertStatusCode(200)
44
+ * ```
45
+ */
46
+ async head(url, options = {}) {
47
+ return Server.request({ url, method: 'HEAD', ...options }).then(res => this.createResponse(res));
48
+ }
49
+ /**
50
+ * Make a OPTIONS request to the given URL and options.
51
+ *
52
+ * @example
53
+ * ```js
54
+ * const response = await request.options('/users')
55
+ *
56
+ * response.assertStatusCode(200)
57
+ * ```
58
+ */
59
+ async options(url, options = {}) {
60
+ return Server.request({ url, method: 'OPTIONS', ...options }).then(res => this.createResponse(res));
61
+ }
62
+ /**
63
+ * Make a POST request to the given URL and options.
64
+ *
65
+ * @example
66
+ * ```js
67
+ * const response = await request.post('/users')
68
+ *
69
+ * response.assertStatusCode(200)
70
+ * ```
71
+ */
72
+ async post(url, options = {}) {
73
+ return Server.request({ url, method: 'POST', ...options }).then(res => this.createResponse(res));
74
+ }
75
+ /**
76
+ * Make a PUT request to the given URL and options.
77
+ *
78
+ * @example
79
+ * ```js
80
+ * const response = await request.put('/users')
81
+ *
82
+ * response.assertStatusCode(200)
83
+ * ```
84
+ */
85
+ async put(url, options = {}) {
86
+ return Server.request({ url, method: 'PUT', ...options }).then(res => this.createResponse(res));
87
+ }
88
+ /**
89
+ * Make a PATCH request to the given URL and options.
90
+ *
91
+ * @example
92
+ * ```js
93
+ * const response = await request.patch('/users')
94
+ *
95
+ * response.assertStatusCode(200)
96
+ * ```
97
+ */
98
+ async patch(url, options = {}) {
99
+ return Server.request({ url, method: 'PATCH', ...options }).then(res => this.createResponse(res));
100
+ }
101
+ /**
102
+ * Make a DELETE request to the given URL and options.
103
+ *
104
+ * @example
105
+ * ```js
106
+ * const response = await request.delete('/users')
107
+ *
108
+ * response.assertStatusCode(200)
109
+ * ```
110
+ */
111
+ async delete(url, options = {}) {
112
+ return Server.request({ url, method: 'DELETE', ...options }).then(res => this.createResponse(res));
113
+ }
114
+ }
@@ -0,0 +1,355 @@
1
+ /**
2
+ * @athenna/http
3
+ *
4
+ * (c) João Lenon <lenon@athenna.io>
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+ import { Assert } from '@japa/assert';
10
+ import { Response } from 'light-my-request';
11
+ export declare class TestResponse {
12
+ /**
13
+ * Japa assert class instance.
14
+ */
15
+ assert: Assert;
16
+ /**
17
+ * Light my request response object.
18
+ */
19
+ response: Response;
20
+ constructor(assert: Assert, response: Response);
21
+ /**
22
+ * Assert the status code of the response.
23
+ *
24
+ * @example
25
+ * ```js
26
+ * response.assertStatusCode(200)
27
+ * ```
28
+ */
29
+ assertStatusCode(statusCode: number): void;
30
+ /**
31
+ * Assert the status code is not the same of the response.
32
+ *
33
+ * @example
34
+ * ```js
35
+ * response.assertIsNotStatusCode(200)
36
+ * ```
37
+ */
38
+ assertIsNotStatusCode(statusCode: number): void;
39
+ /**
40
+ * Assert body (array or object) to contain a subset of the expected value.
41
+ *
42
+ * @example
43
+ * ```js
44
+ * const body = { id: 1, name: 'post 1' }
45
+ *
46
+ * response.assertBodyContains({ id: 1 }) // passes
47
+ * ```
48
+ * @example
49
+ * ```js
50
+ * const body = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
51
+ *
52
+ * response.assertBodyContains([{ id: 1 }, { id: 2 }]) // passes
53
+ * ```
54
+ */
55
+ assertBodyContains(values: any | any[]): void;
56
+ /**
57
+ * Assert body (array or object) to not contain a subset of the expected value.
58
+ *
59
+ * @example
60
+ * ```js
61
+ * const body = { id: 1, name: 'post 1' }
62
+ *
63
+ * response.assertBodyNotContains({ id: 1 }) // fails
64
+ * ```
65
+ * @example
66
+ * ```js
67
+ * const body = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
68
+ *
69
+ * response.assertBodyNotContains([{ id: 3 }]) // passes
70
+ * ```
71
+ */
72
+ assertBodyNotContains(values: any | any[]): void;
73
+ /**
74
+ * Assert body to contain a key.
75
+ *
76
+ * @example
77
+ * ```js
78
+ * const body = { id: 1, name: 'post 1' }
79
+ *
80
+ * response.assertBodyContainsKey('id') // passes
81
+ * ```
82
+ */
83
+ assertBodyContainsKey(key: string): void;
84
+ /**
85
+ * Assert body to not contain a key.
86
+ *
87
+ * @example
88
+ * ```js
89
+ * const body = { id: 1, name: 'post 1' }
90
+ *
91
+ * response.assertBodyNotContainsKey('id') // fails
92
+ * ```
93
+ * @example
94
+ * ```js
95
+ * const body = { id: 1, name: 'post 1' }
96
+ *
97
+ * response.assertBodyNotContainsKey('createdAt') // passes
98
+ * ```
99
+ */
100
+ assertBodyNotContainsKey(key: string): void;
101
+ /**
102
+ * Assert body to contain all keys.
103
+ *
104
+ * @example
105
+ * ```js
106
+ * const body = { id: 1, name: 'post 1' }
107
+ *
108
+ * response.assertBodyContainsAllKeys(['id', 'post']) // passes
109
+ * ```
110
+ */
111
+ assertBodyContainsAllKeys(keys: string[]): void;
112
+ /**
113
+ * Assert body to not contain all keys.
114
+ *
115
+ * @example
116
+ * ```js
117
+ * const body = { id: 1, name: 'post 1' }
118
+ *
119
+ * response.assertBodyNotContainsAllKeys(['id']) // fails
120
+ * ```
121
+ * @example
122
+ * ```js
123
+ * const body = { id: 1, name: 'post 1' }
124
+ *
125
+ * response.assertBodyNotContainsAllKeys(['createdAt']) // passes
126
+ * ```
127
+ */
128
+ assertBodyNotContainsAllKeys(keys: string[]): void;
129
+ /**
130
+ * Assert body (array or object) to be deep equal to the expected value.
131
+ *
132
+ * @example
133
+ * ```js
134
+ * const body = { id: 1, name: 'post 1' }
135
+ *
136
+ * response.assertBodyDeepEqual({ id: 1 }) // fails
137
+ * ```
138
+ * @example
139
+ * ```js
140
+ * const body = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
141
+ *
142
+ * response.assertBodyDeepEqual([{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]) // passes
143
+ * ```
144
+ */
145
+ assertBodyDeepEqual(values: any | any[]): void;
146
+ /**
147
+ * Assert body (array or object) to be not deep equal to the expected value.
148
+ *
149
+ * @example
150
+ * ```js
151
+ * const body = { id: 1, name: 'post 1' }
152
+ *
153
+ * response.assertBodyNotDeepEqual({ id: 1 }) // passes
154
+ * ```
155
+ * @example
156
+ * ```js
157
+ * const body = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
158
+ *
159
+ * response.assertBodyNotDeepEqual([{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]) // fails
160
+ * ```
161
+ */
162
+ assertBodyNotDeepEqual(values: any | any[]): void;
163
+ /**
164
+ * Assert body to be an array.
165
+ *
166
+ * @example
167
+ * ```js
168
+ * const body = { id: 1, name: 'post 1' }
169
+ *
170
+ * response.assertBodyIsArray() // fails
171
+ * ```
172
+ * @example
173
+ * ```js
174
+ * const body = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
175
+ *
176
+ * response.assertBodyIsArray() // passes
177
+ * ```
178
+ */
179
+ assertBodyIsArray(): void;
180
+ /**
181
+ * Assert body to not be an array.
182
+ *
183
+ * @example
184
+ * ```js
185
+ * const body = { id: 1, name: 'post 1' }
186
+ *
187
+ * response.assertBodyIsNotArray() // passes
188
+ * ```
189
+ * @example
190
+ * ```js
191
+ * const body = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
192
+ *
193
+ * response.assertBodyIsNotArray() // fails
194
+ * ```
195
+ */
196
+ assertBodyIsNotArray(): void;
197
+ /**
198
+ * Assert body to be an object.
199
+ *
200
+ * @example
201
+ * ```js
202
+ * const body = { id: 1, name: 'post 1' }
203
+ *
204
+ * response.assertBodyIsObject() // passes
205
+ * ```
206
+ * @example
207
+ * ```js
208
+ * const body = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
209
+ *
210
+ * response.assertBodyIsObject() // fails
211
+ * ```
212
+ */
213
+ assertBodyIsObject(): void;
214
+ /**
215
+ * Assert body to not be an object.
216
+ *
217
+ * @example
218
+ * ```js
219
+ * const body = { id: 1, name: 'post 1' }
220
+ *
221
+ * response.assertBodyIsNotObject() // fails
222
+ * ```
223
+ * @example
224
+ * ```js
225
+ * const body = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
226
+ *
227
+ * response.assertBodyIsNotObject() // passes
228
+ * ```
229
+ */
230
+ assertBodyIsNotObject(): void;
231
+ /**
232
+ * Assert header (array or object) to contain a subset of the expected value.
233
+ *
234
+ * @example
235
+ * ```js
236
+ * const header = { id: 1, name: 'post 1' }
237
+ *
238
+ * response.assertHeaderContains({ id: 1 }) // passes
239
+ * ```
240
+ * @example
241
+ * ```js
242
+ * const header = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
243
+ *
244
+ * response.assertHeaderContains([{ id: 1 }, { id: 2 }]) // passes
245
+ * ```
246
+ */
247
+ assertHeaderContains(values: any | any[]): void;
248
+ /**
249
+ * Assert header (array or object) to not contain a subset of the expected value.
250
+ *
251
+ * @example
252
+ * ```js
253
+ * const header = { id: 1, name: 'post 1' }
254
+ *
255
+ * response.assertHeaderContains({ id: 1 }) // passes
256
+ * ```
257
+ * @example
258
+ * ```js
259
+ * const header = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
260
+ *
261
+ * response.assertHeaderContains([{ id: 1 }, { id: 2 }]) // passes
262
+ * ```
263
+ */
264
+ assertHeaderNotContains(values: any | any[]): void;
265
+ /**
266
+ * Assert header (array or object) to be deep equal to the expected value.
267
+ *
268
+ * @example
269
+ * ```js
270
+ * const header = { id: 1, name: 'post 1' }
271
+ *
272
+ * response.assertHeaderDeepEqual({ id: 1 }) // fails
273
+ * ```
274
+ * @example
275
+ * ```js
276
+ * const header = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
277
+ *
278
+ * response.assertHeaderDeepEqual([{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]) // passes
279
+ * ```
280
+ */
281
+ assertHeaderDeepEqual(values: any | any[]): void;
282
+ /**
283
+ * Assert header (array or object) to be not deep equal to the expected value.
284
+ *
285
+ * @example
286
+ * ```js
287
+ * const header = { id: 1, name: 'post 1' }
288
+ *
289
+ * response.assertHeaderNotDeepEqual({ id: 1 }) // passes
290
+ * ```
291
+ * @example
292
+ * ```js
293
+ * const header = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
294
+ *
295
+ * response.assertHeaderNotDeepEqual([{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]) // fails
296
+ * ```
297
+ */
298
+ assertHeaderNotDeepEqual(values: any | any[]): void;
299
+ /**
300
+ * Assert header to contain a key.
301
+ *
302
+ * @example
303
+ * ```js
304
+ * const header = { id: 1, name: 'post 1' }
305
+ *
306
+ * response.assertHeaderContainsKey('id') // passes
307
+ * ```
308
+ */
309
+ assertHeaderContainsKey(key: string): void;
310
+ /**
311
+ * Assert header to not contain a key.
312
+ *
313
+ * @example
314
+ * ```js
315
+ * const body = { id: 1, name: 'post 1' }
316
+ *
317
+ * response.assertHeaderNotContainsKey('id') // fails
318
+ * ```
319
+ * @example
320
+ * ```js
321
+ * const body = { id: 1, name: 'post 1' }
322
+ *
323
+ * response.assertHeaderNotContainsKey('createdAt') // passes
324
+ * ```
325
+ */
326
+ assertHeaderNotContainsKey(key: string): void;
327
+ /**
328
+ * Assert header to contain all keys.
329
+ *
330
+ * @example
331
+ * ```js
332
+ * const header = { id: 1, name: 'post 1' }
333
+ *
334
+ * response.assertHeaderContainsAllKeys(['id', 'post']) // passes
335
+ * ```
336
+ */
337
+ assertHeaderContainsAllKeys(keys: string[]): void;
338
+ /**
339
+ * Assert header to not contain all keys.
340
+ *
341
+ * @example
342
+ * ```js
343
+ * const header = { id: 1, name: 'post 1' }
344
+ *
345
+ * response.assertHeaderNotContainsAllKeys(['id']) // fails
346
+ * ```
347
+ * @example
348
+ * ```js
349
+ * const header = { id: 1, name: 'post 1' }
350
+ *
351
+ * response.assertHeaderNotContainsAllKeys(['createdAt']) // passes
352
+ * ```
353
+ */
354
+ assertHeaderNotContainsAllKeys(keys: string[]): void;
355
+ }
@@ -0,0 +1,400 @@
1
+ /**
2
+ * @athenna/http
3
+ *
4
+ * (c) João Lenon <lenon@athenna.io>
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+ export class TestResponse {
10
+ /**
11
+ * Japa assert class instance.
12
+ */
13
+ assert;
14
+ /**
15
+ * Light my request response object.
16
+ */
17
+ response;
18
+ constructor(assert, response) {
19
+ this.assert = assert;
20
+ this.response = response;
21
+ }
22
+ /**
23
+ * Assert the status code of the response.
24
+ *
25
+ * @example
26
+ * ```js
27
+ * response.assertStatusCode(200)
28
+ * ```
29
+ */
30
+ assertStatusCode(statusCode) {
31
+ this.assert.deepEqual(this.response.statusCode, statusCode);
32
+ }
33
+ /**
34
+ * Assert the status code is not the same of the response.
35
+ *
36
+ * @example
37
+ * ```js
38
+ * response.assertIsNotStatusCode(200)
39
+ * ```
40
+ */
41
+ assertIsNotStatusCode(statusCode) {
42
+ this.assert.notDeepEqual(this.response.statusCode, statusCode);
43
+ }
44
+ /**
45
+ * Assert body (array or object) to contain a subset of the expected value.
46
+ *
47
+ * @example
48
+ * ```js
49
+ * const body = { id: 1, name: 'post 1' }
50
+ *
51
+ * response.assertBodyContains({ id: 1 }) // passes
52
+ * ```
53
+ * @example
54
+ * ```js
55
+ * const body = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
56
+ *
57
+ * response.assertBodyContains([{ id: 1 }, { id: 2 }]) // passes
58
+ * ```
59
+ */
60
+ assertBodyContains(values) {
61
+ this.assert.containsSubset(this.response.json(), values);
62
+ }
63
+ /**
64
+ * Assert body (array or object) to not contain a subset of the expected value.
65
+ *
66
+ * @example
67
+ * ```js
68
+ * const body = { id: 1, name: 'post 1' }
69
+ *
70
+ * response.assertBodyNotContains({ id: 1 }) // fails
71
+ * ```
72
+ * @example
73
+ * ```js
74
+ * const body = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
75
+ *
76
+ * response.assertBodyNotContains([{ id: 3 }]) // passes
77
+ * ```
78
+ */
79
+ assertBodyNotContains(values) {
80
+ this.assert.notContainsSubset(this.response.json(), values);
81
+ }
82
+ /**
83
+ * Assert body to contain a key.
84
+ *
85
+ * @example
86
+ * ```js
87
+ * const body = { id: 1, name: 'post 1' }
88
+ *
89
+ * response.assertBodyContainsKey('id') // passes
90
+ * ```
91
+ */
92
+ assertBodyContainsKey(key) {
93
+ this.assert.property(this.response.json(), key);
94
+ }
95
+ /**
96
+ * Assert body to not contain a key.
97
+ *
98
+ * @example
99
+ * ```js
100
+ * const body = { id: 1, name: 'post 1' }
101
+ *
102
+ * response.assertBodyNotContainsKey('id') // fails
103
+ * ```
104
+ * @example
105
+ * ```js
106
+ * const body = { id: 1, name: 'post 1' }
107
+ *
108
+ * response.assertBodyNotContainsKey('createdAt') // passes
109
+ * ```
110
+ */
111
+ assertBodyNotContainsKey(key) {
112
+ this.assert.notProperty(this.response.json(), key);
113
+ }
114
+ /**
115
+ * Assert body to contain all keys.
116
+ *
117
+ * @example
118
+ * ```js
119
+ * const body = { id: 1, name: 'post 1' }
120
+ *
121
+ * response.assertBodyContainsAllKeys(['id', 'post']) // passes
122
+ * ```
123
+ */
124
+ assertBodyContainsAllKeys(keys) {
125
+ this.assert.properties(this.response.json(), keys);
126
+ }
127
+ /**
128
+ * Assert body to not contain all keys.
129
+ *
130
+ * @example
131
+ * ```js
132
+ * const body = { id: 1, name: 'post 1' }
133
+ *
134
+ * response.assertBodyNotContainsAllKeys(['id']) // fails
135
+ * ```
136
+ * @example
137
+ * ```js
138
+ * const body = { id: 1, name: 'post 1' }
139
+ *
140
+ * response.assertBodyNotContainsAllKeys(['createdAt']) // passes
141
+ * ```
142
+ */
143
+ assertBodyNotContainsAllKeys(keys) {
144
+ this.assert.notAllProperties(this.response.json(), keys);
145
+ }
146
+ /**
147
+ * Assert body (array or object) to be deep equal to the expected value.
148
+ *
149
+ * @example
150
+ * ```js
151
+ * const body = { id: 1, name: 'post 1' }
152
+ *
153
+ * response.assertBodyDeepEqual({ id: 1 }) // fails
154
+ * ```
155
+ * @example
156
+ * ```js
157
+ * const body = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
158
+ *
159
+ * response.assertBodyDeepEqual([{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]) // passes
160
+ * ```
161
+ */
162
+ assertBodyDeepEqual(values) {
163
+ this.assert.deepEqual(this.response.json(), values);
164
+ }
165
+ /**
166
+ * Assert body (array or object) to be not deep equal to the expected value.
167
+ *
168
+ * @example
169
+ * ```js
170
+ * const body = { id: 1, name: 'post 1' }
171
+ *
172
+ * response.assertBodyNotDeepEqual({ id: 1 }) // passes
173
+ * ```
174
+ * @example
175
+ * ```js
176
+ * const body = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
177
+ *
178
+ * response.assertBodyNotDeepEqual([{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]) // fails
179
+ * ```
180
+ */
181
+ assertBodyNotDeepEqual(values) {
182
+ this.assert.notDeepEqual(this.response.json(), values);
183
+ }
184
+ /**
185
+ * Assert body to be an array.
186
+ *
187
+ * @example
188
+ * ```js
189
+ * const body = { id: 1, name: 'post 1' }
190
+ *
191
+ * response.assertBodyIsArray() // fails
192
+ * ```
193
+ * @example
194
+ * ```js
195
+ * const body = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
196
+ *
197
+ * response.assertBodyIsArray() // passes
198
+ * ```
199
+ */
200
+ assertBodyIsArray() {
201
+ this.assert.isArray(this.response.json());
202
+ }
203
+ /**
204
+ * Assert body to not be an array.
205
+ *
206
+ * @example
207
+ * ```js
208
+ * const body = { id: 1, name: 'post 1' }
209
+ *
210
+ * response.assertBodyIsNotArray() // passes
211
+ * ```
212
+ * @example
213
+ * ```js
214
+ * const body = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
215
+ *
216
+ * response.assertBodyIsNotArray() // fails
217
+ * ```
218
+ */
219
+ assertBodyIsNotArray() {
220
+ this.assert.isNotArray(this.response.json());
221
+ }
222
+ /**
223
+ * Assert body to be an object.
224
+ *
225
+ * @example
226
+ * ```js
227
+ * const body = { id: 1, name: 'post 1' }
228
+ *
229
+ * response.assertBodyIsObject() // passes
230
+ * ```
231
+ * @example
232
+ * ```js
233
+ * const body = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
234
+ *
235
+ * response.assertBodyIsObject() // fails
236
+ * ```
237
+ */
238
+ assertBodyIsObject() {
239
+ this.assert.isObject(this.response.json());
240
+ }
241
+ /**
242
+ * Assert body to not be an object.
243
+ *
244
+ * @example
245
+ * ```js
246
+ * const body = { id: 1, name: 'post 1' }
247
+ *
248
+ * response.assertBodyIsNotObject() // fails
249
+ * ```
250
+ * @example
251
+ * ```js
252
+ * const body = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
253
+ *
254
+ * response.assertBodyIsNotObject() // passes
255
+ * ```
256
+ */
257
+ assertBodyIsNotObject() {
258
+ this.assert.isNotObject(this.response.json());
259
+ }
260
+ /**
261
+ * Assert header (array or object) to contain a subset of the expected value.
262
+ *
263
+ * @example
264
+ * ```js
265
+ * const header = { id: 1, name: 'post 1' }
266
+ *
267
+ * response.assertHeaderContains({ id: 1 }) // passes
268
+ * ```
269
+ * @example
270
+ * ```js
271
+ * const header = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
272
+ *
273
+ * response.assertHeaderContains([{ id: 1 }, { id: 2 }]) // passes
274
+ * ```
275
+ */
276
+ assertHeaderContains(values) {
277
+ this.assert.containsSubset(this.response.headers, values);
278
+ }
279
+ /**
280
+ * Assert header (array or object) to not contain a subset of the expected value.
281
+ *
282
+ * @example
283
+ * ```js
284
+ * const header = { id: 1, name: 'post 1' }
285
+ *
286
+ * response.assertHeaderContains({ id: 1 }) // passes
287
+ * ```
288
+ * @example
289
+ * ```js
290
+ * const header = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
291
+ *
292
+ * response.assertHeaderContains([{ id: 1 }, { id: 2 }]) // passes
293
+ * ```
294
+ */
295
+ assertHeaderNotContains(values) {
296
+ this.assert.notContainsSubset(this.response.headers, values);
297
+ }
298
+ /**
299
+ * Assert header (array or object) to be deep equal to the expected value.
300
+ *
301
+ * @example
302
+ * ```js
303
+ * const header = { id: 1, name: 'post 1' }
304
+ *
305
+ * response.assertHeaderDeepEqual({ id: 1 }) // fails
306
+ * ```
307
+ * @example
308
+ * ```js
309
+ * const header = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
310
+ *
311
+ * response.assertHeaderDeepEqual([{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]) // passes
312
+ * ```
313
+ */
314
+ assertHeaderDeepEqual(values) {
315
+ this.assert.deepEqual(this.response.headers, values);
316
+ }
317
+ /**
318
+ * Assert header (array or object) to be not deep equal to the expected value.
319
+ *
320
+ * @example
321
+ * ```js
322
+ * const header = { id: 1, name: 'post 1' }
323
+ *
324
+ * response.assertHeaderNotDeepEqual({ id: 1 }) // passes
325
+ * ```
326
+ * @example
327
+ * ```js
328
+ * const header = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
329
+ *
330
+ * response.assertHeaderNotDeepEqual([{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]) // fails
331
+ * ```
332
+ */
333
+ assertHeaderNotDeepEqual(values) {
334
+ this.assert.notDeepEqual(this.response.headers, values);
335
+ }
336
+ /**
337
+ * Assert header to contain a key.
338
+ *
339
+ * @example
340
+ * ```js
341
+ * const header = { id: 1, name: 'post 1' }
342
+ *
343
+ * response.assertHeaderContainsKey('id') // passes
344
+ * ```
345
+ */
346
+ assertHeaderContainsKey(key) {
347
+ this.assert.property(this.response.headers, key);
348
+ }
349
+ /**
350
+ * Assert header to not contain a key.
351
+ *
352
+ * @example
353
+ * ```js
354
+ * const body = { id: 1, name: 'post 1' }
355
+ *
356
+ * response.assertHeaderNotContainsKey('id') // fails
357
+ * ```
358
+ * @example
359
+ * ```js
360
+ * const body = { id: 1, name: 'post 1' }
361
+ *
362
+ * response.assertHeaderNotContainsKey('createdAt') // passes
363
+ * ```
364
+ */
365
+ assertHeaderNotContainsKey(key) {
366
+ this.assert.notProperty(this.response.headers, key);
367
+ }
368
+ /**
369
+ * Assert header to contain all keys.
370
+ *
371
+ * @example
372
+ * ```js
373
+ * const header = { id: 1, name: 'post 1' }
374
+ *
375
+ * response.assertHeaderContainsAllKeys(['id', 'post']) // passes
376
+ * ```
377
+ */
378
+ assertHeaderContainsAllKeys(keys) {
379
+ this.assert.properties(this.response.headers, keys);
380
+ }
381
+ /**
382
+ * Assert header to not contain all keys.
383
+ *
384
+ * @example
385
+ * ```js
386
+ * const header = { id: 1, name: 'post 1' }
387
+ *
388
+ * response.assertHeaderNotContainsAllKeys(['id']) // fails
389
+ * ```
390
+ * @example
391
+ * ```js
392
+ * const header = { id: 1, name: 'post 1' }
393
+ *
394
+ * response.assertHeaderNotContainsAllKeys(['createdAt']) // passes
395
+ * ```
396
+ */
397
+ assertHeaderNotContainsAllKeys(keys) {
398
+ this.assert.notAllProperties(this.response.headers, keys);
399
+ }
400
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * @athenna/http
3
+ *
4
+ * (c) João Lenon <lenon@athenna.io>
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+ import { TestRequest } from './Request/TestRequest.js';
10
+ declare module '@japa/runner' {
11
+ interface TestContext {
12
+ request: TestRequest;
13
+ }
14
+ }
15
+ export * from './Request/TestRequest.js';
16
+ export * from './Request/TestResponse.js';
17
+ /**
18
+ * Request plugin register the request macro to the test context.
19
+ */
20
+ export declare function request(): (_config: any, _runner: any, classes: any) => Promise<void>;
@@ -0,0 +1,19 @@
1
+ /**
2
+ * @athenna/http
3
+ *
4
+ * (c) João Lenon <lenon@athenna.io>
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+ import { TestRequest } from './Request/TestRequest.js';
10
+ export * from './Request/TestRequest.js';
11
+ export * from './Request/TestResponse.js';
12
+ /**
13
+ * Request plugin register the request macro to the test context.
14
+ */
15
+ export function request() {
16
+ return async function (_config, _runner, classes) {
17
+ classes.TestContext.macro('request', new TestRequest());
18
+ };
19
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@athenna/http",
3
- "version": "3.3.0",
3
+ "version": "3.4.0",
4
4
  "description": "The Athenna Http server. Built on top of fastify.",
5
5
  "license": "MIT",
6
6
  "author": "João Lenon <lenon@athenna.io>",
@@ -45,6 +45,7 @@
45
45
  "types": "./build/index.d.ts",
46
46
  "exports": {
47
47
  ".": "./build/index.js",
48
+ "./testing/plugins": "./build/Testing/Plugins/index.js",
48
49
  "./providers/HttpRouteProvider": "./build/Providers/HttpRouteProvider.js",
49
50
  "./providers/HttpServerProvider": "./build/Providers/HttpServerProvider.js",
50
51
  "./commands/RouteListCommand": "./build/Commands/RouteListCommand.js",