use of com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts in project htmlunit by HtmlUnit.
the class DefaultCredentialsProvider2Test method basicAuthenticationUserFromUrlUsedForNextSteps.
/**
* @throws Exception if an error occurs
*/
@Test
@Alerts("SecRet")
public void basicAuthenticationUserFromUrlUsedForNextSteps() throws Exception {
final String html = "<html><body onload='alert(\"SecRet\")'></body></html>";
getMockWebConnection().setDefaultResponse(html);
getWebClient().getCredentialsProvider().clear();
try {
// no credentials
loadPage(html, URL_FIRST);
fail("Should not be authorized");
} catch (final FailingHttpStatusCodeException e) {
// success
}
final boolean urlWithCredentials = !getBrowserVersion().isIE();
try {
// now a url with credentials
final URL url = new URL("http://jetty:jetty@localhost:" + PORT + "/");
loadPageWithAlerts(url);
if (!urlWithCredentials) {
fail("Should not be authorized");
}
} catch (final FailingHttpStatusCodeException e) {
if (urlWithCredentials) {
throw e;
}
}
try {
// next step without credentials but the credentials are still known
loadPageWithAlerts(URL_FIRST);
if (!urlWithCredentials) {
fail("Should not be authorized");
}
} catch (final FailingHttpStatusCodeException e) {
if (urlWithCredentials) {
throw e;
}
}
try {
// different path
final URL url = new URL(URL_FIRST, "somewhere");
loadPageWithAlerts(url);
if (!urlWithCredentials) {
fail("Should not be authorized");
}
} catch (final FailingHttpStatusCodeException e) {
if (urlWithCredentials) {
throw e;
}
}
}
use of com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts in project htmlunit by HtmlUnit.
the class CookieManager4Test method storedDomain4.
/**
* @throws Exception if the test fails
*/
@Test
@Alerts(DEFAULT = "c11=11; c12=12", CHROME = "c12=12", EDGE = "c12=12")
public void storedDomain4() throws Exception {
final List<NameValuePair> responseHeader = new ArrayList<>();
responseHeader.add(new NameValuePair("Set-Cookie", "c1=1; Domain=." + DOMAIN + "; Path=/"));
responseHeader.add(new NameValuePair("Set-Cookie", "c2=2; Domain=" + DOMAIN + "; Path=/"));
responseHeader.add(new NameValuePair("Set-Cookie", "c3=3; Domain=.host1." + DOMAIN + "; Path=/"));
responseHeader.add(new NameValuePair("Set-Cookie", "c4=4; Domain=host1." + DOMAIN + "; Path=/"));
responseHeader.add(new NameValuePair("Set-Cookie", "c5=5; Domain=." + DOMAIN + ":" + PORT + "; Path=/"));
responseHeader.add(new NameValuePair("Set-Cookie", "c6=6; Domain=" + DOMAIN + ":" + PORT + "; Path=/"));
responseHeader.add(new NameValuePair("Set-Cookie", "c7=7; Domain=.host1." + DOMAIN + ":" + PORT + "; Path=/"));
responseHeader.add(new NameValuePair("Set-Cookie", "c8=8; Domain=host1." + DOMAIN + ":" + PORT + "; Path=/"));
responseHeader.add(new NameValuePair("Set-Cookie", "c9=9; Domain=.org; Path=/"));
responseHeader.add(new NameValuePair("Set-Cookie", "c10=10; Domain=org; Path=/"));
responseHeader.add(new NameValuePair("Set-Cookie", "c11=11; Domain=.htmlunit; Path=/"));
responseHeader.add(new NameValuePair("Set-Cookie", "c12=12; Domain=htmlunit; Path=/"));
getMockWebConnection().setDefaultResponse(CookieManagerTest.HTML_ALERT_COOKIE, 200, "Ok", MimeType.TEXT_HTML, responseHeader);
final WebDriver driver = loadPageWithAlerts2(new URL(URL_HOST4));
assertEquals("c12=12; path=/; domain=htmlunit", driver.manage().getCookieNamed("c12").toString());
if (driver.manage().getCookieNamed("c11") != null) {
assertEquals("c11=11; path=/; domain=htmlunit", driver.manage().getCookieNamed("c11").toString());
}
}
use of com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts in project htmlunit by HtmlUnit.
the class CookieManager4Test method sameSiteIncludeFromSameDomain.
/**
* @throws Exception if the test fails
*/
@Test
@Alerts("c2=Lax; c3=Strict; c4=empty; c5=unknown; first=1")
public void sameSiteIncludeFromSameDomain() throws Exception {
final List<NameValuePair> responseHeader = new ArrayList<>();
responseHeader.add(new NameValuePair("Set-Cookie", "first=1;"));
responseHeader.add(new NameValuePair("Set-Cookie", "c1=None; SameSite=None; Secure"));
responseHeader.add(new NameValuePair("Set-Cookie", "c2=Lax; SameSite=Lax"));
responseHeader.add(new NameValuePair("Set-Cookie", "c3=Strict; SameSite=Strict"));
responseHeader.add(new NameValuePair("Set-Cookie", "c4=empty; SameSite="));
responseHeader.add(new NameValuePair("Set-Cookie", "c5=unknown; SameSite=unknown"));
final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head>\n" + " <link rel='stylesheet' href='" + URL_HOST1 + "css/style.css'>\n" + "</head>\n" + "<body>\n" + "</body></html>";
getMockWebConnection().setDefaultResponse("");
getMockWebConnection().setResponse(new URL(URL_HOST1), HTML_ALERT_COOKIE, 200, "OK", MimeType.TEXT_HTML, responseHeader);
getMockWebConnection().setResponse(new URL(URL_HOST1 + "include"), html, 200, "OK", MimeType.TEXT_HTML, responseHeader);
final WebDriver driver = loadPageWithAlerts2(new URL(URL_HOST1));
driver.get(URL_HOST1 + "include");
assertEquals(URL_HOST1 + "css/style.css", getMockWebConnection().getLastWebRequest().getUrl());
final Map<String, String> lastHeaders = getMockWebConnection().getLastAdditionalHeaders();
// strange check, but there is no order
final String lastCookies = lastHeaders.get(HttpHeader.COOKIE);
assertEquals(48, lastCookies.length());
assertTrue("lastCookies: " + lastCookies, lastCookies.contains("first=1") && lastCookies.contains("c2=Lax") && lastCookies.contains("c3=Strict") && lastCookies.contains("c4=empty") && lastCookies.contains("c5=unknown"));
if (driver instanceof HtmlUnitDriver) {
final CookieManager mgr = getWebWindowOf((HtmlUnitDriver) driver).getWebClient().getCookieManager();
assertEquals(6, mgr.getCookies().size());
assertNull(mgr.getCookie("first").getSameSite());
assertEquals("lax", mgr.getCookie("c2").getSameSite());
assertEquals("strict", mgr.getCookie("c3").getSameSite());
assertEquals("", mgr.getCookie("c4").getSameSite());
assertEquals("unknown", mgr.getCookie("c5").getSameSite());
}
}
use of com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts in project htmlunit by HtmlUnit.
the class CookieManager4Test method sameSiteIFrameFromSubDomain.
/**
* @throws Exception if the test fails
*/
@Test
@Alerts("c2=Lax; c3=Strict; c4=empty; c5=unknown; first=1")
public void sameSiteIFrameFromSubDomain() throws Exception {
final List<NameValuePair> responseHeader = new ArrayList<>();
responseHeader.add(new NameValuePair("Set-Cookie", "first=1;"));
responseHeader.add(new NameValuePair("Set-Cookie", "c1=None; SameSite=None; Secure"));
responseHeader.add(new NameValuePair("Set-Cookie", "c2=Lax; SameSite=Lax"));
responseHeader.add(new NameValuePair("Set-Cookie", "c3=Strict; SameSite=Strict"));
responseHeader.add(new NameValuePair("Set-Cookie", "c4=empty; SameSite="));
responseHeader.add(new NameValuePair("Set-Cookie", "c5=unknown; SameSite=unknown"));
final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head>\n" + "</head>\n" + "<body>\n" + "<iframe src='" + URL_HOST1 + "iframe.html'></iframe>\n" + "</body></html>";
getMockWebConnection().setDefaultResponse("");
getMockWebConnection().setResponse(new URL(URL_HOST1), HTML_ALERT_COOKIE, 200, "OK", MimeType.TEXT_HTML, responseHeader);
getMockWebConnection().setResponse(new URL(URL_HOST2 + "include"), html, 200, "OK", MimeType.TEXT_HTML, responseHeader);
final WebDriver driver = loadPageWithAlerts2(new URL(URL_HOST1));
driver.get(URL_HOST2 + "include");
assertEquals(URL_HOST1 + "iframe.html", getMockWebConnection().getLastWebRequest().getUrl());
final Map<String, String> lastHeaders = getMockWebConnection().getLastAdditionalHeaders();
// strange check, but there is no order
final String lastCookies = lastHeaders.get(HttpHeader.COOKIE);
assertEquals(48, lastCookies.length());
assertTrue("lastCookies: " + lastCookies, lastCookies.contains("first=1") && lastCookies.contains("c2=Lax") && lastCookies.contains("c3=Strict") && lastCookies.contains("c4=empty") && lastCookies.contains("c5=unknown"));
if (driver instanceof HtmlUnitDriver) {
final CookieManager mgr = getWebWindowOf((HtmlUnitDriver) driver).getWebClient().getCookieManager();
assertEquals(12, mgr.getCookies().size());
assertNull(mgr.getCookie("first").getSameSite());
assertEquals("lax", mgr.getCookie("c2").getSameSite());
assertEquals("strict", mgr.getCookie("c3").getSameSite());
assertEquals("", mgr.getCookie("c4").getSameSite());
assertEquals("unknown", mgr.getCookie("c5").getSameSite());
}
}
use of com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts in project htmlunit by HtmlUnit.
the class CookieManager4Test method sameSite.
/**
* @throws Exception if the test fails
*/
@Test
@Alerts("c2=Lax; c3=Strict; c4=empty; c5=unknown; c6=strict; c7=stRiCT; first=1")
public void sameSite() throws Exception {
final List<NameValuePair> responseHeader = new ArrayList<>();
responseHeader.add(new NameValuePair("Set-Cookie", "first=1;"));
responseHeader.add(new NameValuePair("Set-Cookie", "c1=None; SameSite=None; Secure"));
responseHeader.add(new NameValuePair("Set-Cookie", "c2=Lax; SameSite=Lax"));
responseHeader.add(new NameValuePair("Set-Cookie", "c3=Strict; SameSite=Strict"));
responseHeader.add(new NameValuePair("Set-Cookie", "c4=empty; SameSite="));
responseHeader.add(new NameValuePair("Set-Cookie", "c5=unknown; SameSite=unknown"));
responseHeader.add(new NameValuePair("Set-Cookie", "c6=strict; SameSite=strict"));
responseHeader.add(new NameValuePair("Set-Cookie", "c7=stRiCT; SameSite=stRiCT"));
getMockWebConnection().setDefaultResponse("");
getMockWebConnection().setResponse(new URL(URL_HOST1), HTML_ALERT_COOKIE, 200, "OK", MimeType.TEXT_HTML, responseHeader);
final WebDriver driver = loadPageWithAlerts2(new URL(URL_HOST1));
driver.get(URL_HOST1 + "foo");
final Map<String, String> lastHeaders = getMockWebConnection().getLastAdditionalHeaders();
// strange check, but there is no order
final String lastCookies = lastHeaders.get(HttpHeader.COOKIE);
assertEquals(70, lastCookies.length());
assertTrue("lastCookies: " + lastCookies, lastCookies.contains("first=1") && lastCookies.contains("c2=Lax") && lastCookies.contains("c3=Strict") && lastCookies.contains("c4=empty") && lastCookies.contains("c5=unknown") && lastCookies.contains("c6=strict") && lastCookies.contains("c7=stRiCT"));
if (driver instanceof HtmlUnitDriver) {
final CookieManager mgr = getWebWindowOf((HtmlUnitDriver) driver).getWebClient().getCookieManager();
assertEquals(8, mgr.getCookies().size());
assertNull(mgr.getCookie("first").getSameSite());
assertEquals("lax", mgr.getCookie("c2").getSameSite());
assertEquals("strict", mgr.getCookie("c3").getSameSite());
assertEquals("", mgr.getCookie("c4").getSameSite());
assertEquals("unknown", mgr.getCookie("c5").getSameSite());
assertEquals("strict", mgr.getCookie("c6").getSameSite());
assertEquals("strict", mgr.getCookie("c7").getSameSite());
}
}
Aggregations