From 4bcb7afeda3d09dabdbef19aae962b503e0019b4 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Mon, 8 Mar 2021 12:29:19 -0800 Subject: [PATCH] tests: port new postData tests from upstream (#335) --- .../playwright/TestNetworkPostData.java | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 playwright/src/test/java/com/microsoft/playwright/TestNetworkPostData.java diff --git a/playwright/src/test/java/com/microsoft/playwright/TestNetworkPostData.java b/playwright/src/test/java/com/microsoft/playwright/TestNetworkPostData.java new file mode 100644 index 00000000..6c7c88cf --- /dev/null +++ b/playwright/src/test/java/com/microsoft/playwright/TestNetworkPostData.java @@ -0,0 +1,82 @@ +/* + * Copyright (c) Microsoft Corporation. + * + * 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. + */ + +package com.microsoft.playwright; + +import com.google.gson.Gson; +import org.junit.jupiter.api.Test; + +import java.nio.charset.StandardCharsets; +import java.util.Arrays; + +import static com.microsoft.playwright.Utils.mapOf; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class TestNetworkPostData extends TestBase { + @Test + void shouldReturnCorrectPostDataBufferForUtf8Body() { + page.navigate(server.EMPTY_PAGE); + String value = "baẞ"; + Request request = page.waitForRequest("**", () -> { + page.evaluate("({url, value}) => {\n" + + " const request = new Request(url, {\n" + + " method: 'POST',\n" + + " body: JSON.stringify(value),\n" + + " });\n" + + " request.headers.set('content-type', 'application/json;charset=UTF-8');\n" + + " return fetch(request);\n" + + "}", mapOf("url", server.PREFIX + "/title.html", "value", value)); + }); + assertTrue(Arrays.equals(new Gson().toJson(value).getBytes(StandardCharsets.UTF_8), request.postDataBuffer())); + assertEquals(new Gson().toJson(value), request.postData()); + } + + @Test + void shouldReturnPostDataWOContentType() { + page.navigate(server.EMPTY_PAGE); + Request request = page.waitForRequest("**", () -> { + page.evaluate("({url}) => {\n" + + " const request = new Request(url, {\n" + + " method: 'POST',\n" + + " body: JSON.stringify({ value: 42 }),\n" + + " });\n" + + " request.headers.set('content-type', '');\n" + + " return fetch(request);\n" + + "}", mapOf("url", server.PREFIX + "/title.html")); + }); + assertEquals(new Gson().toJson(mapOf("value", 42)), request.postData()); + } + + void shouldThrowOnInvalidJSONInPostData() { + // Thre is no postDataJSON() in java + } + + @Test + void shouldReturnPostDataForPUTRequests() { + page.navigate(server.EMPTY_PAGE); + Request request = page.waitForRequest("**", () -> { + page.evaluate("({url}) => {\n" + + " const request = new Request(url, {\n" + + " method: 'PUT',\n" + + " body: JSON.stringify({ value: 42 }),\n" + + " });\n" + + " return fetch(request);\n" + + "}", mapOf("url", server.PREFIX + "/title.html")); + }); + assertEquals(new Gson().toJson(mapOf("value", 42)), request.postData()); + } +}