feat: improve error message for response.buffer() (#7669)
This commit is contained in:
parent
4d9dc8c0e6
commit
03c9ecca40
@ -18,6 +18,7 @@ import { Frame } from './FrameManager.js';
|
||||
import { HTTPRequest } from './HTTPRequest.js';
|
||||
import { SecurityDetails } from './SecurityDetails.js';
|
||||
import { Protocol } from 'devtools-protocol';
|
||||
import { ProtocolError } from './Errors.js';
|
||||
|
||||
/**
|
||||
* @public
|
||||
@ -147,13 +148,26 @@ export class HTTPResponse {
|
||||
if (!this._contentPromise) {
|
||||
this._contentPromise = this._bodyLoadedPromise.then(async (error) => {
|
||||
if (error) throw error;
|
||||
const response = await this._client.send('Network.getResponseBody', {
|
||||
requestId: this._request._requestId,
|
||||
});
|
||||
return Buffer.from(
|
||||
response.body,
|
||||
response.base64Encoded ? 'base64' : 'utf8'
|
||||
);
|
||||
try {
|
||||
const response = await this._client.send('Network.getResponseBody', {
|
||||
requestId: this._request._requestId,
|
||||
});
|
||||
return Buffer.from(
|
||||
response.body,
|
||||
response.base64Encoded ? 'base64' : 'utf8'
|
||||
);
|
||||
} catch (error) {
|
||||
if (
|
||||
error instanceof ProtocolError &&
|
||||
error.originalMessage === 'No resource with given identifier found'
|
||||
) {
|
||||
throw new ProtocolError(
|
||||
'Could not load body for this request. This might happen if the request is a preflight request.'
|
||||
);
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
}
|
||||
return this._contentPromise;
|
||||
|
@ -25,6 +25,7 @@ import {
|
||||
itFailsFirefox,
|
||||
describeFailsFirefox,
|
||||
} from './mocha-utils'; // eslint-disable-line import/extensions
|
||||
import { HTTPResponse } from '../lib/cjs/puppeteer/api-docs-entry.js';
|
||||
|
||||
describe('network', function () {
|
||||
setupTestBrowserHooks();
|
||||
@ -366,6 +367,43 @@ describe('network', function () {
|
||||
const responseBuffer = await response.buffer();
|
||||
expect(responseBuffer.equals(imageBuffer)).toBe(true);
|
||||
});
|
||||
it('should throw if the response does not have a body', async () => {
|
||||
const { page, server } = getTestState();
|
||||
|
||||
await page.goto(server.PREFIX + '/empty.html');
|
||||
|
||||
server.setRoute('/test.html', (req, res) => {
|
||||
res.setHeader('Access-Control-Allow-Origin', '*');
|
||||
res.setHeader('Access-Control-Allow-Headers', 'x-ping');
|
||||
res.end('Hello World');
|
||||
});
|
||||
const url = server.CROSS_PROCESS_PREFIX + '/test.html';
|
||||
const responsePromise = new Promise<HTTPResponse>((resolve) => {
|
||||
page.on('response', (response) => {
|
||||
// Get the preflight response.
|
||||
if (
|
||||
response.request().method() === 'OPTIONS' &&
|
||||
response.url() === url
|
||||
) {
|
||||
resolve(response);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Trigger a request with a preflight.
|
||||
await page.evaluate<(src: string) => void>(async (src) => {
|
||||
const response = await fetch(src, {
|
||||
method: 'POST',
|
||||
headers: { 'x-ping': 'pong' },
|
||||
});
|
||||
return response;
|
||||
}, url);
|
||||
|
||||
const response = await responsePromise;
|
||||
await expect(response.buffer()).rejects.toThrowError(
|
||||
'Could not load body for this request. This might happen if the request is a preflight request.'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Response.statusText', function () {
|
||||
|
Loading…
Reference in New Issue
Block a user