From 38f6ce740a58372be028bd5e1caa18f12d5fb5d9 Mon Sep 17 00:00:00 2001 From: Anna Bodnia Date: Fri, 14 Apr 2017 17:09:21 +0300 Subject: [PATCH 1/3] fix consumes #995 --- src/execute.js | 20 +++++-- test/execute.js | 137 ++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 150 insertions(+), 7 deletions(-) diff --git a/src/execute.js b/src/execute.js index 2d2dbdda3..d38e42cdd 100644 --- a/src/execute.js +++ b/src/execute.js @@ -93,9 +93,9 @@ export function buildRequest({ req.headers.accept = responseContentType } - if (requestContentType) { - req.headers['content-type'] = requestContentType - } + // if (requestContentType) { + // req.headers['content-type'] = requestContentType + // } // Add values to request arrayOrEmpty(operation.parameters) // operation parameters @@ -127,6 +127,20 @@ export function buildRequest({ req = applySecurities({request: req, securities, operation, spec}) // Will add the query object into the URL, if it exists mergeInQueryOrForm(req) + + if (req.body || req.form) { + if (requestContentType) { + req.headers['content-type'] = requestContentType + } else if (Array.isArray(operation.consumes)) { + req.headers['content-type'] = operation.consumes[0] + } else if (Array.isArray(spec.consumes)) { + req.headers['content-type'] = spec.consumes[0] + } else if (operation.parameters.filter((p)=> p.type === "file").length) { + req.headers['content-type'] = "multipart/form-data" + } else if (operation.parameters.filter((p)=> p.in === "formData").length) { + req.headers['content-type'] = "application/x-www-form-urlencoded" + } + } return req } diff --git a/test/execute.js b/test/execute.js index 9ba4fbc85..6ebce193e 100644 --- a/test/execute.js +++ b/test/execute.js @@ -441,7 +441,7 @@ describe('execute', () => { }) }) - it('should handle requestContentType', function () { + it('should not add content-type with no form-data or body param', function () { // Given const spec = { host: 'swagger.io', @@ -454,14 +454,143 @@ describe('execute', () => { // Then expect(req).toEqual({ url: 'http://swagger.io/one', - headers: { - 'content-type': 'application/josh', - }, + headers: {}, credentials: 'same-origin', method: 'GET' }) }) + it('should not add content-type with no form-data or body param', function () { + // Given + const spec = { + host: 'swagger.io', + paths: { + '/one': { + post: { + operationId: 'postMe', + parameters: [{name: 'file', type: 'file', 'in': "formData"}] + } + } + } + } + + // When + const req = buildRequest({ + spec, + operationId: 'postMe', + parameters: { file: 'test'}}) + + // Then + expect(req).toEqual({ + method: "POST", + "body": "file=test", + url: 'http://swagger.io/one', + headers: { + "content-type": "multipart/form-data" + }, + credentials: 'same-origin' + }) + }) + + it('should add content-type application/x-www-form-urlencoded when in: formData ', function () { + // Given + const spec = { + host: 'swagger.io', + paths: { + '/one': { + post: { + operationId: 'postMe', + parameters: [{name: 'file', in: 'formData'}] + } + } + } + } + + // When + const req = buildRequest({ + spec, + operationId: 'postMe', + parameters: { file: 'test'}}) + + // Then + expect(req).toEqual({ + body: "file=test", + method: "POST", + url: 'http://swagger.io/one', + headers: { + "content-type": "application/x-www-form-urlencoded" + }, + credentials: 'same-origin' + }) + }) + + it('should add content-type from spec when no consumes in operation and no requestContentType passed', function () { + // Given + const spec = { + host: 'swagger.io', + consumes: ["test"], + paths: { + '/one': { + post: { + operationId: 'postMe', + parameters: [{name: 'file', in: 'formData'}] + } + } + } + } + + // When + const req = buildRequest({ + spec, + operationId: 'postMe', + parameters: { file: 'test'}}) + + // Then + expect(req).toEqual({ + body: "file=test", + method: "POST", + url: 'http://swagger.io/one', + headers: { + "content-type": "test" + }, + credentials: 'same-origin' + }) + }) + + it('should add content-type from operation when no requestContentType passed', function () { + // Given + const spec = { + host: 'swagger.io', + consumes: ["no"], + paths: { + '/one': { + post: { + operationId: 'postMe', + consumes: ["test"], + parameters: [{name: 'file', in: 'formData'}] + } + } + } + } + + // When + const req = buildRequest({ + spec, + operationId: 'postMe', + parameters: { file: 'test'}}) + + // Then + expect(req).toEqual({ + body: "file=test", + method: "POST", + url: 'http://swagger.io/one', + headers: { + "content-type": "test" + }, + credentials: 'same-origin' + }) + }) + it('should build a request for all given fields', function () { // Given const spec = { From 7508f5392fcba69b977b81d6a38e04f80bbc2ab9 Mon Sep 17 00:00:00 2001 From: Anna Bodnia Date: Fri, 14 Apr 2017 17:12:02 +0300 Subject: [PATCH 2/3] fix consumes 995 removed commented code --- src/execute.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/execute.js b/src/execute.js index d38e42cdd..dfff41e2c 100644 --- a/src/execute.js +++ b/src/execute.js @@ -93,10 +93,6 @@ export function buildRequest({ req.headers.accept = responseContentType } - // if (requestContentType) { - // req.headers['content-type'] = requestContentType - // } - // Add values to request arrayOrEmpty(operation.parameters) // operation parameters .concat(arrayOrEmpty(spec.paths[pathName].parameters)) // path parameters From 8bfaa5428132d12a41569501fd5553f11639cbf5 Mon Sep 17 00:00:00 2001 From: Anna Bodnia Date: Fri, 14 Apr 2017 17:36:01 +0300 Subject: [PATCH 3/3] correct test wording --- test/execute.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/execute.js b/test/execute.js index 6ebce193e..a694102ae 100644 --- a/test/execute.js +++ b/test/execute.js @@ -460,7 +460,7 @@ describe('execute', () => { }) }) - it('should not add content-type with no form-data or body param', function () { + it('should add content-type multipart/form-data when param type is file and no other sources of consumes', function () { // Given const spec = { host: 'swagger.io',