From 7f8a774d30185edc20e9d3de64063e810153c6dc Mon Sep 17 00:00:00 2001 From: BTMPL Date: Tue, 23 Jan 2018 08:00:22 +0100 Subject: [PATCH 1/3] Allow functions as Access-Control-Allow headers --- lib/controller/MockController.js | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/controller/MockController.js b/lib/controller/MockController.js index 44182fb..8c33fc7 100644 --- a/lib/controller/MockController.js +++ b/lib/controller/MockController.js @@ -775,10 +775,26 @@ MockController.prototype = extend(MockController.prototype, { res.setHeader(key, value); }); res.setHeader('Content-Type', this.options.contentType); - res.setHeader('Access-Control-Expose-Headers', this.options.accessControlExposeHeaders); - res.setHeader('Access-Control-Allow-Origin', this.options.accessControlAllowOrigin); - res.setHeader('Access-Control-Allow-Methods', this.options.accessControlAllowMethods); - res.setHeader('Access-Control-Allow-Headers', this.options.accessControlAllowHeaders); + res.setHeader('Access-Control-Expose-Headers', + typeof this.options.accessControlExposeHeaders === 'function' ? + this.options.accessControlExposeHeaders(res.req) : + this.options.accessControlExposeHeaders + ); + res.setHeader('Access-Control-Allow-Origin', + typeof this.options.accessControlAllowOrigin === 'function' ? + this.options.accessControlAllowOrigin(res.req) : + this.options.accessControlAllowOrigin + ); + res.setHeader('Access-Control-Allow-Methods', + typeof this.options.accessControlAllowMethods === 'function' ? + this.options.accessControlAllowMethods(res.req) : + this.options.accessControlAllowMethods + ); + res.setHeader('Access-Control-Allow-Headers', + typeof this.options.accessControlAllowHeaders === 'function' ? + this.options.accessControlAllowHeaders(res.req) : + this.options.accessControlAllowHeaders + ); }, }); From 6ebd1d4821e9f9d88fbff0b3d747bd65a8603499 Mon Sep 17 00:00:00 2001 From: BTMPL Date: Tue, 23 Jan 2018 08:06:23 +0100 Subject: [PATCH 2/3] Updated docs to mention function value for ACA headers --- doc/readme-options.md | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/doc/readme-options.md b/doc/readme-options.md index 7b9f4e5..fa6839a 100644 --- a/doc/readme-options.md +++ b/doc/readme-options.md @@ -79,29 +79,33 @@ A string that define the header "Content-Type". #### options.accessControlExposeHeaders -Type: `String` +Type: `String` or `function` Default value: `X-Total-Count` -A string that define the header "Access-Control-Expose-Headers". +A string that define the header "Access-Control-Expose-Headers". If a function +is used, it will be called with the request object as the only parameter. #### options.accessControlAllowOrigin -Type: `String` +Type: `String` or `function` Default value: `*` -A string that define the header "Access-Control-Allow-Origin". +A string that define the header "Access-Control-Allow-Origin". If a function +is used, it will be called with the request object as the only parameter. #### options.accessControlAllowMethods -Type: `String` +Type: `String` or `function` Default value: `GET, POST, PUT, OPTIONS, DELETE, PATCH, HEAD` -A string that define the header "Access-Control-Allow-Methods". +A string that define the header "Access-Control-Allow-Methods". If a function +is used, it will be called with the request object as the only parameter. #### options.accessControlAllowHeaders -Type: `String` +Type: `String` or `function` Default value: `origin, x-requested-with, content-type` -A string that define the header "Access-Control-Allow-Headers". +A string that define the header "Access-Control-Allow-Headers". If a function +is used, it will be called with the request object as the only parameter. #### options.middleware Type: `Object` From d426a4125ff569bc1bfa981e0a5e41aa41a25a2a Mon Sep 17 00:00:00 2001 From: Simon Mollweide Date: Tue, 23 Jan 2018 20:46:29 +0100 Subject: [PATCH 3/3] [setup] bump up version number --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1e85ad3..451ff15 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-mock-server", - "version": "0.20.0", + "version": "0.21.0", "description": "File based Node REST API mock server", "email": "simon.mollweide@web.de", "author": "Simon Mollweide ",