Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fetchOEmbedData): Handle non-ok responses #104

Merged
merged 2 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/__tests__/transformers/GIPHY.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import {

import { cache, getMarkdownASTForFile, parseASTToMarkdown } from '../helpers';

const { Response } = jest.requireActual('node-fetch');
jest.mock('node-fetch', () => jest.fn());

const mockFetch = ({ height, width }) =>
fetchMock.mockResolvedValue({
json: () => Promise.resolve({ height, width }),
});
fetchMock.mockImplementation(() =>
Promise.resolve(new Response(JSON.stringify({ height, width })))
);

beforeEach(() => {
fetchMock.mockClear();
Expand Down
5 changes: 4 additions & 1 deletion src/__tests__/transformers/Instagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import { getHTML, shouldTransform } from '../../transformers/Instagram';

import { cache, getMarkdownASTForFile, parseASTToMarkdown } from '../helpers';

const { Response } = jest.requireActual('node-fetch');
jest.mock('node-fetch', () => jest.fn());

const mockFetch = html =>
fetchMock.mockResolvedValue({ json: () => Promise.resolve({ html }) });
fetchMock.mockImplementation(() =>
Promise.resolve(new Response(JSON.stringify({ html })))
);

beforeEach(() => {
fetchMock.mockClear();
Expand Down
5 changes: 4 additions & 1 deletion src/__tests__/transformers/Streamable.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ import {

import { cache, getMarkdownASTForFile, parseASTToMarkdown } from '../helpers';

const { Response } = jest.requireActual('node-fetch');
jest.mock('node-fetch', () => jest.fn());

const mockFetch = html =>
fetchMock.mockResolvedValue({ json: () => Promise.resolve({ html }) });
fetchMock.mockImplementation(() =>
Promise.resolve(new Response(JSON.stringify({ html })))
);

beforeEach(() => {
fetchMock.mockClear();
Expand Down
13 changes: 7 additions & 6 deletions src/__tests__/transformers/Twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import { getHTML, shouldTransform } from '../../transformers/Twitter';

import { cache, getMarkdownASTForFile, parseASTToMarkdown } from '../helpers';

const { Response } = jest.requireActual('node-fetch');
jest.mock('node-fetch', () => jest.fn());

const mockFetch = (status, moment) =>
fetchMock
.mockResolvedValueOnce({ json: () => Promise.resolve({ html: status }) })
.mockResolvedValueOnce({ json: () => Promise.resolve({ html: status }) })
.mockResolvedValueOnce({ json: () => Promise.resolve({ html: moment }) })
.mockResolvedValueOnce({ json: () => Promise.resolve({ html: moment }) })
.mockResolvedValueOnce({ json: () => Promise.resolve({ html: moment }) })
.mockResolvedValueOnce({ json: () => Promise.resolve({ html: moment }) });
.mockResolvedValueOnce(new Response(JSON.stringify({ html: status })))
.mockResolvedValueOnce(new Response(JSON.stringify({ html: status })))
.mockResolvedValueOnce(new Response(JSON.stringify({ html: moment })))
.mockResolvedValueOnce(new Response(JSON.stringify({ html: moment })))
.mockResolvedValueOnce(new Response(JSON.stringify({ html: moment })))
.mockResolvedValueOnce(new Response(JSON.stringify({ html: moment })));

beforeEach(() => {
fetchMock.mockReset();
Expand Down
19 changes: 19 additions & 0 deletions src/__tests__/transformers/utils/fetchOEmbedData.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ describe(`fetchOEmbedData`, () => {
});
});

test(`throws on non-ok status`, () => {
// make sure we make our result assertion
expect.assertions(1);

fetch.mockResolvedValue(
new Response(JSON.stringify(MockedResponseResult), {
// non-ok status is one outside of 200-299 range
// ( https://developer.mozilla.org/en-US/docs/Web/API/Response/ok )
status: 403,
})
);

return fetchOEmbedData(URL).catch(err => {
expect(err).toMatchInlineSnapshot(
`[Error: Request to https://google.com returned non-OK status (403)]`
);
});
});

describe(`network resilience`, () => {
test(`retries requests if there was network error`, () => {
// make sure we make our result assertion
Expand Down
12 changes: 11 additions & 1 deletion src/transformers/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@ export const fetchOEmbedData = url =>
fetchWithRetries(url, {
retries: 3,
retryDelay: attempt => 2 ** attempt * 1000,
}).then(data => data.json());
})
.then(response => {
if (!response.ok) {
throw new Error(
`Request to ${url} returned non-OK status (${response.status})`
);
}

return response;
})
.then(data => data.json());

export const getTrimmedPathName = pathname =>
// Trim leading and trailing slashes
Expand Down