@centeredgesoft/runtime-mock-routes 2.8.2 → 2.9.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/build/src/appV2.d.ts +1 -1
- package/build/src/appV2.js +7 -2
- package/package.json +1 -1
- package/src/appV2.test.ts +45 -0
- package/src/appV2.ts +13 -9
package/build/src/appV2.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export declare const isSupportedMethod: (obj: any) => obj is "GET" | "POST" | "P
|
|
|
5
5
|
export interface RuntimeRequestMethodBody {
|
|
6
6
|
body: any;
|
|
7
7
|
status?: any;
|
|
8
|
-
headers?:
|
|
8
|
+
headers?: any;
|
|
9
9
|
}
|
|
10
10
|
export interface RequestParameters {
|
|
11
11
|
query: any;
|
package/build/src/appV2.js
CHANGED
|
@@ -157,8 +157,13 @@ const appFactory = (runtimeCollection) => {
|
|
|
157
157
|
res.status(status);
|
|
158
158
|
}
|
|
159
159
|
if (method.headers) {
|
|
160
|
-
|
|
161
|
-
|
|
160
|
+
if (typeof method.headers === 'function') {
|
|
161
|
+
res.set(method.headers(tokenParams));
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
const processedHeaders = Object.keys(method.headers).reduce((acc, curr) => (Object.assign(Object.assign({}, acc), { [curr]: handlebars_1.default.compile(method.headers[curr])(tokenParams) })), {});
|
|
165
|
+
res.set(processedHeaders);
|
|
166
|
+
}
|
|
162
167
|
}
|
|
163
168
|
let { body } = method;
|
|
164
169
|
// if body is a function, execute and then process the response
|
package/package.json
CHANGED
package/src/appV2.test.ts
CHANGED
|
@@ -370,3 +370,48 @@ describe('Status as a function', () => {
|
|
|
370
370
|
expect(secondResponse.status).toEqual(400);
|
|
371
371
|
})
|
|
372
372
|
})
|
|
373
|
+
|
|
374
|
+
describe('Headers as a function', () => {
|
|
375
|
+
test('is true for headers that are a function', () => {
|
|
376
|
+
const seed: RuntimeRequestMethodBody = {
|
|
377
|
+
body: "test",
|
|
378
|
+
status: 200,
|
|
379
|
+
headers: () => {
|
|
380
|
+
return {'test-header': 5}
|
|
381
|
+
}
|
|
382
|
+
};
|
|
383
|
+
|
|
384
|
+
const valid = isRuntimeRequestMethodBody(seed);
|
|
385
|
+
expect(valid).toBeTruthy();
|
|
386
|
+
})
|
|
387
|
+
|
|
388
|
+
test('Executes header functions', async () => {
|
|
389
|
+
const seed: RuntimeRequestCollection = {
|
|
390
|
+
"/test": {
|
|
391
|
+
path: "/test",
|
|
392
|
+
methods: {
|
|
393
|
+
GET: {
|
|
394
|
+
body: {id: 1},
|
|
395
|
+
status: 200,
|
|
396
|
+
headers: function(rp: RequestParameters) {
|
|
397
|
+
if(rp.query.id == "2") {
|
|
398
|
+
return {'test-header': '2'};
|
|
399
|
+
} else {
|
|
400
|
+
return {'test-header': '3'}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
const app = appFactory(seed);
|
|
409
|
+
expect(app).toBeDefined();
|
|
410
|
+
|
|
411
|
+
var response = await request(app).get('/test?id=2');
|
|
412
|
+
expect(response.headers['test-header']).toEqual('2');
|
|
413
|
+
|
|
414
|
+
var secondResponse = await request(app).get('/test?id=3');
|
|
415
|
+
expect(secondResponse.headers['test-header']).toEqual('3');
|
|
416
|
+
})
|
|
417
|
+
})
|
package/src/appV2.ts
CHANGED
|
@@ -36,7 +36,7 @@ export const isSupportedMethod = (obj: any): obj is SupportedMethod => Supported
|
|
|
36
36
|
export interface RuntimeRequestMethodBody {
|
|
37
37
|
body: any;
|
|
38
38
|
status?: any;
|
|
39
|
-
headers?:
|
|
39
|
+
headers?: any;
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
export interface RequestParameters {
|
|
@@ -205,14 +205,18 @@ export const appFactory = (runtimeCollection?: RuntimeRequestCollection) => {
|
|
|
205
205
|
}
|
|
206
206
|
|
|
207
207
|
if (method.headers) {
|
|
208
|
-
|
|
209
|
-
(
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
208
|
+
if (typeof method.headers === 'function') {
|
|
209
|
+
res.set(method.headers(tokenParams))
|
|
210
|
+
} else {
|
|
211
|
+
const processedHeaders = Object.keys(method.headers).reduce(
|
|
212
|
+
(acc, curr) => ({
|
|
213
|
+
...acc,
|
|
214
|
+
[curr]: Handlebars.compile(method.headers[curr])(tokenParams)
|
|
215
|
+
}),
|
|
216
|
+
{} as Record<string, string>
|
|
217
|
+
);
|
|
218
|
+
res.set(processedHeaders);
|
|
219
|
+
}
|
|
216
220
|
}
|
|
217
221
|
|
|
218
222
|
let { body } = method;
|