diff --git a/api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java b/api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java index 0d62c204..0471b985 100644 --- a/api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java +++ b/api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java @@ -236,8 +236,8 @@ class Method extends Element { private static Map customSignature = new HashMap<>(); static { customSignature.put("Page.setViewportSize", new String[]{"void setViewportSize(int width, int height);"}); - customSignature.put("BrowserContext.setHTTPCredentials", new String[]{ - "void setHTTPCredentials(String username, String password);"}); + // The method is deprecated in ts, just remove it in Java. + customSignature.put("BrowserContext.setHTTPCredentials", new String[0]); customSignature.put("BrowserContext.route", new String[]{ "void route(String url, BiConsumer handler);", "void route(Pattern url, BiConsumer handler);", diff --git a/api-generator/src/main/java/com/microsoft/playwright/tools/Types.java b/api-generator/src/main/java/com/microsoft/playwright/tools/Types.java index e5e6f338..e1cc2b25 100644 --- a/api-generator/src/main/java/com/microsoft/playwright/tools/Types.java +++ b/api-generator/src/main/java/com/microsoft/playwright/tools/Types.java @@ -122,7 +122,7 @@ class Types { add("Browser.newContext.options.httpCredentials", "Object", "BrowserContext.HTTPCredentials", new Empty()); add("Browser.newPage.options.httpCredentials", "Object", "BrowserContext.HTTPCredentials", new Empty()); add("BrowserType.launchPersistentContext.options.httpCredentials", "Object", "BrowserContext.HTTPCredentials", new Empty()); - add("BrowserContext.setHTTPCredentials.httpCredentials", "null|Object", "HTTPCredentials", new Empty()); + add("BrowserContext.setHTTPCredentials.httpCredentials", "null|Object", "do nothing", new Empty()); // EvaluationArgument add("Page.$eval.arg", "EvaluationArgument", "Object"); diff --git a/playwright/src/main/java/com/microsoft/playwright/BrowserContext.java b/playwright/src/main/java/com/microsoft/playwright/BrowserContext.java index f8776569..10b22983 100644 --- a/playwright/src/main/java/com/microsoft/playwright/BrowserContext.java +++ b/playwright/src/main/java/com/microsoft/playwright/BrowserContext.java @@ -208,7 +208,6 @@ public interface BrowserContext { void setDefaultTimeout(int timeout); void setExtraHTTPHeaders(Map headers); void setGeolocation(Geolocation geolocation); - void setHTTPCredentials(String username, String password); void setOffline(boolean offline); default void unroute(String url) { unroute(url, null); } default void unroute(Pattern url) { unroute(url, null); } diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/BrowserContextImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/BrowserContextImpl.java index 61fc4cce..f2dfab7b 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/BrowserContextImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/BrowserContextImpl.java @@ -242,11 +242,6 @@ class BrowserContextImpl extends ChannelOwner implements BrowserContext { } - @Override - public void setHTTPCredentials(String username, String password) { - - } - @Override public void setOffline(boolean offline) { JsonObject params = new JsonObject(); diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/ResponseImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/ResponseImpl.java index 32dd886b..5d3f04d1 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/ResponseImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/ResponseImpl.java @@ -16,13 +16,17 @@ package com.microsoft.playwright.impl; +import com.google.gson.Gson; import com.google.gson.JsonObject; import com.microsoft.playwright.Frame; import com.microsoft.playwright.Request; import com.microsoft.playwright.Response; +import java.util.Base64; import java.util.Map; +import static com.microsoft.playwright.impl.Serialization.deserialize; + public class ResponseImpl extends ChannelOwner implements Response { ResponseImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) { super(parent, type, guid, initializer); @@ -30,7 +34,8 @@ public class ResponseImpl extends ChannelOwner implements Response { @Override public byte[] body() { - return new byte[0]; + JsonObject json = sendMessage("body").getAsJsonObject(); + return Base64.getDecoder().decode(json.get("binary").getAsString()); } @Override diff --git a/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextCredentials.java b/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextCredentials.java new file mode 100644 index 00000000..be3e7dd9 --- /dev/null +++ b/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextCredentials.java @@ -0,0 +1,73 @@ +/* + * 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 org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class TestBrowserContextCredentials extends TestBase { + @Test + void shouldFailWithoutCredentials() { +// TODO: test.fail(browserName === "chromium" && headful); + server.setAuth("/empty.html", "user", "pass"); + BrowserContext context = browser.newContext(); + Page page = context.newPage(); + Response response = page.navigate(server.EMPTY_PAGE); + assertEquals(401, response.status()); + context.close(); + } + + void shouldWorkWithSetHTTPCredentials() { + // The method is not exposed in Java. + } + + @Test + void shouldWorkWithCorrectCredentials() { + server.setAuth("/empty.html", "user", "pass"); + BrowserContext context = browser.newContext(new Browser.NewContextOptions() + .withHttpCredentials("user", "pass")); + Page page = context.newPage(); + Response response = page.navigate(server.EMPTY_PAGE); + assertEquals(200, response.status()); + context.close(); + } + + @Test + void shouldFailWithWrongCredentials() { + server.setAuth("/empty.html", "user", "pass"); + BrowserContext context = browser.newContext(new Browser.NewContextOptions().withHttpCredentials("foo", "bar")); + Page page = context.newPage(); + Response response = page.navigate(server.EMPTY_PAGE); + assertEquals(401, response.status()); + context.close(); + } + + @Test + void shouldReturnResourceBody() { + server.setAuth("/playground.html", "user", "pass"); + BrowserContext context = browser.newContext(new Browser.NewContextOptions() + .withHttpCredentials("user", "pass")); + Page page = context.newPage(); + Response response = page.navigate(server.PREFIX + "/playground.html"); + assertEquals(200, response.status()); + assertEquals("Playground", page.title()); + assertTrue(new String(response.body()).contains("Playground")); + context.close(); + } +}