Skip to content

Commit

Permalink
blog-template: improve httpClient response parsing (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
wight554 committed Mar 23, 2022
1 parent feea787 commit b22e5d0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 51 deletions.
8 changes: 3 additions & 5 deletions src/api/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,11 @@ const createHttpClientInstance = (): HttpClientInstance => {

const response = await fetch(url, { ...init, method, body, headers });

let data: any;
let data: any = await response.text();

try {
data = await response.json();
} catch (err) {
data = '';
}
data = JSON.parse(data);
} catch {}

return {
data,
Expand Down
97 changes: 51 additions & 46 deletions test/src/api/httpClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { httpClient } from '@src/api/httpClient';
const url = 'url';
const body = { data: 'data' };
const init = { headers: { Authorization: 'Bearer token' } };
const response = 'response';
const jsonResponse = '{"respose":"response"}';
const textResponse = 'response';

const mockResponse = {
json: vi.fn().mockResolvedValue(response),
text: vi.fn().mockResolvedValue(jsonResponse),
};

global.fetch = vi.fn().mockImplementation(() => mockResponse);
Expand All @@ -31,8 +32,18 @@ describe('httpClient', () => {
});
});

it('should return response from fetch api', async () => {
expect(await httpClient.delete(url, init)).toEqual({ data: response });
describe('response payload is valid JSON', () => {
it('should return parsed response from fetch api', async () => {
expect(await httpClient.delete(url, init)).toEqual({ data: JSON.parse(jsonResponse) });
});
});

describe('response payload is not valid JSON', () => {
it('should return string response from fetch api', async () => {
vi.spyOn(mockResponse, 'text').mockResolvedValueOnce(textResponse);

expect(await httpClient.delete(url, init)).toEqual({ data: textResponse });
});
});
});

Expand All @@ -48,17 +59,16 @@ describe('httpClient', () => {
});

describe('response payload is valid JSON', () => {
it('should return response from fetch api', async () => {
expect(await httpClient.get(url, init)).toEqual({ data: response });
it('should return parsed response from fetch api', async () => {
expect(await httpClient.get(url, init)).toEqual({ data: JSON.parse(jsonResponse) });
});
});

describe('response payload can not be parsed as JSON', () => {
it('should return empty string response from fetch api', async () => {
const error = new Error();
vi.spyOn(mockResponse, 'json').mockRejectedValueOnce(error);
describe('response payload is not valid JSON', () => {
it('should return string response from fetch api', async () => {
vi.spyOn(mockResponse, 'text').mockResolvedValueOnce(textResponse);

expect(await httpClient.get(url, init)).toEqual({ data: '' });
expect(await httpClient.get(url, init)).toEqual({ data: textResponse });
});
});
});
Expand All @@ -75,17 +85,16 @@ describe('httpClient', () => {
});

describe('response payload is valid JSON', () => {
it('should return response from fetch api', async () => {
expect(await httpClient.head(url, init)).toEqual({ data: response });
it('should return parsed response from fetch api', async () => {
expect(await httpClient.head(url, init)).toEqual({ data: JSON.parse(jsonResponse) });
});
});

describe('response payload can not be parsed as JSON', () => {
it('should return empty string response from fetch api', async () => {
const error = new Error();
vi.spyOn(mockResponse, 'json').mockRejectedValueOnce(error);
describe('response payload is not valid JSON', () => {
it('should return string response from fetch api', async () => {
vi.spyOn(mockResponse, 'text').mockResolvedValueOnce(textResponse);

expect(await httpClient.head(url, init)).toEqual({ data: '' });
expect(await httpClient.head(url, init)).toEqual({ data: textResponse });
});
});
});
Expand All @@ -102,17 +111,16 @@ describe('httpClient', () => {
});

describe('response payload is valid JSON', () => {
it('should return response from fetch api', async () => {
expect(await httpClient.options(url, init)).toEqual({ data: response });
it('should return parsed response from fetch api', async () => {
expect(await httpClient.options(url, init)).toEqual({ data: JSON.parse(jsonResponse) });
});
});

describe('response payload can not be parsed as JSON', () => {
it('should return empty string response from fetch api', async () => {
const error = new Error();
vi.spyOn(mockResponse, 'json').mockRejectedValueOnce(error);
describe('response payload is not valid JSON', () => {
it('should return string response from fetch api', async () => {
vi.spyOn(mockResponse, 'text').mockResolvedValueOnce(textResponse);

expect(await httpClient.options(url, init)).toEqual({ data: '' });
expect(await httpClient.options(url, init)).toEqual({ data: textResponse });
});
});
});
Expand All @@ -130,17 +138,16 @@ describe('httpClient', () => {
});

describe('response payload is valid JSON', () => {
it('should return response from fetch api', async () => {
expect(await httpClient.patch(url, body, init)).toEqual({ data: response });
it('should return parsed response from fetch api', async () => {
expect(await httpClient.patch(url, body, init)).toEqual({ data: JSON.parse(jsonResponse) });
});
});

describe('response payload can not be parsed as JSON', () => {
it('should return empty string response from fetch api', async () => {
const error = new Error();
vi.spyOn(mockResponse, 'json').mockRejectedValueOnce(error);
describe('response payload is not valid JSON', () => {
it('should return string response from fetch api', async () => {
vi.spyOn(mockResponse, 'text').mockResolvedValueOnce(textResponse);

expect(await httpClient.patch(url, body, init)).toEqual({ data: '' });
expect(await httpClient.patch(url, body, init)).toEqual({ data: textResponse });
});
});
});
Expand All @@ -158,17 +165,16 @@ describe('httpClient', () => {
});

describe('response payload is valid JSON', () => {
it('should return response from fetch api', async () => {
expect(await httpClient.post(url, body, init)).toEqual({ data: response });
it('should return parsed response from fetch api', async () => {
expect(await httpClient.post(url, body, init)).toEqual({ data: JSON.parse(jsonResponse) });
});
});

describe('response payload can not be parsed as JSON', () => {
it('should return empty string response from fetch api', async () => {
const error = new Error();
vi.spyOn(mockResponse, 'json').mockRejectedValueOnce(error);
describe('response payload is not valid JSON', () => {
it('should return string response from fetch api', async () => {
vi.spyOn(mockResponse, 'text').mockResolvedValueOnce(textResponse);

expect(await httpClient.post(url, body, init)).toEqual({ data: '' });
expect(await httpClient.post(url, body, init)).toEqual({ data: textResponse });
});
});
});
Expand All @@ -186,17 +192,16 @@ describe('httpClient', () => {
});

describe('response payload is valid JSON', () => {
it('should return response from fetch api', async () => {
expect(await httpClient.put(url, body, init)).toEqual({ data: response });
it('should return parsed response from fetch api', async () => {
expect(await httpClient.put(url, body, init)).toEqual({ data: JSON.parse(jsonResponse) });
});
});

describe('response payload can not be parsed as JSON', () => {
it('should return empty string response from fetch api', async () => {
const error = new Error();
vi.spyOn(mockResponse, 'json').mockRejectedValueOnce(error);
describe('response payload is not valid JSON', () => {
it('should return string response from fetch api', async () => {
vi.spyOn(mockResponse, 'text').mockResolvedValueOnce(textResponse);

expect(await httpClient.put(url, body, init)).toEqual({ data: '' });
expect(await httpClient.put(url, body, init)).toEqual({ data: textResponse });
});
});
});
Expand Down

0 comments on commit b22e5d0

Please sign in to comment.