@centeredgesoft/runtime-mock-routes 2.1.0 → 2.5.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.
package/src/appV2.test.ts CHANGED
@@ -1,262 +1,321 @@
1
- import request from 'supertest';
2
- import { appFactory, isRuntimeRequestCollection, RuntimeRequestBody, RuntimeRequestCollection, SupportedMethodsColection } from './appV2';
3
-
4
- describe('appFactory', () => {
5
- test('Creates app with no seed', () => {
6
- const app = appFactory();
7
- expect(app).toBeDefined();
8
- });
9
-
10
- test('Creates app with empty seed', () => {
11
- const app = appFactory({});
12
- expect(app).toBeDefined();
13
- });
14
-
15
- test('Creates app with properly configured seed', () => {
16
- const seed: RuntimeRequestCollection = {
17
- "/test": {
18
- path: "/test",
19
- methods: {
20
- GET: {
21
- body: "Hello",
22
- status: 200
23
- }
24
- }
25
- }
26
- }
27
- const app = appFactory(seed);
28
- expect(app).toBeDefined();
29
- expect(app["runtimeRequestCollection"]).toEqual(seed);
30
- });
31
-
32
- test('Creates app with semi-properly configured seed', () => {
33
- const seed: RuntimeRequestCollection = {
34
- "/test": {
35
- path: "/test",
36
- methods: {
37
- GET: {
38
- body: "Hello",
39
- status: 200
40
- }
41
- }
42
- }
43
- }
44
- const semiSeed: RuntimeRequestCollection = {
45
- "/test": {
46
- path: "",
47
- methods: {
48
- GET: {
49
- body: "Hello",
50
- status: 200
51
- }
52
- }
53
- }
54
- }
55
- const app = appFactory(semiSeed);
56
- expect(app).toBeDefined();
57
- expect(app["runtimeRequestCollection"]).toEqual(seed);
58
- });
59
-
60
- test('throws error on improperly configured seed', () => {
61
- const seed = {
62
- "/test": {
63
- path: "/test",
64
- }
65
- }
66
-
67
- expect(() => appFactory(seed as any)).toThrowError('Intial requests JSON is invalid')
68
- });
69
- });
70
-
71
- describe('GET /', () => {
72
- const empty: RuntimeRequestCollection = {};
73
- const seeded: RuntimeRequestCollection = {
74
- "/test": {
75
- path: "/test",
76
- methods: {
77
- GET: {
78
- body: "Hello",
79
- status: 200
80
- }
81
- }
82
- }
83
- }
84
- test.each([['empty', empty], ['seeded', seeded]])('should get current collection - %s', async (_, seed) => {
85
- const app = appFactory(seed);
86
- const response = await request(app).get('/')
87
- .expect('Content-Type', /json/)
88
- .expect(200);
89
-
90
- expect(response.body).toEqual(app["runtimeRequestCollection"]);
91
- })
92
- });
93
-
94
- describe('POST /', () => {
95
- test('should accept a valid RuntimeRequestBody', async () => {
96
- const app = appFactory();
97
- const reqeustBody: RuntimeRequestBody = {
98
- path: '/test',
99
- methods: {
100
- POST: {
101
- body: { hello: 'world' }
102
- }
103
- }
104
- };
105
- await request(app)
106
- .post('/')
107
- .send(reqeustBody)
108
- .expect(204);
109
-
110
- expect(app["runtimeRequestCollection"]).toEqual(expect.objectContaining({
111
- [reqeustBody.path]: reqeustBody
112
- }));
113
- });
114
-
115
- test('should deny an invalid RuntimeRequestBody', async () => {
116
- const app = appFactory();
117
- const reqeustBody = {
118
- path: '/test',
119
- };
120
- await request(app)
121
- .post('/')
122
- .send(reqeustBody)
123
- .expect(400)
124
- .catch(() => { });
125
- });
126
- });
127
-
128
- describe('PUT', () => {
129
- test('should accept a valid RuntimeRequestCollection', async () => {
130
- const app = appFactory();
131
- const requestBody: RuntimeRequestBody = {
132
- path: '/test',
133
- methods: {
134
- POST: {
135
- body: { hello: 'world' }
136
- }
137
- }
138
- };
139
- const collection: RuntimeRequestCollection = {
140
- [requestBody.path]: requestBody
141
- }
142
- await request(app)
143
- .put('/')
144
- .send(collection)
145
- .expect(204);
146
-
147
- expect(app["runtimeRequestCollection"]).toEqual(collection);
148
- });
149
-
150
- test('should deny an invalid RuntimeRequestCollection', async () => {
151
- const app = appFactory();
152
- const reqeustBody = {
153
- path: '/test',
154
- };
155
- const collection = {
156
- [reqeustBody.path]: reqeustBody
157
- }
158
- await request(app)
159
- .put('/')
160
- .send(collection)
161
- .expect(400)
162
- .catch(() => { });
163
- });
164
-
165
-
166
- });
167
-
168
- describe('DELETE /?path=<path>', () => {
169
- test('should remove an existing path', async () => {
170
- const body: RuntimeRequestBody = {
171
- path: '/test',
172
- methods: {
173
- GET: {
174
- body: {}
175
- }
176
- }
177
- };
178
- const collection: RuntimeRequestCollection = {
179
- [body.path]: body
180
- };
181
- const app = appFactory(collection);
182
- await request(app)
183
- .delete('/')
184
- .query({ path: [body.path] })
185
- .expect(204);
186
-
187
-
188
- expect(app["runtimeRequestCollection"]).toEqual(expect.not.objectContaining({
189
- [body.path]: expect.objectContaining(body)
190
- }));
191
- })
192
-
193
- test('should return 204 when nothing needs to be removed', async () => {
194
- const body: RuntimeRequestBody = {
195
- path: '/test',
196
- methods: {
197
- GET: {
198
- body: {}
199
- }
200
- }
201
- };
202
- const collection: RuntimeRequestCollection = {
203
-
204
- };
205
- const app = appFactory(collection);
206
- await request(app)
207
- .delete('/')
208
- .query({ path: [body.path] })
209
- .expect(204);
210
-
211
-
212
- expect(app["runtimeRequestCollection"]).toEqual(expect.not.objectContaining({
213
- [body.path]: body
214
- }));
215
- })
216
- });
217
-
218
- describe('METHOD /*', () => {
219
- const methods = SupportedMethodsColection.map(m => [m])
220
- test.each(methods)('%s should render JSON hbs template', async (method) => {
221
- const requestBody: RuntimeRequestBody = {
222
- path: '/test/:id',
223
- methods: {
224
- [method]: {
225
- body: `{"id": {{params.id}} }`
226
- }
227
- }
228
- }
229
-
230
- const collection: RuntimeRequestCollection = {
231
- [requestBody.path]: requestBody
232
- };
233
- const id = 42;
234
-
235
- const app = appFactory(collection);
236
-
237
- const response = await request(app)[method.toLocaleLowerCase()](`/test/${id}`)
238
- .expect(200)
239
- .expect('Content-Type', /json/);
240
-
241
- expect(response.body).toEqual({ id })
242
- });
243
- });
244
-
245
- describe('isRuntimeRequestMethodBodyCollection', () => {
246
- test('is true for valid object', () => {
247
- const seed: RuntimeRequestCollection = {
248
- "/test": {
249
- path: "/test",
250
- methods: {
251
- GET: {
252
- body: "Hello",
253
- status: 200
254
- }
255
- }
256
- }
257
- }
258
-
259
- const valid = isRuntimeRequestCollection(seed);
260
- expect(valid).toBeTruthy();
261
- })
1
+ import request from 'supertest';
2
+ import { appFactory, isRuntimeRequestCollection, RuntimeRequestBody, RuntimeRequestCollection, SupportedMethodsColection, RequestParameters } from './appV2';
3
+
4
+ describe('appFactory', () => {
5
+ test('Creates app with no seed', () => {
6
+ const app = appFactory();
7
+ expect(app).toBeDefined();
8
+ });
9
+
10
+ test('Creates app with empty seed', () => {
11
+ const app = appFactory({});
12
+ expect(app).toBeDefined();
13
+ });
14
+
15
+ test('Creates app with properly configured seed', () => {
16
+ const seed: RuntimeRequestCollection = {
17
+ "/test": {
18
+ path: "/test",
19
+ methods: {
20
+ GET: {
21
+ body: "Hello",
22
+ status: 200
23
+ }
24
+ }
25
+ }
26
+ }
27
+ const app = appFactory(seed);
28
+ expect(app).toBeDefined();
29
+ expect(app["runtimeRequestCollection"]).toEqual(seed);
30
+ });
31
+
32
+ test('Creates app with semi-properly configured seed', () => {
33
+ const seed: RuntimeRequestCollection = {
34
+ "/test": {
35
+ path: "/test",
36
+ methods: {
37
+ GET: {
38
+ body: "Hello",
39
+ status: 200
40
+ }
41
+ }
42
+ }
43
+ }
44
+ const semiSeed: RuntimeRequestCollection = {
45
+ "/test": {
46
+ path: "",
47
+ methods: {
48
+ GET: {
49
+ body: "Hello",
50
+ status: 200
51
+ }
52
+ }
53
+ }
54
+ }
55
+ const app = appFactory(semiSeed);
56
+ expect(app).toBeDefined();
57
+ expect(app["runtimeRequestCollection"]).toEqual(seed);
58
+ });
59
+
60
+ test('throws error on improperly configured seed', () => {
61
+ const seed = {
62
+ "/test": {
63
+ path: "/test",
64
+ }
65
+ }
66
+
67
+ expect(() => appFactory(seed as any)).toThrowError('Intial requests JSON is invalid')
68
+ });
69
+ });
70
+
71
+ describe('GET /', () => {
72
+ const empty: RuntimeRequestCollection = {};
73
+ const seeded: RuntimeRequestCollection = {
74
+ "/test": {
75
+ path: "/test",
76
+ methods: {
77
+ GET: {
78
+ body: "Hello",
79
+ status: 200
80
+ }
81
+ }
82
+ }
83
+ }
84
+ test.each([['empty', empty], ['seeded', seeded]])('should get current collection - %s', async (_, seed) => {
85
+ const app = appFactory(seed);
86
+ const response = await request(app).get('/')
87
+ .expect('Content-Type', /json/)
88
+ .expect(200);
89
+
90
+ expect(response.body).toEqual(app["runtimeRequestCollection"]);
91
+ })
92
+ });
93
+
94
+ describe('POST /', () => {
95
+ test('should accept a valid RuntimeRequestBody', async () => {
96
+ const app = appFactory();
97
+ const reqeustBody: RuntimeRequestBody = {
98
+ path: '/test',
99
+ methods: {
100
+ POST: {
101
+ body: { hello: 'world' }
102
+ }
103
+ }
104
+ };
105
+ await request(app)
106
+ .post('/')
107
+ .send(reqeustBody)
108
+ .expect(204);
109
+
110
+ expect(app["runtimeRequestCollection"]).toEqual(expect.objectContaining({
111
+ [reqeustBody.path]: reqeustBody
112
+ }));
113
+ });
114
+
115
+ test('should deny an invalid RuntimeRequestBody', async () => {
116
+ const app = appFactory();
117
+ const reqeustBody = {
118
+ path: '/test',
119
+ };
120
+ await request(app)
121
+ .post('/')
122
+ .send(reqeustBody)
123
+ .expect(400)
124
+ .catch(() => { });
125
+ });
126
+ });
127
+
128
+ describe('PUT', () => {
129
+ test('should accept a valid RuntimeRequestCollection', async () => {
130
+ const app = appFactory();
131
+ const requestBody: RuntimeRequestBody = {
132
+ path: '/test',
133
+ methods: {
134
+ POST: {
135
+ body: { hello: 'world' }
136
+ }
137
+ }
138
+ };
139
+ const collection: RuntimeRequestCollection = {
140
+ [requestBody.path]: requestBody
141
+ }
142
+ await request(app)
143
+ .put('/')
144
+ .send(collection)
145
+ .expect(204);
146
+
147
+ expect(app["runtimeRequestCollection"]).toEqual(collection);
148
+ });
149
+
150
+ test('should deny an invalid RuntimeRequestCollection', async () => {
151
+ const app = appFactory();
152
+ const reqeustBody = {
153
+ path: '/test',
154
+ };
155
+ const collection = {
156
+ [reqeustBody.path]: reqeustBody
157
+ }
158
+ await request(app)
159
+ .put('/')
160
+ .send(collection)
161
+ .expect(400)
162
+ .catch(() => { });
163
+ });
164
+
165
+
166
+ });
167
+
168
+ describe('DELETE /?path=<path>', () => {
169
+ test('should remove an existing path', async () => {
170
+ const body: RuntimeRequestBody = {
171
+ path: '/test',
172
+ methods: {
173
+ GET: {
174
+ body: {}
175
+ }
176
+ }
177
+ };
178
+ const collection: RuntimeRequestCollection = {
179
+ [body.path]: body
180
+ };
181
+ const app = appFactory(collection);
182
+ await request(app)
183
+ .delete('/')
184
+ .query({ path: [body.path] })
185
+ .expect(204);
186
+
187
+
188
+ expect(app["runtimeRequestCollection"]).toEqual(expect.not.objectContaining({
189
+ [body.path]: expect.objectContaining(body)
190
+ }));
191
+ })
192
+
193
+ test('should return 204 when nothing needs to be removed', async () => {
194
+ const body: RuntimeRequestBody = {
195
+ path: '/test',
196
+ methods: {
197
+ GET: {
198
+ body: {}
199
+ }
200
+ }
201
+ };
202
+ const collection: RuntimeRequestCollection = {
203
+
204
+ };
205
+ const app = appFactory(collection);
206
+ await request(app)
207
+ .delete('/')
208
+ .query({ path: [body.path] })
209
+ .expect(204);
210
+
211
+
212
+ expect(app["runtimeRequestCollection"]).toEqual(expect.not.objectContaining({
213
+ [body.path]: body
214
+ }));
215
+ })
216
+ });
217
+
218
+ describe('METHOD /*', () => {
219
+ const methods = SupportedMethodsColection.map(m => [m])
220
+ test.each(methods)('%s should render JSON hbs template', async (method) => {
221
+ const requestBody: RuntimeRequestBody = {
222
+ path: '/test/:id',
223
+ methods: {
224
+ [method]: {
225
+ body: `{"id": {{params.id}} }`
226
+ }
227
+ }
228
+ }
229
+
230
+ const collection: RuntimeRequestCollection = {
231
+ [requestBody.path]: requestBody
232
+ };
233
+ const id = 42;
234
+
235
+ const app = appFactory(collection);
236
+
237
+ const response = await request(app)[method.toLocaleLowerCase()](`/test/${id}`)
238
+ .expect(200)
239
+ .expect('Content-Type', /json/);
240
+
241
+ expect(response.body).toEqual({ id })
242
+ });
243
+ });
244
+
245
+ describe('Body as Function', () => {
246
+ test('executes the method for the result', async () => {
247
+ const seed: RuntimeRequestCollection = {
248
+ "/test": {
249
+ path: "/test",
250
+ methods: {
251
+ GET: {
252
+ body: function(rp: RequestParameters) {
253
+ if(rp.query.id == "2") {
254
+ return {id: 2};
255
+ } else {
256
+ return {id: 3};
257
+ }
258
+ },
259
+ status: 200
260
+ }
261
+ }
262
+ }
263
+ }
264
+
265
+ const app = appFactory(seed);
266
+ expect(app).toBeDefined();
267
+
268
+ var response = await request(app).get('/test?id=2');
269
+
270
+ expect(response.body).toEqual({id: 2});
271
+
272
+ var secondResponse = await request(app).get('/test?id=3');
273
+
274
+ expect(secondResponse.body).toEqual({id: 3});
275
+ })
276
+ });
277
+
278
+ describe('Request Body is Simple Type', () => {
279
+ test('Send in string as JSON', async () => {
280
+ const seed: RuntimeRequestCollection = {
281
+ "/test": {
282
+ path: "/test",
283
+ methods: {
284
+ POST: {
285
+ body: JSON.stringify(true),
286
+ status: 200
287
+ }
288
+ }
289
+ }
290
+ }
291
+
292
+ const app = appFactory(seed);
293
+ expect(app).toBeDefined();
294
+
295
+ var response = await request(app)
296
+ .post('/test?id=2')
297
+ .set('Content-type', 'application/json')
298
+ .send('"test"');
299
+
300
+ expect(response.body).toEqual(true);
301
+ })
302
+ });
303
+
304
+ describe('isRuntimeRequestMethodBodyCollection', () => {
305
+ test('is true for valid object', () => {
306
+ const seed: RuntimeRequestCollection = {
307
+ "/test": {
308
+ path: "/test",
309
+ methods: {
310
+ GET: {
311
+ body: "Hello",
312
+ status: 200
313
+ }
314
+ }
315
+ }
316
+ }
317
+
318
+ const valid = isRuntimeRequestCollection(seed);
319
+ expect(valid).toBeTruthy();
320
+ })
262
321
  })