Skip to content

Commit

Permalink
fix: set accept header only if not set request headers
Browse files Browse the repository at this point in the history
  • Loading branch information
freaz committed Oct 6, 2022
1 parent c577455 commit 3d6aed2
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed
- Do not set `accept` header when defined in [HTTP request headers](https://spec.superface.dev/latest/map-spec.html#HTTPHeaders)

## [2.0.0] - 2022-08-15
### Added
- Pass `cache` flag to SuperfaceClient constructor
Expand Down
17 changes: 17 additions & 0 deletions src/core/interpreter/http/filters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,5 +360,22 @@ describe('HTTP Filters', () => {
})
);
});

it("doesn't set accept header if already exists", async () => {
const result = await headersFilter({
parameters: {
url: '/test',
method: '',
baseUrl: 'https://example.com',
headers: {
aCcEpT: 'application/json',
},
accept: 'text/plain',
contentType: JSON_CONTENT,
},
});

expect(result.request?.headers?.accept).toBe(undefined);
})
});
});
10 changes: 7 additions & 3 deletions src/core/interpreter/http/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import type {
RequestParameters,
} from './security';
import type { HttpResponse } from './types';
import { createUrl, fetchRequest } from './utils';
import { createUrl, fetchRequest, hasAcceptHeader } from './utils';

/**
* Represents input of pipe filter which works with http response
Expand Down Expand Up @@ -258,8 +258,12 @@ export const headersFilter: Filter = ({
request,
response,
}: FilterInputOutput) => {
const headers: Record<string, string> = parameters.headers || {};
headers['accept'] = parameters.accept ?? '*/*';
const headers: Record<string, string> = parameters.headers ?? {};

if (!hasAcceptHeader(headers)) {
headers['accept'] = parameters.accept ?? '*/*';
}

headers['user-agent'] ??= USER_AGENT;
if (parameters.contentType === JSON_CONTENT) {
headers['content-type'] ??= JSON_CONTENT;
Expand Down
6 changes: 6 additions & 0 deletions src/core/interpreter/http/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,9 @@ export async function fetchRequest(
},
};
}

export function hasAcceptHeader(headers: NonPrimitive): boolean {
return Object.keys(headers).some(
header => header.toLowerCase() === 'accept'
);
}
2 changes: 1 addition & 1 deletion src/core/interpreter/map-interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ export class MapInterpreter<TInput extends NonPrimitive | undefined>
this.visit(responseHandler)
);

let accept = '';
let accept: string;
if (responseHandlers.some(([, accept]) => accept === undefined)) {
accept = '*/*';
} else {
Expand Down

0 comments on commit 3d6aed2

Please sign in to comment.