Skip to content

Commit 778b305

Browse files
authored
fix(fetch-http-handler): omit body for HEAD/GET methods (#1698)
1 parent 8adfed1 commit 778b305

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,56 @@ describe.skip(FetchHttpHandler.name, () => {
8686
expect(requestCall[0]).toBe("https://foo.amazonaws.com:443/test/?bar=baz");
8787
});
8888

89+
it("will omit body if method is GET", async () => {
90+
const mockResponse = {
91+
headers: { entries: jest.fn().mockReturnValue([]) },
92+
blob: jest.fn().mockResolvedValue(new Blob()),
93+
};
94+
const mockFetch = jest.fn().mockResolvedValue(mockResponse);
95+
96+
(global as any).fetch = mockFetch;
97+
98+
const httpRequest = new HttpRequest({
99+
headers: {},
100+
hostname: "foo.amazonaws.com",
101+
method: "GET",
102+
path: "/",
103+
body: "will be omitted",
104+
});
105+
const fetchHttpHandler = new FetchHttpHandler();
106+
107+
await fetchHttpHandler.handle(httpRequest, {});
108+
109+
expect(mockFetch.mock.calls.length).toBe(1);
110+
const requestCall = mockRequest.mock.calls[0];
111+
expect(requestCall[1].body).toBeUndefined();
112+
});
113+
114+
it("will omit body if method is HEAD", async () => {
115+
const mockResponse = {
116+
headers: { entries: jest.fn().mockReturnValue([]) },
117+
blob: jest.fn().mockResolvedValue(new Blob()),
118+
};
119+
const mockFetch = jest.fn().mockResolvedValue(mockResponse);
120+
121+
(global as any).fetch = mockFetch;
122+
123+
const httpRequest = new HttpRequest({
124+
headers: {},
125+
hostname: "foo.amazonaws.com",
126+
method: "HEAD",
127+
path: "/",
128+
body: "will be omitted",
129+
});
130+
const fetchHttpHandler = new FetchHttpHandler();
131+
132+
await fetchHttpHandler.handle(httpRequest, {});
133+
134+
expect(mockFetch.mock.calls.length).toBe(1);
135+
const requestCall = mockRequest.mock.calls[0];
136+
expect(requestCall[1].body).toBeUndefined();
137+
});
138+
89139
it("will not make request if already aborted", async () => {
90140
const mockResponse = {
91141
headers: {

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,15 @@ export class FetchHttpHandler implements HttpHandler {
4444
}
4545
}
4646

47-
const port = request.port;
47+
const { port, method } = request;
4848
const url = `${request.protocol}//${request.hostname}${port ? `:${port}` : ""}${path}`;
49+
// Request constructor doesn't allow GET/HEAD request with body
50+
// ref: https://github.com/whatwg/fetch/issues/551
51+
const body = method === "GET" || method === "HEAD" ? undefined : request.body;
4952
const requestOptions: RequestInit = {
50-
body: request.body,
53+
body,
5154
headers: new Headers(request.headers),
52-
method: request.method,
55+
method: method,
5356
};
5457

5558
// some browsers support abort signal

0 commit comments

Comments
 (0)