Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rawHeaders to HttpResponse #165

Merged
merged 3 commits into from
Dec 28, 2021
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
1 change: 1 addition & 0 deletions src/cli/send/jsonOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export function toSendJsonOutput(context: Record<string, Array<HttpRegion>>, opt

function convertResponse(response: HttpResponse | undefined, output: string | undefined) {
if (response) {
delete response.rawHeaders;
delete response.rawBody;
delete response.prettyPrintBody;
delete response.parsedBody;
Expand Down
1 change: 1 addition & 0 deletions src/models/httpResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface HttpResponse {
body?: unknown;
parsedBody?: unknown;
prettyPrintBody?: string;
rawHeaders?: string[];
rawBody?: Buffer;
request?: Request;

Expand Down
23 changes: 23 additions & 0 deletions src/utils/gotUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ function toHttpResponse(response: Response<unknown>): HttpResponse {
protocol: `HTTP/${response.httpVersion}`,
statusMessage: response.statusMessage,
body: response.body,
rawHeaders: response.rawHeaders,
rawBody: response.rawBody,
headers: response.headers,
timings: response.timings.phases,
Expand Down Expand Up @@ -164,3 +165,25 @@ export function initHttpClient(content: { config?: EnvironmentConfig }): HttpCli
};
return gotHttpClientFactory(request);
}

/**
* Merges a raw HTTP headers array from a got HTTP Response into a record that
* groups same-named lower-cased HTTP Headers to arrays of values.
* I.e. HTTP headers that only appear once will be associated with a single-item string-array,
* Headers that appear multiple times (e.g. Set-Cookie) are stored in multi-item string-arrays in order of appearence.
* @param rawHeaders A raw HTTP headers array, even numbered indicies represent HTTP header names, odd numbered indicies represent header values.
*/
export function mergeRawHttpHeaders(rawHeaders: string[]): Record<string, string[]> {
const mergedHeaders: Record<string, string[]> = {};
for (let i = 0; i < rawHeaders.length; i += 2) {
const headerRawName = rawHeaders[i];
const headerRawValue = rawHeaders[i + 1];
if (typeof headerRawValue === 'undefined') {
continue; // Likely at end of array, continue will make for-condition to evaluate falsy
}
const headerName = headerRawName.toLowerCase();
mergedHeaders[headerName] ||= [];
mergedHeaders[headerName].push(headerRawValue);
}
return mergedHeaders;
}
1 change: 1 addition & 0 deletions src/utils/requestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ export function cloneResponse(response: models.HttpResponse): models.HttpRespons
httpVersion: response.httpVersion,
headers: response.headers,
body: response.body,
rawHeaders: response.rawHeaders,
rawBody: response.rawBody,
parsedBody: response.parsedBody,
prettyPrintBody: response.prettyPrintBody,
Expand Down
23 changes: 23 additions & 0 deletions test/utils/gotUtils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { mergeRawHttpHeaders } from '../../src/utils/gotUtils';

describe('Raw HTTP Header merge utils', () => {
it('merges example raw headers to expected record value', () => {
// Example taken from got documentation of the rawHeaders property of the Response type
const rawHeaders = [
'user-agent',
'this is invalid because there can be only one',
'User-Agent',
'curl/7.22.0',
'Host',
'127.0.0.1:8000',
'ACCEPT',
'*',
];
const mergedHeaders = mergeRawHttpHeaders(rawHeaders);
// Note User-Agent header with different casing collapsed into a single multi-item string-array
expect(mergedHeaders['user-agent']).toEqual(['this is invalid because there can be only one', 'curl/7.22.0']);
// Headers that only appear once are stored in single-item string-arrays
expect(mergedHeaders.host).toEqual(['127.0.0.1:8000']);
expect(mergedHeaders.accept).toEqual(['*']);
});
});