From 128599dde6a0706d56a1819d68ad50e86eb2ef3d Mon Sep 17 00:00:00 2001 From: Dom Harrington Date: Tue, 11 Sep 2018 15:17:17 -0700 Subject: [PATCH 1/2] Add support for x-headers extension https://readme.readme.io/docs/swagger-extensions#section-static-headers --- example/swagger-files/x-headers.json | 32 +++++++++++++++++++ .../__tests__/lib/oas-to-har.test.js | 13 ++++++++ packages/api-explorer/src/lib/oas-to-har.js | 10 ++++++ 3 files changed, 55 insertions(+) create mode 100644 example/swagger-files/x-headers.json diff --git a/example/swagger-files/x-headers.json b/example/swagger-files/x-headers.json new file mode 100644 index 000000000..95ec28ca6 --- /dev/null +++ b/example/swagger-files/x-headers.json @@ -0,0 +1,32 @@ +{ + "openapi": "3.0.0", + "servers": [ + { + "url": "http://httpbin.org" + } + ], + "info": { + "version": "1.0.0", + "title": "x-headers demo" + }, + "paths": { + "/post": { + "post": { + "summary": "Should send static x-headers with the request", + "description": "", + "parameters": [], + "responses": { + "200": { + "description": "Successful" + } + } + } + } + }, + "x-headers": [ + { + "key": "x-api-key", + "value": "static-value" + } + ] +} diff --git a/packages/api-explorer/__tests__/lib/oas-to-har.test.js b/packages/api-explorer/__tests__/lib/oas-to-har.test.js index 00c834490..9e5480f6f 100644 --- a/packages/api-explorer/__tests__/lib/oas-to-har.test.js +++ b/packages/api-explorer/__tests__/lib/oas-to-har.test.js @@ -1184,3 +1184,16 @@ describe('content-type & accept header', () => { ).toEqual([{ name: 'Content-Type', value: 'application/json' }]); }); }); + +describe('x-headers', () => { + it('should append any static headers to the request', () => { + expect(oasToHar({ + 'x-headers': [ + { + key: 'x-api-key', + value: '123456', + }, + ], + }).log.entries[0].request.headers).toEqual([{ name: 'x-api-key', value: '123456' }]) + }); +}); diff --git a/packages/api-explorer/src/lib/oas-to-har.js b/packages/api-explorer/src/lib/oas-to-har.js index e46be2815..67c59a30c 100644 --- a/packages/api-explorer/src/lib/oas-to-har.js +++ b/packages/api-explorer/src/lib/oas-to-har.js @@ -160,6 +160,16 @@ module.exports = ( }); } + // x-headers static headers + if (oas['x-headers']) { + oas['x-headers'].forEach((header) => { + har.headers.push({ + name: header.key, + value: String(header.value), + }); + }); + } + const schema = getSchema(pathOperation, oas) || { schema: {} }; function stringify(json) { From 9ea564a1494f261d5617a7473384e172b52fe3f9 Mon Sep 17 00:00:00 2001 From: Dom Harrington Date: Tue, 11 Sep 2018 15:19:32 -0700 Subject: [PATCH 2/2] Prettier --- .../__tests__/lib/oas-to-har.test.js | 18 ++++++++++-------- packages/api-explorer/src/lib/oas-to-har.js | 2 +- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/api-explorer/__tests__/lib/oas-to-har.test.js b/packages/api-explorer/__tests__/lib/oas-to-har.test.js index 9e5480f6f..57014230e 100644 --- a/packages/api-explorer/__tests__/lib/oas-to-har.test.js +++ b/packages/api-explorer/__tests__/lib/oas-to-har.test.js @@ -1187,13 +1187,15 @@ describe('content-type & accept header', () => { describe('x-headers', () => { it('should append any static headers to the request', () => { - expect(oasToHar({ - 'x-headers': [ - { - key: 'x-api-key', - value: '123456', - }, - ], - }).log.entries[0].request.headers).toEqual([{ name: 'x-api-key', value: '123456' }]) + expect( + oasToHar({ + 'x-headers': [ + { + key: 'x-api-key', + value: '123456', + }, + ], + }).log.entries[0].request.headers, + ).toEqual([{ name: 'x-api-key', value: '123456' }]); }); }); diff --git a/packages/api-explorer/src/lib/oas-to-har.js b/packages/api-explorer/src/lib/oas-to-har.js index 67c59a30c..cc4c22e8c 100644 --- a/packages/api-explorer/src/lib/oas-to-har.js +++ b/packages/api-explorer/src/lib/oas-to-har.js @@ -162,7 +162,7 @@ module.exports = ( // x-headers static headers if (oas['x-headers']) { - oas['x-headers'].forEach((header) => { + oas['x-headers'].forEach(header => { har.headers.push({ name: header.key, value: String(header.value),