mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
Implement Body interface for both Request and Response
This patch partially implements Fetch API's [Body](https://developer.mozilla.org/en-US/docs/Web/API/Body) interface for both Request and Response. Fixes #26.
This commit is contained in:
28
test/test.js
28
test/test.js
@@ -37,8 +37,8 @@ describe('Puppeteer', function() {
|
||||
});
|
||||
|
||||
afterAll(function() {
|
||||
browser.close();
|
||||
staticServer.stop();
|
||||
browser.close();
|
||||
});
|
||||
|
||||
beforeEach(SX(async function() {
|
||||
@@ -526,6 +526,32 @@ describe('Puppeteer', function() {
|
||||
expect(result).toBe('<div>hello</div>');
|
||||
}));
|
||||
});
|
||||
describe('Request implements Body', function() {
|
||||
it('should work', SX(async function() {
|
||||
await page.navigate(EMPTY_PAGE);
|
||||
staticServer.setRoute('/post', (req, res) => res.end());
|
||||
let request = null;
|
||||
page.on('request', r => request = r);
|
||||
await page.evaluate(() => fetch('./post', { method: 'POST', body: JSON.stringify({foo: 'bar'})}));
|
||||
expect(request).toBeTruthy();
|
||||
expect(request.bodyUsed).toBe(false);
|
||||
expect(await request.text()).toBe('{"foo":"bar"}');
|
||||
expect(await request.json()).toEqual({foo: 'bar'});
|
||||
expect(request.bodyUsed).toBe(true);
|
||||
}));
|
||||
});
|
||||
describe('Response implements Body', function() {
|
||||
it('should work', SX(async function() {
|
||||
let response = null;
|
||||
page.on('response', r => response = r);
|
||||
await page.navigate(STATIC_PREFIX + '/simple.json');
|
||||
expect(response).toBeTruthy();
|
||||
expect(response.bodyUsed).toBe(false);
|
||||
expect(await response.text()).toBe('{"foo": "bar"}\n');
|
||||
expect(await response.json()).toEqual({foo: 'bar'});
|
||||
expect(response.bodyUsed).toBe(true);
|
||||
}));
|
||||
});
|
||||
describe('Network Events', function() {
|
||||
it('Page.Events.Request', SX(async function() {
|
||||
let requests = [];
|
||||
|
||||
Reference in New Issue
Block a user