Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
fix(postData): do not require content type when retrieving post data (#5736)
Pavel Feldman9e205662443d
diff --git a/src/client/network.ts b/src/client/network.tsindex 44c96dc726..35b61a1e80 100644--- a/src/client/network.ts+++ b/src/client/network.ts@@ -113,9 +113,6 @@ export class Request extends ChannelOwner<channels.RequestChannel, channels.Requreturn 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(postData);@@ -124,7 +121,11 @@ export class Request extends ChannelOwner<channels.RequestChannel, channels.Requreturn entries;}- return JSON.parse(postData);+ try {+ return JSON.parse(postData);+ } catch (e) {+ throw new Error('POST data is not a valid JSON object: ' + postData);+ }}headers(): Headers {diff --git a/test/page-wait-for-request.spec.ts b/test/page-wait-for-request.spec.tsindex 0b89bf1317..de7a93bb44 100644--- a/test/page-wait-for-request.spec.ts+++ b/test/page-wait-for-request.spec.ts@@ -118,3 +118,40 @@ it('should return correct postData buffer for utf-8 body', async ({page, server}expect(request.postDataBuffer().equals(Buffer.from(JSON.stringify(value), 'utf-8'))).toBe(true);expect(request.postDataJSON()).toBe(value);});++it('should return post data w/o content-type', async ({page, server}) => {+ await page.goto(server.EMPTY_PAGE);+ const [request] = await Promise.all([+ page.waitForRequest('**'),+ page.evaluate(({url}) => {+ const request = new Request(url, {+ method: 'POST',+ body: JSON.stringify({ value: 42 }),+ });+ request.headers.set('content-type', '');+ return fetch(request);+ }, {url: server.PREFIX + '/title.html'})+ ]);+ expect(request.postDataJSON()).toEqual({ value: 42 });+});++it('should throw on invalid JSON in post data', async ({page, server}) => {+ await page.goto(server.EMPTY_PAGE);+ const [request] = await Promise.all([+ page.waitForRequest('**'),+ page.evaluate(({url}) => {+ const request = new Request(url, {+ method: 'POST',+ body: '<not a json>',+ });+ return fetch(request);+ }, {url: server.PREFIX + '/title.html'})+ ]);+ let error;+ try {+ request.postDataJSON();+ } catch (e) {+ error = e;+ }+ expect(error.message).toContain('POST data is not a valid JSON object: <not a json>');+});