Skip to content

Commit

Permalink
feat(protocol-http): add reason to HttpResponse (#4772)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
milesziemer committed Jun 6, 2023
1 parent 8e69678 commit d29cb58
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/fetch-http-handler/src/fetch-http-handler.spec.ts
Expand Up @@ -290,6 +290,28 @@ describe(FetchHttpHandler.name, () => {
expect(mockRequest.mock.calls[0][1]).not.toHaveProperty("signal");
});

it("creates correct HTTPResponse object", async () => {
const mockResponse = {
headers: {
entries: jest.fn().mockReturnValue([["foo", "bar"]]),
},
blob: jest.fn().mockResolvedValue(new Blob(["FOO"])),
status: 200,
statusText: "foo",
};
const mockFetch = jest.fn().mockResolvedValue(mockResponse);
(global as any).fetch = mockFetch;

const fetchHttpHandler = new FetchHttpHandler();
const { response } = await fetchHttpHandler.handle({} as any, {});

expect(mockFetch.mock.calls.length).toBe(1);
expect(response.headers).toStrictEqual({ foo: "bar" });
expect(response.reason).toBe("foo");
expect(response.statusCode).toBe(200);
expect(await blobToText(response.body)).toBe("FOO");
});

describe("#destroy", () => {
it("should be callable and return nothing", () => {
const httpHandler = new FetchHttpHandler();
Expand Down
2 changes: 2 additions & 0 deletions packages/fetch-http-handler/src/fetch-http-handler.ts
Expand Up @@ -91,6 +91,7 @@ export class FetchHttpHandler implements HttpHandler {
return response.blob().then((body) => ({
response: new HttpResponse({
headers: transformedHeaders,
reason: response.statusText,
statusCode: response.status,
body,
}),
Expand All @@ -100,6 +101,7 @@ export class FetchHttpHandler implements HttpHandler {
return {
response: new HttpResponse({
headers: transformedHeaders,
reason: response.statusText,
statusCode: response.status,
body: response.body,
}),
Expand Down
2 changes: 2 additions & 0 deletions packages/node-http-handler/src/node-http-handler.spec.ts
Expand Up @@ -141,6 +141,7 @@ describe("NodeHttpHandler", () => {
it("can send http requests", async () => {
const mockResponse = {
statusCode: 200,
statusText: "OK",
headers: {},
body: "test",
};
Expand All @@ -160,6 +161,7 @@ describe("NodeHttpHandler", () => {
);

expect(response.statusCode).toEqual(mockResponse.statusCode);
expect(response.reason).toEqual(mockResponse.statusText);
expect(response.headers).toBeDefined();
expect(response.headers).toMatchObject(mockResponse.headers);
expect(response.body).toBeDefined();
Expand Down
1 change: 1 addition & 0 deletions packages/node-http-handler/src/node-http-handler.ts
Expand Up @@ -134,6 +134,7 @@ export class NodeHttpHandler implements HttpHandler {
const req = requestFunc(nodeHttpsOptions, (res) => {
const httpResponse = new HttpResponse({
statusCode: res.statusCode || -1,
reason: res.statusMessage,
headers: getTransformedHeaders(res.headers),
body: res,
});
Expand Down
3 changes: 3 additions & 0 deletions packages/protocol-http/src/httpResponse.ts
Expand Up @@ -2,17 +2,20 @@ import { HeaderBag, HttpMessage, HttpResponse as IHttpResponse } from "@aws-sdk/

type HttpResponseOptions = Partial<HttpMessage> & {
statusCode: number;
reason?: string;
};

export interface HttpResponse extends IHttpResponse {}

export class HttpResponse {
public statusCode: number;
public reason?: string;
public headers: HeaderBag;
public body?: any;

constructor(options: HttpResponseOptions) {
this.statusCode = options.statusCode;
this.reason = options.reason;
this.headers = options.headers || {};
this.body = options.body;
}
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/http.ts
Expand Up @@ -98,6 +98,7 @@ export interface HttpRequest extends HttpMessage, Endpoint {
*/
export interface HttpResponse extends HttpMessage {
statusCode: number;
reason?: string;
}

/**
Expand Down

0 comments on commit d29cb58

Please sign in to comment.