Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

fix: edge case where Content-Type could sometimes be doubled up #484

Merged
merged 1 commit into from
Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions packages/oas-to-har/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1435,6 +1435,38 @@ describe('content-type & accept header', () => {
).log.entries[0].request.headers
).toStrictEqual([{ name: 'Content-Type', value: 'application/json' }]);
});

it("should only add a content-type if one isn't already present", () => {
const har = oasToHar(
new Oas({
'x-headers': [{ key: 'Content-Type', value: 'multipart/form-data' }],
}),
{
path: '/',
method: 'put',
requestBody: {
content: {
'application/json': {
schema: {
type: 'object',
required: ['a'],
properties: {
a: {
type: 'string',
},
},
},
example: { a: 'value' },
},
},
},
}
);

// `Content-Type: application/json` would normally appear here if there were no `x-headers`, but since there is
// we should default to that so as to we don't double up on Content-Type headers.
expect(har.log.entries[0].request.headers).toStrictEqual([{ name: 'Content-Type', value: 'multipart/form-data' }]);
});
});

describe('x-headers', () => {
Expand Down
17 changes: 14 additions & 3 deletions packages/oas-to-har/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,17 @@ module.exports = (
}

// Do we have any `header` parameters on the operation?
let hasContentType = false;
const headers = parameters && parameters.filter(param => param.in === 'header');
if (headers && headers.length) {
headers.forEach(header => {
const value = formatter(formData, header, 'header', true);
if (typeof value === 'undefined') return;

if (header.name.toLowerCase() === 'content-type') {
hasContentType = true;
}

har.headers.push({
name: header.name,
value: String(value),
Expand All @@ -165,6 +171,10 @@ module.exports = (
// Are there `x-static` static headers configured for this OAS?
if (oas['x-headers']) {
oas['x-headers'].forEach(header => {
if (header.key.toLowerCase() === 'content-type') {
hasContentType = true;
}

har.headers.push({
name: header.key,
value: String(header.value),
Expand Down Expand Up @@ -235,10 +245,11 @@ module.exports = (
}
}

// Add content-type header if there are any body values setup above ^^
// or if there is a schema defined
if (har.postData.text || Object.keys(schema.schema).length) {
// Add a `Content-Type` header if there are any body values setup above or if there is a schema defined, but only do
// so if we don't already have a `Content-Type` present as it's impossible for a request to have multiple.
if ((har.postData.text || Object.keys(schema.schema).length) && !hasContentType) {
const type = getContentType(pathOperation);

har.headers.push({
name: 'Content-Type',
value: type,
Expand Down