Skip to content

Commit

Permalink
Include trailers in server Send events
Browse files Browse the repository at this point in the history
  • Loading branch information
pimterry committed May 1, 2024
1 parent 2aa0289 commit 1bb5365
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/client/client-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type * as Mockttp from 'mockttp';
// --- Request definition types ---

export type RawHeaders = Mockttp.RawHeaders;
export type RawTrailers = Mockttp.RawTrailers;

export interface RequestDefinition {
method: string;
Expand Down Expand Up @@ -87,6 +88,7 @@ export type ResponseStreamEvents =
| RequestStart
| ResponseHead
| ResponseBodyPart
| ResponseTrailers
| ResponseEnd;
// Other notable event is errors (via 'error' event)

Expand All @@ -110,6 +112,12 @@ export interface ResponseBodyPart {
timestamp: number;
}

export interface ResponseTrailers {
type: 'response-trailers';
trailers: RawTrailers;
timestamp: number;
}

export interface ResponseEnd {
type: 'response-end';
timestamp: number;
Expand Down
7 changes: 7 additions & 0 deletions src/client/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ export class HttpClient {
}));

response.on('end', () => {
if (response.rawTrailers?.length) {
resultsStream.push({
type: 'response-trailers',
trailers: pairFlatRawHeaders(response.rawTrailers),
timestamp: performance.now()
});
}
resultsStream.push({ type: 'response-end', timestamp: performance.now() });
resultsStream.push(null);
});
Expand Down
19 changes: 16 additions & 3 deletions test/unit/send-request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ describe("The HTTP client API", () => {
return {
statusCode: 200,
statusMessage: 'Custom status message',
headers: { 'custom-HEADER': 'custom-VALUE' },
headers: {
'custom-HEADER': 'custom-VALUE',
'Transfer-Encoding': 'chunked'
},
trailers: { 'custom-TRAILER': 'trailer-VALUE' },
rawBody: Buffer.from('Mock response body')
};
});
Expand All @@ -46,7 +50,7 @@ describe("The HTTP client API", () => {

const responseEvents = await streamToArray<any>(responseStream);

expect(responseEvents.length).to.equal(4);
expect(responseEvents.length).to.equal(5);
expect(_.omit(responseEvents[0], 'timestamp', 'startTime')).to.deep.equal({
type: 'request-start'
});
Expand All @@ -55,13 +59,22 @@ describe("The HTTP client API", () => {
statusCode: 200,
statusMessage: 'Custom status message',
headers: [
['custom-HEADER', 'custom-VALUE']
['custom-HEADER', 'custom-VALUE'],
['Transfer-Encoding', 'chunked']
]
});

expect(responseEvents[2].type).equal('response-body-part');
expect(responseEvents[2].rawBody.toString()).to.equal('Mock response body');

expect(_.omit(responseEvents[3], 'timestamp')).to.deep.equal({
type: 'response-trailers',
trailers: [
['custom-TRAILER', 'trailer-VALUE']
]
});

expect(_.omit(responseEvents[4], 'timestamp')).to.deep.equal({
type: 'response-end'
});
});
Expand Down

0 comments on commit 1bb5365

Please sign in to comment.