Skip to content

Commit

Permalink
fix(repeater): setting the request headers (#150)
Browse files Browse the repository at this point in the history
fixes #149
  • Loading branch information
derevnjuk authored Jun 16, 2023
1 parent 28ac74c commit f652a89
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
13 changes: 13 additions & 0 deletions packages/repeater/src/request-runner/Request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,18 @@ describe('Request', () => {
'x-a2': 'a2'
});
});

it('should join headers if multiple values is present', () => {
const request = new Request({
url: 'http://foo.bar',
protocol: Protocol.HTTP
});

request.setHeaders({ host: ['example.com', 'example1.com'] });

expect(request.headers).toEqual({
host: 'example.com, example1.com'
});
});
});
});
32 changes: 30 additions & 2 deletions packages/repeater/src/request-runner/Request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ export interface RequestOptions {
}

export class Request {
public static readonly SINGLE_VALUE_HEADERS: ReadonlySet<string> =
new Set<string>([
'authorization',
'content-disposition',
'content-length',
'content-type',
'from',
'host',
'if-modified-since',
'if-unmodified-since',
'location',
'max-forwards',
'proxy-authorization',
'referer',
'user-agent'
]);
public readonly protocol: Protocol;
public readonly url: string;
public readonly body?: string;
Expand Down Expand Up @@ -46,16 +62,28 @@ export class Request {
this.url = url;
this.correlationIdRegex =
this.normalizeCorrelationIdRegex(correlationIdRegex);
this._headers = headers;
this.setHeaders(headers);
this.precheckBody(body);
this.body = body;
}

public setHeaders(headers: Record<string, string | string[]>): void {
this._headers = {
const mergedHeaders = {
...this._headers,
...headers
};

this._headers = Object.fromEntries(
Object.entries(mergedHeaders).map(
([field, value]: [string, string | string[]]) => [
field,
Array.isArray(value) &&
Request.SINGLE_VALUE_HEADERS.has(field.toLowerCase())
? value.join(', ')
: value
]
)
);
}

private validateUrl(url: string): void {
Expand Down

0 comments on commit f652a89

Please sign in to comment.