Skip to content

Commit

Permalink
feat: implement HTTP HEAD method (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastijankuzner committed Aug 3, 2020
1 parent 291be4e commit 828736b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
23 changes: 23 additions & 0 deletions __tests__/http.test.ts
Expand Up @@ -116,6 +116,29 @@ describe("HTTP", () => {
});
});

describe("#head", () => {
it("should send a request and receive status code 200", async () => {
const { statusCode } = await http.head(`${serverURL}/get?key=value`);

expect(statusCode).toBe(200);
});

it("should send a request and receive status code 404", async () => {
await expect(http.head(`${serverURL}/status/404`)).rejects.toThrow("Not Found");
});

// HTTP GET will throw error because body is malformed
it("should send a request and receive status code 200 because malformed JSON is not received in body", async () => {
const { statusCode } = await http.head(`${serverURL}/malformed`);

expect(statusCode).toBe(200);
});

it("should send a request and throw when the request times out", async () => {
await expect(http.head(`${serverURL}/timeout`, { timeout: 1000 })).rejects.toThrow("socket hang up");
});
});

describe("#post", () => {
it("should send a request and receive status code 200", async () => {
const { statusCode } = await http.post(`${serverURL}/post`);
Expand Down
1 change: 1 addition & 0 deletions src/http.ts
Expand Up @@ -120,6 +120,7 @@ const sendRequest = (method: string, url: string, opts?: HttpOptions): Promise<H

export const http = {
get: (url: string, opts?: HttpOptions): Promise<HttpResponse> => sendRequest("GET", url, opts),
head: (url: string, opts?: HttpOptions): Promise<HttpResponse> => sendRequest("HEAD", url, opts),
post: (url: string, opts?: HttpOptions): Promise<HttpResponse> => sendRequest("POST", url, opts),
put: (url: string, opts?: HttpOptions): Promise<HttpResponse> => sendRequest("PUT", url, opts),
patch: (url: string, opts?: HttpOptions): Promise<HttpResponse> => sendRequest("PATCH", url, opts),
Expand Down

0 comments on commit 828736b

Please sign in to comment.