@athenna/http 3.2.1 → 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,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.2.1",
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",
@@ -63,8 +64,8 @@
63
64
  "fastify": "^4.13.0"
64
65
  },
65
66
  "devDependencies": {
66
- "@athenna/artisan": "^3.2.0",
67
- "@athenna/common": "^3.3.1",
67
+ "@athenna/artisan": "^3.3.0",
68
+ "@athenna/common": "^3.3.3",
68
69
  "@athenna/config": "^3.2.0",
69
70
  "@athenna/ioc": "^3.1.5",
70
71
  "@athenna/logger": "^3.1.5",
@@ -125,7 +126,8 @@
125
126
  "exclude": [
126
127
  "src/Types/*",
127
128
  "src/Contracts/*",
128
- "src/Exceptions/*"
129
+ "src/Exceptions/*",
130
+ "src/Commands/*"
129
131
  ],
130
132
  "reporter": [
131
133
  "text-summary",
@@ -262,7 +264,11 @@
262
264
  "#tests/Stubs/middlewares/DecoratedGlobalTerminator"
263
265
  ],
264
266
  "commandsManifest": {
265
- "route:list": "#src/Commands/RouteListCommand",
267
+ "route:list": {
268
+ "path": "#src/Commands/RouteListCommand",
269
+ "route": "./tests/Stubs/routes/http.js",
270
+ "kernel": "./tests/Stubs/kernels/HttpKernel.js"
271
+ },
266
272
  "make:controller": "#src/Commands/MakeControllerCommand",
267
273
  "make:interceptor": "#src/Commands/MakeInterceptorCommand",
268
274
  "make:middleware": "#src/Commands/MakeMiddlewareCommand",