diff --git a/src/execute.js b/src/execute.js index 78f23c92f..0d5d4cdda 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 @@ -127,6 +123,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 30ee60613..71f6998e3 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 add content-type multipart/form-data when param type is file and no other sources of consumes', 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 = {