fix: postDataJSON with application/x-www-form-urlencoded; charset=UTF-8 (#29889)

Fixes https://github.com/microsoft/playwright/issues/29872
This commit is contained in:
Max Schmitt 2024-03-12 17:20:39 +01:00 committed by GitHub
parent be325507cb
commit 38fc74db7c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 1 deletions

View File

@ -141,7 +141,7 @@ export class Request extends ChannelOwner<channels.RequestChannel> implements ap
return null;
const contentType = this.headers()['content-type'];
if (contentType === 'application/x-www-form-urlencoded') {
if (contentType.includes('application/x-www-form-urlencoded')) {
const entries: Record<string, string> = {};
const parsed = new URLSearchParams(postData);
for (const [k, v] of parsed.entries())

View File

@ -290,6 +290,20 @@ it('should parse the data if content-type is application/x-www-form-urlencoded',
expect(request.postDataJSON()).toEqual({ 'foo': 'bar', 'baz': '123' });
});
it('should parse the data if content-type is application/x-www-form-urlencoded; charset=UTF-8', async ({ page, server }) => {
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/29872' });
await page.goto(server.EMPTY_PAGE);
const requestPromise = page.waitForRequest('**/post');
await page.evaluate(() => fetch('./post', {
method: 'POST',
headers: {
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
},
body: 'foo=bar&baz=123'
}));
expect((await requestPromise).postDataJSON()).toEqual({ 'foo': 'bar', 'baz': '123' });
});
it('should get |undefined| with postDataJSON() when there is no post data', async ({ page, server }) => {
const response = await page.goto(server.EMPTY_PAGE);
expect(response.request().postDataJSON()).toBe(null);