Skip to content

Commit d29cb58

Browse files
authored
feat(protocol-http): add reason to HttpResponse (#4772)
Adds `reason` property to HttpResponse, which is a simple message explaining the status code. The property is optional, so adding it is backward compatible. Http handlers were updated to populate this property when constructing the HttpResponse, except for the Node http2 handler, because http2 doesn't support an equivalent. Tests were added to Http handlers to make sure the property is populated when available.
1 parent 8e69678 commit d29cb58

File tree

6 files changed

+31
-0
lines changed

6 files changed

+31
-0
lines changed

packages/fetch-http-handler/src/fetch-http-handler.spec.ts

+22
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,28 @@ describe(FetchHttpHandler.name, () => {
290290
expect(mockRequest.mock.calls[0][1]).not.toHaveProperty("signal");
291291
});
292292

293+
it("creates correct HTTPResponse object", async () => {
294+
const mockResponse = {
295+
headers: {
296+
entries: jest.fn().mockReturnValue([["foo", "bar"]]),
297+
},
298+
blob: jest.fn().mockResolvedValue(new Blob(["FOO"])),
299+
status: 200,
300+
statusText: "foo",
301+
};
302+
const mockFetch = jest.fn().mockResolvedValue(mockResponse);
303+
(global as any).fetch = mockFetch;
304+
305+
const fetchHttpHandler = new FetchHttpHandler();
306+
const { response } = await fetchHttpHandler.handle({} as any, {});
307+
308+
expect(mockFetch.mock.calls.length).toBe(1);
309+
expect(response.headers).toStrictEqual({ foo: "bar" });
310+
expect(response.reason).toBe("foo");
311+
expect(response.statusCode).toBe(200);
312+
expect(await blobToText(response.body)).toBe("FOO");
313+
});
314+
293315
describe("#destroy", () => {
294316
it("should be callable and return nothing", () => {
295317
const httpHandler = new FetchHttpHandler();

packages/fetch-http-handler/src/fetch-http-handler.ts

+2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export class FetchHttpHandler implements HttpHandler {
9191
return response.blob().then((body) => ({
9292
response: new HttpResponse({
9393
headers: transformedHeaders,
94+
reason: response.statusText,
9495
statusCode: response.status,
9596
body,
9697
}),
@@ -100,6 +101,7 @@ export class FetchHttpHandler implements HttpHandler {
100101
return {
101102
response: new HttpResponse({
102103
headers: transformedHeaders,
104+
reason: response.statusText,
103105
statusCode: response.status,
104106
body: response.body,
105107
}),

packages/node-http-handler/src/node-http-handler.spec.ts

+2
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ describe("NodeHttpHandler", () => {
141141
it("can send http requests", async () => {
142142
const mockResponse = {
143143
statusCode: 200,
144+
statusText: "OK",
144145
headers: {},
145146
body: "test",
146147
};
@@ -160,6 +161,7 @@ describe("NodeHttpHandler", () => {
160161
);
161162

162163
expect(response.statusCode).toEqual(mockResponse.statusCode);
164+
expect(response.reason).toEqual(mockResponse.statusText);
163165
expect(response.headers).toBeDefined();
164166
expect(response.headers).toMatchObject(mockResponse.headers);
165167
expect(response.body).toBeDefined();

packages/node-http-handler/src/node-http-handler.ts

+1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ export class NodeHttpHandler implements HttpHandler {
134134
const req = requestFunc(nodeHttpsOptions, (res) => {
135135
const httpResponse = new HttpResponse({
136136
statusCode: res.statusCode || -1,
137+
reason: res.statusMessage,
137138
headers: getTransformedHeaders(res.headers),
138139
body: res,
139140
});

packages/protocol-http/src/httpResponse.ts

+3
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@ import { HeaderBag, HttpMessage, HttpResponse as IHttpResponse } from "@aws-sdk/
22

33
type HttpResponseOptions = Partial<HttpMessage> & {
44
statusCode: number;
5+
reason?: string;
56
};
67

78
export interface HttpResponse extends IHttpResponse {}
89

910
export class HttpResponse {
1011
public statusCode: number;
12+
public reason?: string;
1113
public headers: HeaderBag;
1214
public body?: any;
1315

1416
constructor(options: HttpResponseOptions) {
1517
this.statusCode = options.statusCode;
18+
this.reason = options.reason;
1619
this.headers = options.headers || {};
1720
this.body = options.body;
1821
}

packages/types/src/http.ts

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ export interface HttpRequest extends HttpMessage, Endpoint {
9898
*/
9999
export interface HttpResponse extends HttpMessage {
100100
statusCode: number;
101+
reason?: string;
101102
}
102103

103104
/**

0 commit comments

Comments
 (0)