Fix link preview race condition
This commit is contained in:
parent
66d9bb8418
commit
6642652bdb
3 changed files with 77 additions and 2 deletions
|
@ -1354,5 +1354,70 @@ describe('link preview fetching', () => {
|
|||
})
|
||||
);
|
||||
});
|
||||
|
||||
it("doesn't read the image if the request was aborted before reading started", async () => {
|
||||
const abortController = new AbortController();
|
||||
|
||||
const fixture = await readFixture('kitten-1-64-64.jpg');
|
||||
|
||||
const fakeFetch = stub().callsFake(() => {
|
||||
const response = new Response(fixture, {
|
||||
headers: {
|
||||
'Content-Type': 'image/jpeg',
|
||||
'Content-Length': fixture.length.toString(),
|
||||
},
|
||||
});
|
||||
sinon
|
||||
.stub(response, 'arrayBuffer')
|
||||
.rejects(new Error('Should not be called'));
|
||||
sinon.stub(response, 'blob').rejects(new Error('Should not be called'));
|
||||
sinon.stub(response, 'text').rejects(new Error('Should not be called'));
|
||||
sinon.stub(response, 'body').get(() => {
|
||||
throw new Error('Should not be accessed');
|
||||
});
|
||||
|
||||
abortController.abort();
|
||||
|
||||
return response;
|
||||
});
|
||||
|
||||
assert.isNull(
|
||||
await fetchLinkPreviewImage(
|
||||
fakeFetch,
|
||||
'https://example.com/img',
|
||||
abortController.signal
|
||||
)
|
||||
);
|
||||
});
|
||||
|
||||
it('returns null if the request was aborted after the image was read', async () => {
|
||||
const abortController = new AbortController();
|
||||
|
||||
const fixture = await readFixture('kitten-1-64-64.jpg');
|
||||
|
||||
const fakeFetch = stub().callsFake(() => {
|
||||
const response = new Response(fixture, {
|
||||
headers: {
|
||||
'Content-Type': 'image/jpeg',
|
||||
'Content-Length': fixture.length.toString(),
|
||||
},
|
||||
});
|
||||
const oldArrayBufferMethod = response.arrayBuffer.bind(response);
|
||||
sinon.stub(response, 'arrayBuffer').callsFake(async () => {
|
||||
const data = await oldArrayBufferMethod();
|
||||
abortController.abort();
|
||||
return data;
|
||||
});
|
||||
return response;
|
||||
});
|
||||
|
||||
assert.isNull(
|
||||
await fetchLinkPreviewImage(
|
||||
fakeFetch,
|
||||
'https://example.com/img',
|
||||
abortController.signal
|
||||
)
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue