|
|
|
@ -4,7 +4,7 @@
|
|
|
|
|
// import * as path from 'path';
|
|
|
|
|
// import AbortController from 'abort-controller';
|
|
|
|
|
// import { IMAGE_JPEG, MIMEType } from '../../../types/MIME';
|
|
|
|
|
// FIXME audric enable back those test
|
|
|
|
|
// // FIXME audric enable back those test
|
|
|
|
|
// import {
|
|
|
|
|
// fetchLinkPreviewImage,
|
|
|
|
|
// fetchLinkPreviewMetadata,
|
|
|
|
@ -12,29 +12,25 @@
|
|
|
|
|
// import { TestUtils } from '../../test-utils';
|
|
|
|
|
// import { globalAny } from '../../test-utils/utils';
|
|
|
|
|
|
|
|
|
|
// // tslint:disable: no-http-string
|
|
|
|
|
// tslint:disable: no-http-string
|
|
|
|
|
|
|
|
|
|
// describe('link preview fetching', () => {
|
|
|
|
|
// // We'll use this to create a fake `fetch`. We'll want to call `.resolves` or
|
|
|
|
|
// // `.rejects` on it (meaning that it needs to be a Sinon Stub type), but we'll also
|
|
|
|
|
// // want it to be a fake `fetch`. `any` seems like the best "supertype" there.
|
|
|
|
|
// // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
describe('link preview fetching', () => {
|
|
|
|
|
// We'll use this to create a fake `fetch`. We'll want to call `.resolves` or
|
|
|
|
|
// `.rejects` on it (meaning that it needs to be a Sinon Stub type), but we'll also
|
|
|
|
|
// want it to be a fake `fetch`. `any` seems like the best "supertype" there.
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
// function stub(): any {
|
|
|
|
|
// return sinon.stub();
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// let sandbox: sinon.SinonSandbox;
|
|
|
|
|
// let warn: sinon.SinonStub;
|
|
|
|
|
|
|
|
|
|
// beforeEach(() => {
|
|
|
|
|
// sandbox = sinon.createSandbox();
|
|
|
|
|
// warn = sandbox.stub(window.log, 'warn');
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// afterEach(() => {
|
|
|
|
|
// sandbox.restore();
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// // tslint:disable-next-line: max-func-body-length
|
|
|
|
|
// describe('fetchLinkPreviewMetadata', () => {
|
|
|
|
|
// const makeHtml = (stuffInHead: ReadonlyArray<string> = []) => `
|
|
|
|
@ -44,7 +40,6 @@
|
|
|
|
|
// <body>should be ignored</body>
|
|
|
|
|
// </html>
|
|
|
|
|
// `;
|
|
|
|
|
|
|
|
|
|
// const makeResponse = ({
|
|
|
|
|
// status = 200,
|
|
|
|
|
// headers = {},
|
|
|
|
@ -76,7 +71,6 @@
|
|
|
|
|
// bodyLength = null;
|
|
|
|
|
// bodyStream = body;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// const headersObj = new Headers();
|
|
|
|
|
// Object.entries({
|
|
|
|
|
// 'Content-Type': 'text/html; charset=utf-8',
|
|
|
|
@ -87,7 +81,6 @@
|
|
|
|
|
// headersObj.set(headerName, headerValue);
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// return {
|
|
|
|
|
// headers: headersObj,
|
|
|
|
|
// body: bodyStream,
|
|
|
|
@ -96,7 +89,6 @@
|
|
|
|
|
// url,
|
|
|
|
|
// };
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
// it('handles the "kitchen sink" of results', async () => {
|
|
|
|
|
// const fakeFetch = stub().resolves(
|
|
|
|
|
// makeResponse({
|
|
|
|
@ -108,7 +100,6 @@
|
|
|
|
|
// ]),
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// assert.deepEqual(
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -123,7 +114,6 @@
|
|
|
|
|
// }
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('logs no warnings if everything goes smoothly', async () => {
|
|
|
|
|
// const fakeFetch = stub().resolves(
|
|
|
|
|
// makeResponse({
|
|
|
|
@ -135,25 +125,20 @@
|
|
|
|
|
// ]),
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
|
// 'https://example.com',
|
|
|
|
|
// new AbortController().signal
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// sinon.assert.notCalled(warn);
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('sends "WhatsApp" as the User-Agent for compatibility', async () => {
|
|
|
|
|
// const fakeFetch = stub().resolves(makeResponse());
|
|
|
|
|
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
|
// 'https://example.com',
|
|
|
|
|
// new AbortController().signal
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// sinon.assert.calledWith(
|
|
|
|
|
// fakeFetch,
|
|
|
|
|
// 'https://example.com',
|
|
|
|
@ -164,10 +149,8 @@
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('returns null if the request fails', async () => {
|
|
|
|
|
// const fakeFetch = stub().rejects(new Error('Test request failure'));
|
|
|
|
|
|
|
|
|
|
// assert.isNull(
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -175,19 +158,16 @@
|
|
|
|
|
// new AbortController().signal
|
|
|
|
|
// )
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// sinon.assert.calledOnce(warn);
|
|
|
|
|
// sinon.assert.calledWith(
|
|
|
|
|
// warn,
|
|
|
|
|
// 'fetchLinkPreviewMetadata: failed to fetch link preview HTML; bailing'
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it("returns null if the response status code isn't 2xx", async () => {
|
|
|
|
|
// await Promise.all(
|
|
|
|
|
// [100, 304, 400, 404, 500, 0, -200].map(async status => {
|
|
|
|
|
// const fakeFetch = stub().resolves(makeResponse({ status }));
|
|
|
|
|
|
|
|
|
|
// assert.isNull(
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -195,7 +175,6 @@
|
|
|
|
|
// new AbortController().signal
|
|
|
|
|
// )
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// sinon.assert.calledWith(
|
|
|
|
|
// warn,
|
|
|
|
|
// `fetchLinkPreviewMetadata: got a ${status} status code; bailing`
|
|
|
|
@ -203,23 +182,19 @@
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it("doesn't use fetch's automatic redirection behavior", async () => {
|
|
|
|
|
// const fakeFetch = stub().resolves(makeResponse());
|
|
|
|
|
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
|
// 'https://example.com',
|
|
|
|
|
// new AbortController().signal
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// sinon.assert.calledWith(
|
|
|
|
|
// fakeFetch,
|
|
|
|
|
// 'https://example.com',
|
|
|
|
|
// sinon.match({ redirect: 'manual' })
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// [301, 302, 303, 307, 308].forEach(status => {
|
|
|
|
|
// it(`handles ${status} redirects`, async () => {
|
|
|
|
|
// const fakeFetch = stub();
|
|
|
|
@ -231,7 +206,6 @@
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
// fakeFetch.onSecondCall().resolves(makeResponse());
|
|
|
|
|
|
|
|
|
|
// assert.deepEqual(
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -245,15 +219,12 @@
|
|
|
|
|
// imageHref: null,
|
|
|
|
|
// }
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// sinon.assert.calledTwice(fakeFetch);
|
|
|
|
|
// sinon.assert.calledWith(fakeFetch.getCall(0), 'https://example.com');
|
|
|
|
|
// sinon.assert.calledWith(fakeFetch.getCall(1), 'https://example.com/2');
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it(`returns null when seeing a ${status} status with no Location header`, async () => {
|
|
|
|
|
// const fakeFetch = stub().resolves(makeResponse({ status }));
|
|
|
|
|
|
|
|
|
|
// assert.isNull(
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -263,7 +234,6 @@
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('handles relative redirects', async () => {
|
|
|
|
|
// const fakeFetch = stub();
|
|
|
|
|
// fakeFetch.onFirstCall().resolves(
|
|
|
|
@ -281,7 +251,6 @@
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
// fakeFetch.onThirdCall().resolves(makeResponse());
|
|
|
|
|
|
|
|
|
|
// assert.deepEqual(
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -295,13 +264,11 @@
|
|
|
|
|
// imageHref: null,
|
|
|
|
|
// }
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// sinon.assert.calledThrice(fakeFetch);
|
|
|
|
|
// sinon.assert.calledWith(fakeFetch.getCall(0), 'https://example.com');
|
|
|
|
|
// sinon.assert.calledWith(fakeFetch.getCall(1), 'https://example.com/2/');
|
|
|
|
|
// sinon.assert.calledWith(fakeFetch.getCall(2), 'https://example.com/2/3');
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('returns null if redirecting to an insecure HTTP URL', async () => {
|
|
|
|
|
// const fakeFetch = stub().resolves(
|
|
|
|
|
// makeResponse({
|
|
|
|
@ -310,7 +277,6 @@
|
|
|
|
|
// body: null,
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// assert.isNull(
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -318,10 +284,8 @@
|
|
|
|
|
// new AbortController().signal
|
|
|
|
|
// )
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// sinon.assert.calledOnce(fakeFetch);
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it("returns null if there's a redirection loop", async () => {
|
|
|
|
|
// const fakeFetch = stub();
|
|
|
|
|
// fakeFetch.onFirstCall().resolves(
|
|
|
|
@ -338,7 +302,6 @@
|
|
|
|
|
// body: null,
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// assert.isNull(
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -346,10 +309,8 @@
|
|
|
|
|
// new AbortController().signal
|
|
|
|
|
// )
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// sinon.assert.calledTwice(fakeFetch);
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('returns null if redirecting more than 20 times', async () => {
|
|
|
|
|
// const fakeFetch = stub().callsFake(async () =>
|
|
|
|
|
// makeResponse({
|
|
|
|
@ -359,7 +320,6 @@
|
|
|
|
|
// body: null,
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// assert.isNull(
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -367,13 +327,10 @@
|
|
|
|
|
// new AbortController().signal
|
|
|
|
|
// )
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// sinon.assert.callCount(fakeFetch, 20);
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('returns null if the response has no body', async () => {
|
|
|
|
|
// const fakeFetch = stub().resolves(makeResponse({ body: null }));
|
|
|
|
|
|
|
|
|
|
// assert.isNull(
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -381,16 +338,13 @@
|
|
|
|
|
// new AbortController().signal
|
|
|
|
|
// )
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// sinon.assert.calledWith(
|
|
|
|
|
// warn,
|
|
|
|
|
// 'fetchLinkPreviewMetadata: no response body; bailing'
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('returns null if the result body is too short', async () => {
|
|
|
|
|
// const fakeFetch = stub().resolves(makeResponse({ body: '<title>' }));
|
|
|
|
|
|
|
|
|
|
// assert.isNull(
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -398,21 +352,18 @@
|
|
|
|
|
// new AbortController().signal
|
|
|
|
|
// )
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// sinon.assert.calledOnce(warn);
|
|
|
|
|
// sinon.assert.calledWith(
|
|
|
|
|
// warn,
|
|
|
|
|
// 'fetchLinkPreviewMetadata: Content-Length is too short; bailing'
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('returns null if the result is meant to be downloaded', async () => {
|
|
|
|
|
// const fakeFetch = stub().resolves(
|
|
|
|
|
// makeResponse({
|
|
|
|
|
// headers: { 'Content-Disposition': 'attachment' },
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// assert.isNull(
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -420,21 +371,18 @@
|
|
|
|
|
// new AbortController().signal
|
|
|
|
|
// )
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// sinon.assert.calledOnce(warn);
|
|
|
|
|
// sinon.assert.calledWith(
|
|
|
|
|
// warn,
|
|
|
|
|
// 'fetchLinkPreviewMetadata: Content-Disposition header is not inline; bailing'
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('allows an explitly inline Content-Disposition header', async () => {
|
|
|
|
|
// const fakeFetch = stub().resolves(
|
|
|
|
|
// makeResponse({
|
|
|
|
|
// headers: { 'Content-Disposition': 'inline' },
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// assert.deepEqual(
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -449,14 +397,12 @@
|
|
|
|
|
// }
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('returns null if the Content-Type is not HTML', async () => {
|
|
|
|
|
// const fakeFetch = stub().resolves(
|
|
|
|
|
// makeResponse({
|
|
|
|
|
// headers: { 'Content-Type': 'text/plain' },
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// assert.isNull(
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -464,21 +410,18 @@
|
|
|
|
|
// new AbortController().signal
|
|
|
|
|
// )
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// sinon.assert.calledOnce(warn);
|
|
|
|
|
// sinon.assert.calledWith(
|
|
|
|
|
// warn,
|
|
|
|
|
// 'fetchLinkPreviewMetadata: Content-Type is not HTML; bailing'
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('accepts non-lowercase Content-Type headers', async () => {
|
|
|
|
|
// const fakeFetch = stub().resolves(
|
|
|
|
|
// makeResponse({
|
|
|
|
|
// headers: { 'Content-Type': 'TEXT/HTML; chArsEt=utf-8' },
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// assert.deepEqual(
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -493,7 +436,6 @@
|
|
|
|
|
// }
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('parses the response as UTF-8 if the body contains a byte order mark', async () => {
|
|
|
|
|
// const fakeFetch = stub().resolves(
|
|
|
|
|
// makeResponse({
|
|
|
|
@ -508,7 +450,6 @@
|
|
|
|
|
// })(),
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// assert.deepEqual(
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -523,11 +464,9 @@
|
|
|
|
|
// }
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('respects the UTF-8 byte order mark above the Content-Type header', async () => {
|
|
|
|
|
// const bom = new Uint8Array([0xef, 0xbb, 0xbf]);
|
|
|
|
|
// const titleHtml = new TextEncoder().encode('<title>\u{1F389}</title>');
|
|
|
|
|
|
|
|
|
|
// const fakeFetch = stub().resolves(
|
|
|
|
|
// makeResponse({
|
|
|
|
|
// headers: {
|
|
|
|
@ -549,12 +488,10 @@
|
|
|
|
|
// '🎉'
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('respects the UTF-8 byte order mark above a <meta http-equiv> in the document', async () => {
|
|
|
|
|
// const bom = new Uint8Array([0xef, 0xbb, 0xbf]);
|
|
|
|
|
// const titleHtml = new TextEncoder().encode('<title>\u{1F389}</title>');
|
|
|
|
|
// const endHeadHtml = new TextEncoder().encode('</head>');
|
|
|
|
|
|
|
|
|
|
// const fakeFetch = stub().resolves(
|
|
|
|
|
// makeResponse({
|
|
|
|
|
// headers: {
|
|
|
|
@ -580,12 +517,10 @@
|
|
|
|
|
// '🎉'
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('respects the UTF-8 byte order mark above a <meta charset> in the document', async () => {
|
|
|
|
|
// const bom = new Uint8Array([0xef, 0xbb, 0xbf]);
|
|
|
|
|
// const titleHtml = new TextEncoder().encode('<title>\u{1F389}</title>');
|
|
|
|
|
// const endHeadHtml = new TextEncoder().encode('</head>');
|
|
|
|
|
|
|
|
|
|
// const fakeFetch = stub().resolves(
|
|
|
|
|
// makeResponse({
|
|
|
|
|
// headers: {
|
|
|
|
@ -611,11 +546,9 @@
|
|
|
|
|
// '🎉'
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('respects the Content-Type header above anything in the HTML', async () => {
|
|
|
|
|
// const titleHtml = new TextEncoder().encode('<title>\u{1F389}</title>');
|
|
|
|
|
// const endHeadHtml = new TextEncoder().encode('</head>');
|
|
|
|
|
|
|
|
|
|
// {
|
|
|
|
|
// const fakeFetch = stub().resolves(
|
|
|
|
|
// makeResponse({
|
|
|
|
@ -641,7 +574,6 @@
|
|
|
|
|
// '🎉'
|
|
|
|
|
// );
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// {
|
|
|
|
|
// const fakeFetch = stub().resolves(
|
|
|
|
|
// makeResponse({
|
|
|
|
@ -668,7 +600,6 @@
|
|
|
|
|
// );
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('prefers the Content-Type http-equiv in the HTML above <meta charset>', async () => {
|
|
|
|
|
// const fakeFetch = stub().resolves(
|
|
|
|
|
// makeResponse({
|
|
|
|
@ -682,7 +613,6 @@
|
|
|
|
|
// ]),
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// assert.propertyVal(
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -693,7 +623,6 @@
|
|
|
|
|
// '🎉'
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('parses non-UTF8 encodings', async () => {
|
|
|
|
|
// const titleBytes = new Uint8Array([0x61, 0x71, 0x75, 0xed]);
|
|
|
|
|
// assert.notDeepEqual(
|
|
|
|
@ -701,7 +630,6 @@
|
|
|
|
|
// new TextDecoder('latin1').decode(titleBytes),
|
|
|
|
|
// 'Test data was not set up correctly'
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// const fakeFetch = stub().resolves(
|
|
|
|
|
// makeResponse({
|
|
|
|
|
// headers: {
|
|
|
|
@ -714,7 +642,6 @@
|
|
|
|
|
// })(),
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// assert.propertyVal(
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -725,7 +652,6 @@
|
|
|
|
|
// 'aquí'
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('handles incomplete bodies', async () => {
|
|
|
|
|
// const fakeFetch = stub().resolves(
|
|
|
|
|
// makeResponse({
|
|
|
|
@ -737,7 +663,6 @@
|
|
|
|
|
// })(),
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// assert.propertyVal(
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -747,19 +672,15 @@
|
|
|
|
|
// 'title',
|
|
|
|
|
// 'foo bar'
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// sinon.assert.calledOnce(warn);
|
|
|
|
|
// sinon.assert.calledWith(
|
|
|
|
|
// warn,
|
|
|
|
|
// 'getHtmlDocument: error when reading body; continuing with what we got'
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('stops reading the body after cancelation', async () => {
|
|
|
|
|
// const shouldNeverBeCalled = sinon.stub();
|
|
|
|
|
|
|
|
|
|
// const abortController = new AbortController();
|
|
|
|
|
|
|
|
|
|
// const fakeFetch = stub().resolves(
|
|
|
|
|
// makeResponse({
|
|
|
|
|
// body: (async function* body() {
|
|
|
|
@ -770,7 +691,6 @@
|
|
|
|
|
// })(),
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// assert.isNull(
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -778,13 +698,10 @@
|
|
|
|
|
// abortController.signal
|
|
|
|
|
// )
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// sinon.assert.notCalled(shouldNeverBeCalled);
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('stops reading bodies after 500 kilobytes', async () => {
|
|
|
|
|
// const shouldNeverBeCalled = sinon.stub();
|
|
|
|
|
|
|
|
|
|
// const fakeFetch = stub().resolves(
|
|
|
|
|
// makeResponse({
|
|
|
|
|
// body: (async function* body() {
|
|
|
|
@ -802,7 +719,6 @@
|
|
|
|
|
// })(),
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// assert.deepEqual(
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -816,10 +732,8 @@
|
|
|
|
|
// imageHref: null,
|
|
|
|
|
// }
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// sinon.assert.notCalled(shouldNeverBeCalled);
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it("returns null if the HTML doesn't contain a title, even if it contains other values", async () => {
|
|
|
|
|
// const fakeFetch = stub().resolves(
|
|
|
|
|
// makeResponse({
|
|
|
|
@ -830,7 +744,6 @@
|
|
|
|
|
// ]),
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// assert.isNull(
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -838,14 +751,12 @@
|
|
|
|
|
// new AbortController().signal
|
|
|
|
|
// )
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// sinon.assert.calledOnce(warn);
|
|
|
|
|
// sinon.assert.calledWith(
|
|
|
|
|
// warn,
|
|
|
|
|
// "parseMetadata: HTML document doesn't have a title; bailing"
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('prefers og:title to document.title', async () => {
|
|
|
|
|
// const fakeFetch = stub().resolves(
|
|
|
|
|
// makeResponse({
|
|
|
|
@ -855,7 +766,6 @@
|
|
|
|
|
// ]),
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// assert.propertyVal(
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -866,7 +776,6 @@
|
|
|
|
|
// 'foo bar'
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('prefers og:description to <meta name="description">', async () => {
|
|
|
|
|
// const fakeFetch = stub().resolves(
|
|
|
|
|
// makeResponse({
|
|
|
|
@ -877,7 +786,6 @@
|
|
|
|
|
// ]),
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// assert.propertyVal(
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -888,7 +796,6 @@
|
|
|
|
|
// 'bar'
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('parses <meta name="description">', async () => {
|
|
|
|
|
// const fakeFetch = stub().resolves(
|
|
|
|
|
// makeResponse({
|
|
|
|
@ -898,7 +805,6 @@
|
|
|
|
|
// ]),
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// assert.propertyVal(
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -909,7 +815,6 @@
|
|
|
|
|
// 'bar'
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('ignores empty descriptions', async () => {
|
|
|
|
|
// const fakeFetch = stub().resolves(
|
|
|
|
|
// makeResponse({
|
|
|
|
@ -919,7 +824,6 @@
|
|
|
|
|
// ]),
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// assert.propertyVal(
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -930,7 +834,6 @@
|
|
|
|
|
// null
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('parses absolute image URLs', async () => {
|
|
|
|
|
// const fakeFetch = stub().resolves(
|
|
|
|
|
// makeResponse({
|
|
|
|
@ -940,7 +843,6 @@
|
|
|
|
|
// ]),
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// assert.propertyVal(
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -951,7 +853,6 @@
|
|
|
|
|
// 'https://example.com/image.jpg'
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('parses relative image URLs', async () => {
|
|
|
|
|
// const fakeFetch = stub().resolves(
|
|
|
|
|
// makeResponse({
|
|
|
|
@ -961,7 +862,6 @@
|
|
|
|
|
// ]),
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// assert.propertyVal(
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -972,7 +872,6 @@
|
|
|
|
|
// 'https://example.com/assets/image.jpg'
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('relative image URL resolution is relative to the final URL after redirects, not the original URL', async () => {
|
|
|
|
|
// const fakeFetch = stub().resolves(
|
|
|
|
|
// makeResponse({
|
|
|
|
@ -983,7 +882,6 @@
|
|
|
|
|
// url: 'https://bar.example/assets/',
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// assert.propertyVal(
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -994,7 +892,6 @@
|
|
|
|
|
// 'https://bar.example/assets/image.jpg'
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('ignores empty image URLs', async () => {
|
|
|
|
|
// const fakeFetch = stub().resolves(
|
|
|
|
|
// makeResponse({
|
|
|
|
@ -1004,7 +901,6 @@
|
|
|
|
|
// ]),
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// assert.propertyVal(
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -1015,7 +911,6 @@
|
|
|
|
|
// null
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('ignores blank image URLs', async () => {
|
|
|
|
|
// const fakeFetch = stub().resolves(
|
|
|
|
|
// makeResponse({
|
|
|
|
@ -1025,7 +920,6 @@
|
|
|
|
|
// ]),
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// assert.propertyVal(
|
|
|
|
|
// await fetchLinkPreviewMetadata(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -1037,7 +931,6 @@
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// // tslint:disable-next-line: max-func-body-length
|
|
|
|
|
// describe('fetchLinkPreviewImage', () => {
|
|
|
|
|
// const readFixture = async (filename: string): Promise<Uint8Array> => {
|
|
|
|
@ -1047,7 +940,6 @@
|
|
|
|
|
// assert(result.length > 10, `Test failed to read fixture ${filename}`);
|
|
|
|
|
// return result;
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
// [
|
|
|
|
|
// {
|
|
|
|
|
// title: 'JPEG',
|
|
|
|
@ -1078,7 +970,6 @@
|
|
|
|
|
// ].forEach(({ title, contentType, fixtureFilename }) => {
|
|
|
|
|
// it(`handles ${title} images`, async () => {
|
|
|
|
|
// const fixture = await readFixture(fixtureFilename);
|
|
|
|
|
|
|
|
|
|
// const fakeFetch = stub().resolves(
|
|
|
|
|
// new Response(fixture, {
|
|
|
|
|
// headers: {
|
|
|
|
@ -1087,7 +978,6 @@
|
|
|
|
|
// },
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// assert.deepEqual(
|
|
|
|
|
// await fetchLinkPreviewImage(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -1101,10 +991,8 @@
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('returns null if the request fails', async () => {
|
|
|
|
|
// const fakeFetch = stub().rejects(new Error('Test request failure'));
|
|
|
|
|
|
|
|
|
|
// assert.isNull(
|
|
|
|
|
// await fetchLinkPreviewImage(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -1112,17 +1000,14 @@
|
|
|
|
|
// new AbortController().signal
|
|
|
|
|
// )
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// sinon.assert.calledOnce(warn);
|
|
|
|
|
// sinon.assert.calledWith(
|
|
|
|
|
// warn,
|
|
|
|
|
// 'fetchLinkPreviewImage: failed to fetch image; bailing'
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it("returns null if the response status code isn't 2xx", async () => {
|
|
|
|
|
// const fixture = await readFixture('kitten-1-64-64.jpg');
|
|
|
|
|
|
|
|
|
|
// await Promise.all(
|
|
|
|
|
// [400, 404, 500, 598].map(async status => {
|
|
|
|
|
// const fakeFetch = stub().resolves(
|
|
|
|
@ -1134,7 +1019,6 @@
|
|
|
|
|
// },
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// assert.isNull(
|
|
|
|
|
// await fetchLinkPreviewImage(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -1142,7 +1026,6 @@
|
|
|
|
|
// new AbortController().signal
|
|
|
|
|
// )
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// sinon.assert.calledWith(
|
|
|
|
|
// warn,
|
|
|
|
|
// `fetchLinkPreviewImage: got a ${status} status code; bailing`
|
|
|
|
@ -1150,11 +1033,9 @@
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// // Most of the redirect behavior is tested above.
|
|
|
|
|
// it('handles 301 redirects', async () => {
|
|
|
|
|
// const fixture = await readFixture('kitten-1-64-64.jpg');
|
|
|
|
|
|
|
|
|
|
// const fakeFetch = stub();
|
|
|
|
|
// fakeFetch.onFirstCall().resolves(
|
|
|
|
|
// new Response(null, {
|
|
|
|
@ -1172,7 +1053,6 @@
|
|
|
|
|
// },
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// assert.deepEqual(
|
|
|
|
|
// await fetchLinkPreviewImage(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -1184,7 +1064,6 @@
|
|
|
|
|
// contentType: IMAGE_JPEG,
|
|
|
|
|
// }
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// sinon.assert.calledTwice(fakeFetch);
|
|
|
|
|
// sinon.assert.calledWith(fakeFetch.getCall(0), 'https://example.com/img');
|
|
|
|
|
// sinon.assert.calledWith(
|
|
|
|
@ -1192,7 +1071,6 @@
|
|
|
|
|
// 'https://example.com/result.jpg'
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('returns null if the response is too small', async () => {
|
|
|
|
|
// const fakeFetch = stub().resolves(
|
|
|
|
|
// new Response(await readFixture('kitten-1-64-64.jpg'), {
|
|
|
|
@ -1202,7 +1080,6 @@
|
|
|
|
|
// },
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// assert.isNull(
|
|
|
|
|
// await fetchLinkPreviewImage(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -1210,14 +1087,12 @@
|
|
|
|
|
// new AbortController().signal
|
|
|
|
|
// )
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// sinon.assert.calledOnce(warn);
|
|
|
|
|
// sinon.assert.calledWith(
|
|
|
|
|
// warn,
|
|
|
|
|
// 'fetchLinkPreviewImage: Content-Length is too short; bailing'
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('returns null if the response is too large', async () => {
|
|
|
|
|
// const fakeFetch = stub().resolves(
|
|
|
|
|
// new Response(await readFixture('kitten-1-64-64.jpg'), {
|
|
|
|
@ -1227,7 +1102,6 @@
|
|
|
|
|
// },
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// assert.isNull(
|
|
|
|
|
// await fetchLinkPreviewImage(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -1235,17 +1109,14 @@
|
|
|
|
|
// new AbortController().signal
|
|
|
|
|
// )
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// sinon.assert.calledOnce(warn);
|
|
|
|
|
// sinon.assert.calledWith(
|
|
|
|
|
// warn,
|
|
|
|
|
// 'fetchLinkPreviewImage: Content-Length is too large or is unset; bailing'
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('returns null if the Content-Type is not a valid image', async () => {
|
|
|
|
|
// const fixture = await readFixture('kitten-1-64-64.jpg');
|
|
|
|
|
|
|
|
|
|
// await Promise.all(
|
|
|
|
|
// ['', 'image/tiff', 'video/mp4', 'text/plain', 'application/html'].map(
|
|
|
|
|
// async contentType => {
|
|
|
|
@ -1257,7 +1128,6 @@
|
|
|
|
|
// },
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// assert.isNull(
|
|
|
|
|
// await fetchLinkPreviewImage(
|
|
|
|
|
// fakeFetch,
|
|
|
|
@ -1265,7 +1135,6 @@
|
|
|
|
|
// new AbortController().signal
|
|
|
|
|
// )
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// sinon.assert.calledWith(
|
|
|
|
|
// warn,
|
|
|
|
|
// 'fetchLinkPreviewImage: Content-Type is not an image; bailing'
|
|
|
|
@ -1274,16 +1143,13 @@
|
|
|
|
|
// )
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// it('sends "WhatsApp" as the User-Agent for compatibility', async () => {
|
|
|
|
|
// const fakeFetch = stub().resolves(new Response(null));
|
|
|
|
|
|
|
|
|
|
// await fetchLinkPreviewImage(
|
|
|
|
|
// fakeFetch,
|
|
|
|
|
// 'https://example.com/img',
|
|
|
|
|
// new AbortController().signal
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// sinon.assert.calledWith(
|
|
|
|
|
// fakeFetch,
|
|
|
|
|
// 'https://example.com/img',
|
|
|
|
@ -1295,4 +1161,4 @@
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
// });
|
|
|
|
|
// });
|
|
|
|
|
});
|
|
|
|
|