Skip to content

Commit 1bb5365

Browse files
committed
Include trailers in server Send events
1 parent 2aa0289 commit 1bb5365

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

src/client/client-types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type * as Mockttp from 'mockttp';
33
// --- Request definition types ---
44

55
export type RawHeaders = Mockttp.RawHeaders;
6+
export type RawTrailers = Mockttp.RawTrailers;
67

78
export interface RequestDefinition {
89
method: string;
@@ -87,6 +88,7 @@ export type ResponseStreamEvents =
8788
| RequestStart
8889
| ResponseHead
8990
| ResponseBodyPart
91+
| ResponseTrailers
9092
| ResponseEnd;
9193
// Other notable event is errors (via 'error' event)
9294

@@ -110,6 +112,12 @@ export interface ResponseBodyPart {
110112
timestamp: number;
111113
}
112114

115+
export interface ResponseTrailers {
116+
type: 'response-trailers';
117+
trailers: RawTrailers;
118+
timestamp: number;
119+
}
120+
113121
export interface ResponseEnd {
114122
type: 'response-end';
115123
timestamp: number;

src/client/http-client.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@ export class HttpClient {
163163
}));
164164

165165
response.on('end', () => {
166+
if (response.rawTrailers?.length) {
167+
resultsStream.push({
168+
type: 'response-trailers',
169+
trailers: pairFlatRawHeaders(response.rawTrailers),
170+
timestamp: performance.now()
171+
});
172+
}
166173
resultsStream.push({ type: 'response-end', timestamp: performance.now() });
167174
resultsStream.push(null);
168175
});

test/unit/send-request.spec.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ describe("The HTTP client API", () => {
2727
return {
2828
statusCode: 200,
2929
statusMessage: 'Custom status message',
30-
headers: { 'custom-HEADER': 'custom-VALUE' },
30+
headers: {
31+
'custom-HEADER': 'custom-VALUE',
32+
'Transfer-Encoding': 'chunked'
33+
},
34+
trailers: { 'custom-TRAILER': 'trailer-VALUE' },
3135
rawBody: Buffer.from('Mock response body')
3236
};
3337
});
@@ -46,7 +50,7 @@ describe("The HTTP client API", () => {
4650

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

49-
expect(responseEvents.length).to.equal(4);
53+
expect(responseEvents.length).to.equal(5);
5054
expect(_.omit(responseEvents[0], 'timestamp', 'startTime')).to.deep.equal({
5155
type: 'request-start'
5256
});
@@ -55,13 +59,22 @@ describe("The HTTP client API", () => {
5559
statusCode: 200,
5660
statusMessage: 'Custom status message',
5761
headers: [
58-
['custom-HEADER', 'custom-VALUE']
62+
['custom-HEADER', 'custom-VALUE'],
63+
['Transfer-Encoding', 'chunked']
5964
]
6065
});
6166

6267
expect(responseEvents[2].type).equal('response-body-part');
6368
expect(responseEvents[2].rawBody.toString()).to.equal('Mock response body');
69+
6470
expect(_.omit(responseEvents[3], 'timestamp')).to.deep.equal({
71+
type: 'response-trailers',
72+
trailers: [
73+
['custom-TRAILER', 'trailer-VALUE']
74+
]
75+
});
76+
77+
expect(_.omit(responseEvents[4], 'timestamp')).to.deep.equal({
6578
type: 'response-end'
6679
});
6780
});

0 commit comments

Comments
 (0)