Skip to content

Commit

Permalink
fix(credential-provider-imds): destroy request handle on promise reso…
Browse files Browse the repository at this point in the history
…lve/reject (#2452)

Co-authored-by: Trivikram Kamat <16024985+trivikr@users.noreply.github.com>
  • Loading branch information
samchungy and trivikr authored Jun 17, 2021
1 parent 0f6548e commit 122c139
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { ProviderError } from "@aws-sdk/property-provider";
import { createServer } from "http";
import http, { createServer } from "http";
import nock from "nock";

import { httpRequest } from "./httpRequest";

describe("httpRequest", () => {
const requestSpy = jest.spyOn(http, "request");
let port: number;
const host = "localhost";
const path = "/";
Expand All @@ -26,13 +27,18 @@ describe("httpRequest", () => {
port = await getOpenPort();
});

afterEach(() => {
jest.clearAllMocks();
});

describe("returns response", () => {
it("defaults to method GET", async () => {
const expectedResponse = "expectedResponse";
const scope = nock(`http://${host}:${port}`).get(path).reply(200, expectedResponse);

const response = await httpRequest({ host, path, port });
expect(response.toString()).toStrictEqual(expectedResponse);
expect(requestSpy.mock.results[0].value.socket).toHaveProperty("destroyed", true);

scope.done();
});
Expand All @@ -44,6 +50,7 @@ describe("httpRequest", () => {

const response = await httpRequest({ host, path, port, method });
expect(response.toString()).toStrictEqual(expectedResponse);
expect(requestSpy.mock.results[0].value.socket).toHaveProperty("destroyed", true);

scope.done();
});
Expand All @@ -57,6 +64,7 @@ describe("httpRequest", () => {
await expect(httpRequest({ host, path, port })).rejects.toStrictEqual(
Object.assign(new ProviderError("Error response received from instance metadata service"), { statusCode })
);
expect(requestSpy.mock.results[0].value.socket).toHaveProperty("destroyed", true);

scope.done();
});
Expand All @@ -68,6 +76,7 @@ describe("httpRequest", () => {
await expect(httpRequest({ host, path, port })).rejects.toStrictEqual(
new ProviderError("Unable to connect to instance metadata service")
);
expect(requestSpy.mock.results[0].value.socket).toHaveProperty("destroyed", true);

scope.done();
});
Expand All @@ -91,6 +100,7 @@ describe("httpRequest", () => {
await expect(httpRequest({ host, path, port, timeout })).rejects.toStrictEqual(
new ProviderError("TimeoutError from instance metadata service")
);
expect(requestSpy.mock.results[0].value.socket).toHaveProperty("destroyed", true);

scope.done();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ export function httpRequest(options: RequestOptions): Promise<Buffer> {

req.on("error", (err) => {
reject(Object.assign(new ProviderError("Unable to connect to instance metadata service"), err));
req.destroy();
});

req.on("timeout", () => {
reject(new ProviderError("TimeoutError from instance metadata service"));
req.destroy();
});

req.on("response", (res: IncomingMessage) => {
Expand All @@ -23,6 +25,7 @@ export function httpRequest(options: RequestOptions): Promise<Buffer> {
reject(
Object.assign(new ProviderError("Error response received from instance metadata service"), { statusCode })
);
req.destroy();
}

const chunks: Array<Buffer> = [];
Expand All @@ -31,6 +34,7 @@ export function httpRequest(options: RequestOptions): Promise<Buffer> {
});
res.on("end", () => {
resolve(Buffer.concat(chunks));
req.destroy();
});
});

Expand Down

0 comments on commit 122c139

Please sign in to comment.