From 53a62a3af8c822806e91fe1aa2307b5779e3045b Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Sat, 6 Mar 2021 07:15:02 -0800 Subject: [PATCH] test: add post data test with PUT request (#5745) --- test/network-post-data.spec.ts | 87 ++++++++++++++++++++++++++++++ test/page-wait-for-request.spec.ts | 55 ------------------- 2 files changed, 87 insertions(+), 55 deletions(-) create mode 100644 test/network-post-data.spec.ts diff --git a/test/network-post-data.spec.ts b/test/network-post-data.spec.ts new file mode 100644 index 0000000000..16f6b90a15 --- /dev/null +++ b/test/network-post-data.spec.ts @@ -0,0 +1,87 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { it, expect } from './fixtures'; + +it('should return correct postData buffer for utf-8 body', async ({page, server}) => { + await page.goto(server.EMPTY_PAGE); + const value = 'baẞ'; + const [request] = await Promise.all([ + page.waitForRequest('**'), + page.evaluate(({url, value}) => { + const request = new Request(url, { + method: 'POST', + body: JSON.stringify(value), + }); + request.headers.set('content-type', 'application/json;charset=UTF-8'); + return fetch(request); + }, {url: server.PREFIX + '/title.html', value}) + ]); + 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: '', + }); + 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: '); +}); + +it('should return post data for PUT requests', 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: 'PUT', + body: JSON.stringify({ value: 42 }), + }); + return fetch(request); + }, {url: server.PREFIX + '/title.html'}) + ]); + expect(request.postDataJSON()).toEqual({ value: 42 }); +}); diff --git a/test/page-wait-for-request.spec.ts b/test/page-wait-for-request.spec.ts index de7a93bb44..506a25fac8 100644 --- a/test/page-wait-for-request.spec.ts +++ b/test/page-wait-for-request.spec.ts @@ -100,58 +100,3 @@ it('should work with url match regular expression from a different context', asy ]); expect(request.url()).toBe(server.PREFIX + '/digits/1.png'); }); - -it('should return correct postData buffer for utf-8 body', async ({page, server}) => { - await page.goto(server.EMPTY_PAGE); - const value = 'baẞ'; - const [request] = await Promise.all([ - page.waitForRequest('**'), - page.evaluate(({url, value}) => { - const request = new Request(url, { - method: 'POST', - body: JSON.stringify(value), - }); - request.headers.set('content-type', 'application/json;charset=UTF-8'); - return fetch(request); - }, {url: server.PREFIX + '/title.html', value}) - ]); - 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: '', - }); - 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: '); -});