@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.
@@ -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?: Record<string, string>;
8
+ headers?: any;
9
9
  }
10
10
  export interface RequestParameters {
11
11
  query: any;
@@ -157,8 +157,13 @@ const appFactory = (runtimeCollection) => {
157
157
  res.status(status);
158
158
  }
159
159
  if (method.headers) {
160
- const processedHeaders = Object.keys(method.headers).reduce((acc, curr) => (Object.assign(Object.assign({}, acc), { [curr]: handlebars_1.default.compile(method.headers[curr])(tokenParams) })), {});
161
- res.set(processedHeaders);
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@centeredgesoft/runtime-mock-routes",
3
- "version": "2.8.2",
3
+ "version": "2.9.0",
4
4
  "description": "",
5
5
  "main": "build/index.js",
6
6
  "scripts": {
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?: Record<string, string>;
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
- const processedHeaders = Object.keys(method.headers).reduce(
209
- (acc, curr) => ({
210
- ...acc,
211
- [curr]: Handlebars.compile(method.headers[curr])(tokenParams)
212
- }),
213
- {} as Record<string, string>
214
- );
215
- res.set(processedHeaders);
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;