feat: Request.postDataJSON (#2368)
This commit is contained in:
Родитель
e168fddac8
Коммит
9dfe9348ac
|
@ -3499,6 +3499,7 @@ If request gets a 'redirect' response, the request is successfully finished with
|
|||
- [request.isNavigationRequest()](#requestisnavigationrequest)
|
||||
- [request.method()](#requestmethod)
|
||||
- [request.postData()](#requestpostdata)
|
||||
- [request.postDataJSON()](#requestpostdatajson)
|
||||
- [request.redirectedFrom()](#requestredirectedfrom)
|
||||
- [request.redirectedTo()](#requestredirectedto)
|
||||
- [request.resourceType()](#requestresourcetype)
|
||||
|
@ -3538,6 +3539,11 @@ Whether this request is driving frame's navigation.
|
|||
#### request.postData()
|
||||
- returns: <?[string]> Request's post body, if any.
|
||||
|
||||
#### request.postDataJSON()
|
||||
- returns: <?[Object]> Parsed request's body for `form-urlencoded` and JSON as a fallback if any.
|
||||
|
||||
When the response is `application/x-www-form-urlencoded` then a key/value object of the values will be returned. Otherwise it will be parsed as JSON.
|
||||
|
||||
#### request.redirectedFrom()
|
||||
- returns: <?[Request]> Request that was redirected by the server to this one, if any.
|
||||
|
||||
|
|
|
@ -20,6 +20,7 @@ import * as util from 'util';
|
|||
import * as frames from './frames';
|
||||
import { assert, helper } from './helper';
|
||||
import { Page } from './page';
|
||||
import { URLSearchParams } from 'url';
|
||||
|
||||
export type NetworkCookie = {
|
||||
name: string,
|
||||
|
@ -153,6 +154,25 @@ export class Request {
|
|||
return this._postData;
|
||||
}
|
||||
|
||||
postDataJSON(): Object | null {
|
||||
if (!this._postData)
|
||||
return null;
|
||||
|
||||
const contentType = this.headers()['content-type'];
|
||||
if (!contentType)
|
||||
return null;
|
||||
|
||||
if (contentType === 'application/x-www-form-urlencoded') {
|
||||
const entries: Record<string, string> = {};
|
||||
const parsed = new URLSearchParams(this._postData);
|
||||
for (const [k, v] of parsed.entries())
|
||||
entries[k] = v;
|
||||
return entries;
|
||||
}
|
||||
|
||||
return JSON.parse(this._postData);
|
||||
}
|
||||
|
||||
headers(): {[key: string]: string} {
|
||||
return { ...this._headers };
|
||||
}
|
||||
|
|
|
@ -140,6 +140,34 @@ describe('Request.postData', function() {
|
|||
});
|
||||
});
|
||||
|
||||
describe('Request.postDataJSON', function () {
|
||||
it('should parse the JSON payload', async ({ page, server }) => {
|
||||
await page.goto(server.EMPTY_PAGE);
|
||||
server.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.postDataJSON()).toEqual({ "foo": "bar" });
|
||||
});
|
||||
|
||||
it('should parse the data if content-type is application/x-www-form-urlencoded', async({page, server}) => {
|
||||
await page.goto(server.EMPTY_PAGE);
|
||||
server.setRoute('/post', (req, res) => res.end());
|
||||
let request = null;
|
||||
page.on('request', r => request = r);
|
||||
await page.setContent(`<form method='POST' action='/post'><input type='text' name='foo' value='bar'><input type='number' name='baz' value='123'><input type='submit'></form>`);
|
||||
await page.click('input[type=submit]');
|
||||
expect(request).toBeTruthy();
|
||||
expect(request.postDataJSON()).toEqual({'foo':'bar','baz':'123'});
|
||||
})
|
||||
|
||||
it('should be |undefined| when there is no post data', async ({ page, server }) => {
|
||||
const response = await page.goto(server.EMPTY_PAGE);
|
||||
expect(response.request().postDataJSON()).toBe(null);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Response.text', function() {
|
||||
it('should work', async({page, server}) => {
|
||||
const response = await page.goto(server.PREFIX + '/simple.json');
|
||||
|
|
Загрузка…
Ссылка в новой задаче