use of com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts in project htmlunit by HtmlUnit.
the class CookieManagerTest method cookie2.
/**
* @throws Exception if the test fails
*/
@Test
@Alerts("first=1")
public void cookie2() throws Exception {
final List<NameValuePair> responseHeader1 = new ArrayList<>();
responseHeader1.add(new NameValuePair("Set-Cookie", "first=1"));
getMockWebConnection().setResponse(URL_FIRST, HTML_ALERT_COOKIE, 200, "OK", MimeType.TEXT_HTML, responseHeader1);
loadPageWithAlerts2(URL_FIRST);
final List<NameValuePair> responseHeader2 = new ArrayList<>();
responseHeader2.add(new NameValuePair("Set-Cookie", "second=2"));
getMockWebConnection().setResponse(URL_FIRST, HTML_ALERT_COOKIE, 200, "OK", MimeType.TEXT_HTML, responseHeader2);
setExpectedAlerts("first=1; second=2");
loadPageWithAlerts2(URL_FIRST);
}
use of com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts in project htmlunit by HtmlUnit.
the class CookieManagerTest method setCookieExpired_badDateFormat.
/**
* Regression test for bug 3081652: it seems that expiration date should be ignored if format is incorrect.
* @throws Exception if the test fails
*/
@Test
@Alerts(DEFAULT = "fourth=4; third=3", FF = "first=1; second=2; third=3", FF_ESR = "first=1; second=2; third=3", IE = "first=1; fourth=4; second=2; third=3")
public void setCookieExpired_badDateFormat() throws Exception {
final List<NameValuePair> responseHeader1 = new ArrayList<>();
responseHeader1.add(new NameValuePair("Set-Cookie", "first=1;expires=Dec-1-94 16:00:00"));
responseHeader1.add(new NameValuePair("Set-Cookie", "second=2;expires=Dec-1-1994 16:00:00"));
responseHeader1.add(new NameValuePair("Set-Cookie", "third=3;expires=Dec-1-2094 16:00:00"));
responseHeader1.add(new NameValuePair("Set-Cookie", "fourth=4;expires=1/1/2000; path=/"));
getMockWebConnection().setResponse(URL_FIRST, HTML_ALERT_COOKIE, 200, "OK", MimeType.TEXT_HTML, responseHeader1);
loadPageWithAlerts2(URL_FIRST);
}
use of com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts in project htmlunit by HtmlUnit.
the class CookieManagerTest method httpOnly.
/**
* HttpOnly cookies should not be available from JS.
* @throws Exception if the test fails
*/
@Test
@Alerts("second=2")
public void httpOnly() throws Exception {
final List<NameValuePair> responseHeader = new ArrayList<>();
responseHeader.add(new NameValuePair("Set-Cookie", "first=1; path=/; HttpOnly"));
responseHeader.add(new NameValuePair("Set-Cookie", "second=2; path=/;"));
getMockWebConnection().setDefaultResponse("");
getMockWebConnection().setResponse(URL_FIRST, HTML_ALERT_COOKIE, 200, "OK", MimeType.TEXT_HTML, responseHeader);
final WebDriver driver = loadPageWithAlerts2(URL_FIRST);
driver.get(URL_FIRST + "foo");
final Map<String, String> lastHeaders = getMockWebConnection().getLastAdditionalHeaders();
// strange check, but there is no order
final String lastCookies = lastHeaders.get(HttpHeader.COOKIE);
assertEquals(17, lastCookies.length());
assertTrue("lastCookies: " + lastCookies, lastCookies.contains("first=1") && lastCookies.contains("second=2") && lastCookies.contains("; "));
if (driver instanceof HtmlUnitDriver) {
final CookieManager mgr = getWebWindowOf((HtmlUnitDriver) driver).getWebClient().getCookieManager();
assertEquals(2, mgr.getCookies().size());
assertTrue(mgr.getCookie("first").isHttpOnly());
assertFalse(mgr.getCookie("second").isHttpOnly());
}
}
use of com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts in project htmlunit by HtmlUnit.
the class CookieManagerTest method setCookieExpired.
/**
* Tests for expiration date.
* Tests as well for bug 3421201 (with expiration date enclosed with quotes).
* @throws Exception if the test fails
*/
@Test
@Alerts("second=2; visitor=f2")
public void setCookieExpired() throws Exception {
// one hour later
final Date aBitLater = new Date(new Date().getTime() + 60 * 60 * 1000);
final List<NameValuePair> responseHeader1 = new ArrayList<>();
responseHeader1.add(new NameValuePair("Set-Cookie", "first=1;expires=Fri, 02-Jan-1970 00:00:00 GMT"));
responseHeader1.add(new NameValuePair("Set-Cookie", "second=2;expires=" + DateUtils.formatDate(aBitLater)));
responseHeader1.add(new NameValuePair("Set-Cookie", "visit=fo; expires=\"Sat, 07-Apr-2002 13:11:33 GMT\";"));
responseHeader1.add(new NameValuePair("Set-Cookie", "visitor=f2; expires=\"Sat, 07-Apr-2092 13:11:33 GMT\";"));
getMockWebConnection().setResponse(URL_FIRST, HTML_ALERT_COOKIE, 200, "OK", MimeType.TEXT_HTML, responseHeader1);
loadPageWithAlerts2(URL_FIRST);
}
use of com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts in project htmlunit by HtmlUnit.
the class CookieManagerTest method setCookieDifferentPathSlashAtEnd.
/**
* Response to /a/b sets cookie to /foo/blah.
* @throws Exception if the test fails
*/
@Test
@Alerts("first=1; second=2")
public void setCookieDifferentPathSlashAtEnd() throws Exception {
final List<NameValuePair> responseHeader1 = new ArrayList<>();
responseHeader1.add(new NameValuePair("Set-Cookie", "first=1; path=/foo"));
responseHeader1.add(new NameValuePair("Set-Cookie", "second=2; path=/foo/"));
responseHeader1.add(new NameValuePair("Set-Cookie", "third=3; path=/foo/other"));
responseHeader1.add(new NameValuePair("Set-Cookie", "fourth=4; path=/other/path"));
responseHeader1.add(new NameValuePair("Location", "/foo/"));
getMockWebConnection().setDefaultResponse(HTML_ALERT_COOKIE);
final URL firstUrl = new URL(URL_FIRST, "/a/b");
getMockWebConnection().setResponse(firstUrl, "", 302, "Moved", MimeType.TEXT_HTML, responseHeader1);
loadPageWithAlerts2(firstUrl);
}
Aggregations