Skip to content
Open
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
74 changes: 73 additions & 1 deletion packages/http/src/mocker/__tests__/HttpMocker.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createLogger, IPrismInput } from '@stoplight/prism-core';
import { IHttpOperation, INodeExample, DiagnosticSeverity } from '@stoplight/types';
import { IHttpOperation, INodeExample, DiagnosticSeverity, HttpParamStyles } from '@stoplight/types';
import { right } from 'fp-ts/ReaderEither';
import * as E from 'fp-ts/Either';
import { flatMap } from 'lodash';
Expand Down Expand Up @@ -676,5 +676,77 @@ describe('mocker', () => {
});
});
});

describe('transport-level headers', () => {
it('does not include Content-Encoding in mocked response headers', () => {
jest.spyOn(helpers, 'negotiateOptionsForValidRequest').mockReturnValue(
right({
code: '200',
mediaType: 'application/json',
bodyExample: { id: 'test', key: 'test', value: { message: 'ok' } },
headers: [
{
id: 'Content-Encoding',
name: 'Content-Encoding',
style: HttpParamStyles.Simple,
schema: { type: 'string' as const, enum: ['gzip', 'deflate', 'br'] },
},
{
id: 'X-Request-Id',
name: 'X-Request-Id',
style: HttpParamStyles.Simple,
schema: { type: 'string' as const },
},
],
})
);

const mockResult = mock({
config: { dynamic: false },
resource: mockResource,
input: mockInput,
})(logger);

assertRight(mockResult, result => {
expect(result.headers).not.toHaveProperty('Content-Encoding');
expect(result.headers).toHaveProperty('X-Request-Id');
});
});

it('does not include Transfer-Encoding or Content-Length in mocked response headers', () => {
jest.spyOn(helpers, 'negotiateOptionsForValidRequest').mockReturnValue(
right({
code: '200',
mediaType: 'application/json',
bodyExample: { id: 'test', key: 'test', value: { message: 'ok' } },
headers: [
{
id: 'Transfer-Encoding',
name: 'Transfer-Encoding',
style: HttpParamStyles.Simple,
schema: { type: 'string' as const, enum: ['chunked'] },
},
{
id: 'Content-Length',
name: 'Content-Length',
style: HttpParamStyles.Simple,
schema: { type: 'integer' as const },
},
],
})
);

const mockResult = mock({
config: { dynamic: false },
resource: mockResource,
input: mockInput,
})(logger);

assertRight(mockResult, result => {
expect(result.headers).not.toHaveProperty('Transfer-Encoding');
expect(result.headers).not.toHaveProperty('Content-Length');
});
});
});
});
});
10 changes: 9 additions & 1 deletion packages/http/src/mocker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,10 +359,18 @@ function isINodeExample(nodeExample: ContentExample | undefined): nodeExample is
return !!nodeExample && 'value' in nodeExample;
}

const TRANSPORT_HEADERS = new Set([
'content-encoding',
'content-length',
'transfer-encoding',
'content-md5',
]);

function computeMockedHeaders(headers: IHttpHeaderParam[], payloadGenerator: PayloadGenerator) {
const filteredHeaders = headers.filter(h => !TRANSPORT_HEADERS.has(h.name.toLowerCase()));
return eitherRecordSequence(
mapValues(
keyBy(headers, h => h.name),
keyBy(filteredHeaders, h => h.name),
header => {
if (header.schema) {
if (header.examples && header.examples.length > 0) {
Expand Down