Skip to content

Commit

Permalink
Implementing a servlet on the test server that allows to set cookies …
Browse files Browse the repository at this point in the history
…on the server side. That allows to create tests for secure and httpOnly flags.
  • Loading branch information
barancev committed Mar 10, 2014
1 parent 2378c70 commit 3c2fbb2
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 13 deletions.
70 changes: 57 additions & 13 deletions java/client/test/org/openqa/selenium/CookieImplementationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,17 @@
public class CookieImplementationTest extends JUnit4TestBase {

private DomainHelper domainHelper;
private String cookiePage;
private static final Random random = new Random();

@Before
public void setUp() throws Exception {
domainHelper = new DomainHelper(appServer);
cookiePage = domainHelper.getUrlForFirstValidHostname("/common/cookie");
assumeTrue(domainHelper.checkIsOnValidHostname());

deleteAllCookiesOnServerSide();

// This page is the deepest page we go to in the cookie tests
// We go to it to ensure that cookies with /common/... paths are deleted
// Do not write test in this class which use pages other than under /common
Expand All @@ -84,8 +88,7 @@ public void testShouldGetCookieByName() {
String value = "set";
assertCookieIsNotPresentWithName(key);

((JavascriptExecutor) driver).executeScript(
"document.cookie = arguments[0] + '=' + arguments[1];", key, value);
addCookieOnServerSide(new Cookie(key, value));

Cookie cookie = driver.manage().getCookieNamed(key);
assertEquals(value, cookie.getValue());
Expand All @@ -102,6 +105,9 @@ public void testShouldBeAbleToAddCookie() {
driver.manage().addCookie(cookie);

assertCookieHasValue(key, value);

openAnotherPage();
assertCookieHasValue(key, value);
}

@Test
Expand All @@ -121,7 +127,7 @@ public void testGetAllCookies() {
driver.manage().addCookie(one);
driver.manage().addCookie(two);

driver.get(domainHelper.getUrlForFirstValidHostname("simpleTest.html"));
openAnotherPage();
cookies = driver.manage().getCookies();
assertEquals(countBefore + 2, cookies.size());

Expand All @@ -132,12 +138,15 @@ public void testGetAllCookies() {
@JavascriptEnabled
@Test
public void testDeleteAllCookies() {
((JavascriptExecutor) driver).executeScript("document.cookie = 'foo=set';");
addCookieOnServerSide(new Cookie("foo", "set"));
assertSomeCookiesArePresent();

driver.manage().deleteAllCookies();

assertNoCookiesArePresent();

openAnotherPage();
assertNoCookiesArePresent();
}

@JavascriptEnabled
Expand All @@ -146,8 +155,8 @@ public void testDeleteCookieWithName() {
String key1 = generateUniqueKey();
String key2 = generateUniqueKey();

((JavascriptExecutor) driver).executeScript("document.cookie = arguments[0] + '=set';", key1);
((JavascriptExecutor) driver).executeScript("document.cookie = arguments[0] + '=set';", key2);
addCookieOnServerSide(new Cookie(key1, "set"));
addCookieOnServerSide(new Cookie(key2, "set"));

assertCookieIsPresentWithName(key1);
assertCookieIsPresentWithName(key2);
Expand All @@ -156,6 +165,10 @@ public void testDeleteCookieWithName() {

assertCookieIsNotPresentWithName(key1);
assertCookieIsPresentWithName(key2);

openAnotherPage();
assertCookieIsNotPresentWithName(key1);
assertCookieIsPresentWithName(key2);
}

@Test
Expand Down Expand Up @@ -204,7 +217,7 @@ public void testCannotGetCookiesWithPathDifferingOnlyInCase() {
Cookie cookie = new Cookie.Builder(cookieName, "cod").path("/Common/animals").build();
driver.manage().addCookie(cookie);

driver.get(domainHelper.getUrlForFirstValidHostname("animals"));
driver.get(domainHelper.getUrlForFirstValidHostname("/common/animals"));
assertNull(driver.manage().getCookieNamed(cookieName));
}

Expand Down Expand Up @@ -417,7 +430,7 @@ private String generateUniqueKey() {
private void assertNoCookiesArePresent() {
Set<Cookie> cookies = driver.manage().getCookies();
assertTrue("Cookies were not empty, present: " + cookies,
cookies.isEmpty());
cookies.isEmpty());
String documentCookie = getDocumentCookieOrNull();
if (documentCookie != null) {
assertEquals("Cookies were not empty", "", documentCookie);
Expand All @@ -426,7 +439,7 @@ private void assertNoCookiesArePresent() {

private void assertSomeCookiesArePresent() {
assertFalse("Cookies were empty",
driver.manage().getCookies().isEmpty());
driver.manage().getCookies().isEmpty());
String documentCookie = getDocumentCookieOrNull();
if (documentCookie != null) {
assertNotSame("Cookies were empty", "", documentCookie);
Expand All @@ -438,8 +451,8 @@ private void assertCookieIsNotPresentWithName(final String key) {
String documentCookie = getDocumentCookieOrNull();
if (documentCookie != null) {
assertThat("Cookie was present with name " + key,
documentCookie,
not(containsString(key + "=")));
documentCookie,
not(containsString(key + "=")));
}
}

Expand All @@ -455,8 +468,8 @@ private void assertCookieIsPresentWithName(final String key) {

private void assertCookieHasValue(final String key, final String value) {
assertEquals("Cookie had wrong value",
value,
driver.manage().getCookieNamed(key).getValue());
value,
driver.manage().getCookieNamed(key).getValue());
String documentCookie = getDocumentCookieOrNull();
if (documentCookie != null) {
assertThat("Cookie was present with name " + key,
Expand All @@ -479,4 +492,35 @@ private String getDocumentCookieOrNull() {
private Date someTimeInTheFuture() {
return new Date(System.currentTimeMillis() + 100000);
}

private void openAnotherPage() {
driver.get(domainHelper.getUrlForFirstValidHostname("simpleTest.html"));
}

private void deleteAllCookiesOnServerSide() {
driver.get(cookiePage + "?action=deleteAll");
}

private void addCookieOnServerSide(Cookie cookie) {
StringBuilder url = new StringBuilder(cookiePage);
url.append("?action=add");
url.append("&name=").append(cookie.getName());
url.append("&value=").append(cookie.getValue());
if (cookie.getDomain() != null) {
url.append("&domain=").append(cookie.getDomain());
}
if (cookie.getPath() != null) {
url.append("&path=").append(cookie.getPath());
}
if (cookie.getExpiry() != null) {
url.append("&expiry=").append(cookie.getExpiry().getTime());
}
if (cookie.isSecure()) {
url.append("&secure=").append(cookie.isSecure());
}
if (cookie.isHttpOnly()) {
url.append("&httpOnly=").append(cookie.isHttpOnly());
}
driver.get(url.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
Copyright 2014 Selenium committers
Copyright 2014 Software Freedom Conservancy
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 org.openqa.selenium.environment.webserver;

import java.io.IOException;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieServlet extends HttpServlet {

private static final String RESPONSE_STRING =
"<html><head><title>Done</title></head><body>%s : %s</body></html>";

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.setContentType("text/html");
//Dont Cache Anything at the browser
response.setHeader("Cache-Control","no-cache");
response.setHeader("Pragma","no-cache");
response.setDateHeader ("Expires", 0);

String action = request.getParameter("action");
if ("add".equals(action)) {
String name = request.getParameter("name");
String value = request.getParameter("value");
String domain = request.getParameter("domain");
String path = request.getParameter("path");
String expiry = request.getParameter("expiry");
String secure = request.getParameter("secure");
String httpOnly = request.getParameter("httpOnly");
Cookie newCookie = new Cookie(name, value);
if (domain != null) {
newCookie.setDomain(domain);
}
if (path != null) {
newCookie.setPath(path);
}
if (expiry != null) {
newCookie.setMaxAge(Integer.parseInt(expiry));
}
if (secure != null) {
newCookie.setSecure(Boolean.parseBoolean(secure));
}
// TODO: Requires servlet-api 3.0+
//if (httpOnly != null) {
// newCookie.setHttpOnly(Boolean.parseBoolean(httpOnly));
//}
response.addCookie(newCookie);

response.getOutputStream().println(
String.format(RESPONSE_STRING, "Cookie added", name));

} else if ("delete".equals(action)) {
String name = request.getParameter("name");
for (Cookie cookie : request.getCookies()) {
if (! cookie.getName().equals(name)) {
cookie.setValue("");
cookie.setPath("/");
cookie.setMaxAge(0);
response.addCookie(cookie);
}
}
response.getOutputStream().println(
String.format(RESPONSE_STRING, "Cookie deleted", name));

} else if ("deleteAll".equals(action)) {
for (Cookie cookie : request.getCookies()) {
System.out.println(cookie);
cookie.setValue("");
cookie.setPath("/");
cookie.setMaxAge(0);
response.addCookie(cookie);
}
response.getOutputStream().println(
String.format(RESPONSE_STRING, "All cookies deleted", ""));

} else {
response.getOutputStream().println(
String.format(RESPONSE_STRING, "Unrecognized action", action));
return;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public Jetty7AppServer(String hostName) {
addServlet(defaultContext, "/upload", UploadServlet.class);
addServlet(defaultContext, "/encoding", EncodingServlet.class);
addServlet(defaultContext, "/sleep", SleepingServlet.class);
addServlet(defaultContext, "/cookie", CookieServlet.class);
addServlet(defaultContext, "/quitquitquit", KillSwitchServlet.class);
addServlet(defaultContext, "/basicAuth", BasicAuth.class);
addFilter(defaultContext, MultiPartFilter.class, "/upload", 0 /* DEFAULT dispatches */);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ java_library(

"Jetty7AppServer.java",
"BasicAuth.java",
"CookieServlet.java",
"EncodingServlet.java",
"Jetty7AppServer.java",
"KillSwitchServlet.java",
Expand Down

0 comments on commit 3c2fbb2

Please sign in to comment.