Skip to content

feat(protocol-http): add reason to HttpResponse #4772

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

Merged
merged 1 commit into from
Jun 6, 2023
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
22 changes: 22 additions & 0 deletions packages/fetch-http-handler/src/fetch-http-handler.spec.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export interface HttpRequest extends HttpMessage, Endpoint {
*/
export interface HttpResponse extends HttpMessage {
statusCode: number;
reason?: string;
}

/**
Expand Down