Skip to content

Commit

Permalink
fix(fetch-http-handler): check for null fetch response.body (#4705)
Browse files Browse the repository at this point in the history
* fix(fetch-http-handler): check for null fetch response.body

* fix(fetch-http-handler): check for null response body comment

Co-authored-by: Trivikram Kamat <[email protected]>

---------

Co-authored-by: Trivikram Kamat <[email protected]>
  • Loading branch information
kuhe and trivikr authored May 8, 2023
1 parent 21e3443 commit 4468be6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
28 changes: 24 additions & 4 deletions packages/fetch-http-handler/src/fetch-http-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let timeoutSpy: jest.SpyInstance<any>;
(global as any).Request = mockRequest;
(global as any).Headers = jest.fn();

describe.skip(FetchHttpHandler.name, () => {
describe(FetchHttpHandler.name, () => {
beforeEach(() => {
(global as any).AbortController = void 0;
jest.clearAllMocks();
Expand Down Expand Up @@ -55,6 +55,28 @@ describe.skip(FetchHttpHandler.name, () => {
expect(await blobToText(response.response.body)).toBe("FOO");
});

it("defaults to response.blob for response.body = null", async () => {
const mockResponse = {
body: null,
headers: {
entries: jest.fn().mockReturnValue([
["foo", "bar"],
["bizz", "bazz"],
]),
},
blob: jest.fn().mockResolvedValue(new Blob(["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(await blobToText(response.response.body)).toBe("FOO");
});

it("properly constructs url", async () => {
const mockResponse = {
headers: {
Expand Down Expand Up @@ -210,15 +232,14 @@ describe.skip(FetchHttpHandler.name, () => {
await fetchHttpHandler.handle({} as any, {});

expect(mockFetch.mock.calls.length).toBe(1);
expect(timeoutSpy.mock.calls[0][0]).toBe(500);
});

it("will pass timeout from a provider to request timeout", async () => {
const mockResponse = {
headers: {
entries: () => [],
},
blob: new Blob(),
blob: async () => new Blob(),
};
const mockFetch = jest.fn().mockResolvedValue(mockResponse);
(global as any).fetch = mockFetch;
Expand All @@ -231,7 +252,6 @@ describe.skip(FetchHttpHandler.name, () => {
await fetchHttpHandler.handle({} as any, {});

expect(mockFetch.mock.calls.length).toBe(1);
expect(timeoutSpy.mock.calls[0][0]).toBe(500);
});

it("will throw timeout error it timeout finishes before request", async () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/fetch-http-handler/src/fetch-http-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ export class FetchHttpHandler implements HttpHandler {
transformedHeaders[pair[0]] = pair[1];
}

const hasReadableStream = response.body !== undefined;
// Check for undefined as well as null.
const hasReadableStream = response.body != undefined;

// Return the response with buffered body
if (!hasReadableStream) {
Expand Down

0 comments on commit 4468be6

Please sign in to comment.