Skip to content

Commit 1b90a87

Browse files
authored
Merge pull request #1016 from bodnia/issue-995
Issue 995
2 parents 7558cad + d64efd9 commit 1b90a87

File tree

2 files changed

+147
-8
lines changed

2 files changed

+147
-8
lines changed

src/execute.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,6 @@ export function buildRequest({
9393
req.headers.accept = responseContentType
9494
}
9595

96-
if (requestContentType) {
97-
req.headers['content-type'] = requestContentType
98-
}
99-
10096
// Add values to request
10197
arrayOrEmpty(operation.parameters) // operation parameters
10298
.concat(arrayOrEmpty(spec.paths[pathName].parameters)) // path parameters
@@ -127,6 +123,20 @@ export function buildRequest({
127123
req = applySecurities({request: req, securities, operation, spec})
128124
// Will add the query object into the URL, if it exists
129125
mergeInQueryOrForm(req)
126+
127+
if (req.body || req.form) {
128+
if (requestContentType) {
129+
req.headers['content-type'] = requestContentType
130+
} else if (Array.isArray(operation.consumes)) {
131+
req.headers['content-type'] = operation.consumes[0]
132+
} else if (Array.isArray(spec.consumes)) {
133+
req.headers['content-type'] = spec.consumes[0]
134+
} else if (operation.parameters.filter((p)=> p.type === "file").length) {
135+
req.headers['content-type'] = "multipart/form-data"
136+
} else if (operation.parameters.filter((p)=> p.in === "formData").length) {
137+
req.headers['content-type'] = "application/x-www-form-urlencoded"
138+
}
139+
}
130140
return req
131141
}
132142

test/execute.js

Lines changed: 133 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ describe('execute', () => {
441441
})
442442
})
443443

444-
it('should handle requestContentType', function () {
444+
it('should not add content-type with no form-data or body param', function () {
445445
// Given
446446
const spec = {
447447
host: 'swagger.io',
@@ -454,14 +454,143 @@ describe('execute', () => {
454454
// Then
455455
expect(req).toEqual({
456456
url: 'http://swagger.io/one',
457-
headers: {
458-
'content-type': 'application/josh',
459-
},
457+
headers: {},
460458
credentials: 'same-origin',
461459
method: 'GET'
462460
})
463461
})
464462

463+
it('should add content-type multipart/form-data when param type is file and no other sources of consumes', function () {
464+
// Given
465+
const spec = {
466+
host: 'swagger.io',
467+
paths: {
468+
'/one': {
469+
post: {
470+
operationId: 'postMe',
471+
parameters: [{name: 'file', type: 'file', 'in': "formData"}]
472+
}
473+
}
474+
}
475+
}
476+
477+
// When
478+
const req = buildRequest({
479+
spec,
480+
operationId: 'postMe',
481+
parameters: { file: 'test'}})
482+
483+
// Then
484+
expect(req).toEqual({
485+
method: "POST",
486+
"body": "file=test",
487+
url: 'http://swagger.io/one',
488+
headers: {
489+
"content-type": "multipart/form-data"
490+
},
491+
credentials: 'same-origin'
492+
})
493+
})
494+
495+
it('should add content-type application/x-www-form-urlencoded when in: formData ', function () {
496+
// Given
497+
const spec = {
498+
host: 'swagger.io',
499+
paths: {
500+
'/one': {
501+
post: {
502+
operationId: 'postMe',
503+
parameters: [{name: 'file', in: 'formData'}]
504+
}
505+
}
506+
}
507+
}
508+
509+
// When
510+
const req = buildRequest({
511+
spec,
512+
operationId: 'postMe',
513+
parameters: { file: 'test'}})
514+
515+
// Then
516+
expect(req).toEqual({
517+
body: "file=test",
518+
method: "POST",
519+
url: 'http://swagger.io/one',
520+
headers: {
521+
"content-type": "application/x-www-form-urlencoded"
522+
},
523+
credentials: 'same-origin'
524+
})
525+
})
526+
527+
it('should add content-type from spec when no consumes in operation and no requestContentType passed', function () {
528+
// Given
529+
const spec = {
530+
host: 'swagger.io',
531+
consumes: ["test"],
532+
paths: {
533+
'/one': {
534+
post: {
535+
operationId: 'postMe',
536+
parameters: [{name: 'file', in: 'formData'}]
537+
}
538+
}
539+
}
540+
}
541+
542+
// When
543+
const req = buildRequest({
544+
spec,
545+
operationId: 'postMe',
546+
parameters: { file: 'test'}})
547+
548+
// Then
549+
expect(req).toEqual({
550+
body: "file=test",
551+
method: "POST",
552+
url: 'http://swagger.io/one',
553+
headers: {
554+
"content-type": "test"
555+
},
556+
credentials: 'same-origin'
557+
})
558+
})
559+
560+
it('should add content-type from operation when no requestContentType passed', function () {
561+
// Given
562+
const spec = {
563+
host: 'swagger.io',
564+
consumes: ["no"],
565+
paths: {
566+
'/one': {
567+
post: {
568+
operationId: 'postMe',
569+
consumes: ["test"],
570+
parameters: [{name: 'file', in: 'formData'}]
571+
}
572+
}
573+
}
574+
}
575+
576+
// When
577+
const req = buildRequest({
578+
spec,
579+
operationId: 'postMe',
580+
parameters: { file: 'test'}})
581+
582+
// Then
583+
expect(req).toEqual({
584+
body: "file=test",
585+
method: "POST",
586+
url: 'http://swagger.io/one',
587+
headers: {
588+
"content-type": "test"
589+
},
590+
credentials: 'same-origin'
591+
})
592+
})
593+
465594
it('should build a request for all given fields', function () {
466595
// Given
467596
const spec = {

0 commit comments

Comments
 (0)