mirror of
https://github.com/microsoft/playwright-java.git
synced 2025-09-08 21:01:00 +00:00
feat: http credentials (#45)
This commit is contained in:
parent
df4bf71321
commit
24b101d071
@ -236,8 +236,8 @@ class Method extends Element {
|
|||||||
private static Map<String, String[]> customSignature = new HashMap<>();
|
private static Map<String, String[]> customSignature = new HashMap<>();
|
||||||
static {
|
static {
|
||||||
customSignature.put("Page.setViewportSize", new String[]{"void setViewportSize(int width, int height);"});
|
customSignature.put("Page.setViewportSize", new String[]{"void setViewportSize(int width, int height);"});
|
||||||
customSignature.put("BrowserContext.setHTTPCredentials", new String[]{
|
// The method is deprecated in ts, just remove it in Java.
|
||||||
"void setHTTPCredentials(String username, String password);"});
|
customSignature.put("BrowserContext.setHTTPCredentials", new String[0]);
|
||||||
customSignature.put("BrowserContext.route", new String[]{
|
customSignature.put("BrowserContext.route", new String[]{
|
||||||
"void route(String url, BiConsumer<Route, Request> handler);",
|
"void route(String url, BiConsumer<Route, Request> handler);",
|
||||||
"void route(Pattern url, BiConsumer<Route, Request> handler);",
|
"void route(Pattern url, BiConsumer<Route, Request> handler);",
|
||||||
|
@ -122,7 +122,7 @@ class Types {
|
|||||||
add("Browser.newContext.options.httpCredentials", "Object", "BrowserContext.HTTPCredentials", new Empty());
|
add("Browser.newContext.options.httpCredentials", "Object", "BrowserContext.HTTPCredentials", new Empty());
|
||||||
add("Browser.newPage.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("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
|
// EvaluationArgument
|
||||||
add("Page.$eval.arg", "EvaluationArgument", "Object");
|
add("Page.$eval.arg", "EvaluationArgument", "Object");
|
||||||
|
@ -208,7 +208,6 @@ public interface BrowserContext {
|
|||||||
void setDefaultTimeout(int timeout);
|
void setDefaultTimeout(int timeout);
|
||||||
void setExtraHTTPHeaders(Map<String, String> headers);
|
void setExtraHTTPHeaders(Map<String, String> headers);
|
||||||
void setGeolocation(Geolocation geolocation);
|
void setGeolocation(Geolocation geolocation);
|
||||||
void setHTTPCredentials(String username, String password);
|
|
||||||
void setOffline(boolean offline);
|
void setOffline(boolean offline);
|
||||||
default void unroute(String url) { unroute(url, null); }
|
default void unroute(String url) { unroute(url, null); }
|
||||||
default void unroute(Pattern url) { unroute(url, null); }
|
default void unroute(Pattern url) { unroute(url, null); }
|
||||||
|
@ -242,11 +242,6 @@ class BrowserContextImpl extends ChannelOwner implements BrowserContext {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setHTTPCredentials(String username, String password) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setOffline(boolean offline) {
|
public void setOffline(boolean offline) {
|
||||||
JsonObject params = new JsonObject();
|
JsonObject params = new JsonObject();
|
||||||
|
@ -16,13 +16,17 @@
|
|||||||
|
|
||||||
package com.microsoft.playwright.impl;
|
package com.microsoft.playwright.impl;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.microsoft.playwright.Frame;
|
import com.microsoft.playwright.Frame;
|
||||||
import com.microsoft.playwright.Request;
|
import com.microsoft.playwright.Request;
|
||||||
import com.microsoft.playwright.Response;
|
import com.microsoft.playwright.Response;
|
||||||
|
|
||||||
|
import java.util.Base64;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static com.microsoft.playwright.impl.Serialization.deserialize;
|
||||||
|
|
||||||
public class ResponseImpl extends ChannelOwner implements Response {
|
public class ResponseImpl extends ChannelOwner implements Response {
|
||||||
ResponseImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
|
ResponseImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
|
||||||
super(parent, type, guid, initializer);
|
super(parent, type, guid, initializer);
|
||||||
@ -30,7 +34,8 @@ public class ResponseImpl extends ChannelOwner implements Response {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public byte[] body() {
|
public byte[] body() {
|
||||||
return new byte[0];
|
JsonObject json = sendMessage("body").getAsJsonObject();
|
||||||
|
return Base64.getDecoder().decode(json.get("binary").getAsString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user