// TEST 1: check getRequestProperty doesn't return the HttpOnly cookie // In fact, that it doesn't return any automatically set cookies.
String cookie = uc.getRequestProperty("Cookie");
check(cookie == null, "Cookie header returned from getRequestProperty, value " + cookie);
// TEST 2: check getRequestProperties doesn't return the HttpOnly cookie. // In fact, that it doesn't return any automatically set cookies.
Map<String,List<String>> reqHeaders = uc.getRequestProperties();
Set<Map.Entry<String,List<String>>> entries = reqHeaders.entrySet(); for (Map.Entry<String,List<String>> entry : entries) {
String header = entry.getKey();
check(!"Cookie".equalsIgnoreCase(header), "Cookie header returned from getRequestProperties, value " +
entry.getValue());
}
// TEST 3: check getHeaderField doesn't return Set-Cookie with HttpOnly
String setCookie = uc.getHeaderField("Set-Cookie"); if (setCookie != null) {
debug("Set-Cookie:" + setCookie);
check(!setCookie.toLowerCase().contains("httponly"), "getHeaderField returned Set-Cookie header with HttpOnly, " + "value = " + setCookie);
}
// TEST 3.5: check getHeaderField doesn't return Set-Cookie2 with HttpOnly
String setCookie2 = uc.getHeaderField("Set-Cookie2"); if (setCookie2 != null) {
debug("Set-Cookie2:" + setCookie2);
check(!setCookie2.toLowerCase().contains("httponly"), "getHeaderField returned Set-Cookie2 header with HttpOnly, " + "value = " + setCookie2);
}
// TEST 4: check getHeaderFields doesn't return Set-Cookie // or Set-Cookie2 headers with HttpOnly
Map<String,List<String>> respHeaders = uc.getHeaderFields();
Set<Map.Entry<String,List<String>>> respEntries = respHeaders.entrySet(); for (Map.Entry<String,List<String>> entry : respEntries) {
String header = entry.getKey(); if ("Set-Cookie".equalsIgnoreCase(header)) {
List<String> setCookieValues = entry.getValue();
debug("Set-Cookie:" + setCookieValues); for (String value : setCookieValues)
check(!value.toLowerCase().contains("httponly"), "getHeaderFields returned Set-Cookie header with HttpOnly, "
+ "value = " + value);
} if ("Set-Cookie2".equalsIgnoreCase(header)) {
List<String> setCookieValues = entry.getValue();
debug("Set-Cookie2:" + setCookieValues); for (String value : setCookieValues)
check(!value.toLowerCase().contains("httponly"), "getHeaderFields returned Set-Cookie2 header with HttpOnly, "
+ "value = " + value);
}
}
// Now add some user set cookies into the mix.
uc = (HttpURLConnection) uri.toURL().openConnection(Proxy.NO_PROXY);
uc.addRequestProperty("Cookie", "CUSTOMER_ID=CHEGAR;");
resp = uc.getResponseCode();
check(resp == 200, "Unexpected response code. Expected 200, got " + resp);
// TEST 5: check getRequestProperty doesn't return the HttpOnly cookie
cookie = uc.getRequestProperty("Cookie");
check(!cookie.toLowerCase().contains("httponly"), "HttpOnly cookie returned from getRequestProperty, value " + cookie);
// TEST 6: check getRequestProperties doesn't return the HttpOnly cookie.
reqHeaders = uc.getRequestProperties();
entries = reqHeaders.entrySet(); for (Map.Entry<String,List<String>> entry : entries) {
String header = entry.getKey(); if ("Cookie".equalsIgnoreCase(header)) { for (String val : entry.getValue())
check(!val.toLowerCase().contains("httponly"), "HttpOnly cookie returned from getRequestProperties," + " value " + val);
}
}
// TEST 7 : check that header keys containing empty key values don't return null int i = 1;
String key = "";
String value = "";
while (true) {
key = uc.getHeaderFieldKey(i);
value = uc.getHeaderField(i++); if (key == null && value == null) break;
if (key != null)
check(value != null, "Encountered a null value for key value : " + key);
}
// TEST 7.5 similar test but use getHeaderFields
respHeaders = uc.getHeaderFields();
respEntries = respHeaders.entrySet(); for (Map.Entry<String,List<String>> entry : respEntries) {
String header = entry.getKey(); if (header != null) {
List<String> listValues = entry.getValue(); for (String value1 : listValues)
check(value1 != null, "getHeaderFields returned null values for header:, "
+ header);
}
}
}
// some small sanity check
List<String> cookies = reqHeaders.get("Cookie"); for (String cookie : cookies) { if (!cookie.contains("JSESSIONID")
|| !cookie.contains("WILE_E_COYOTE"))
t.sendResponseHeaders(400, -1);
}
// return some cookies so we can check getHeaderField(s)
Headers respHeaders = t.getResponseHeaders();
List<String> values = new ArrayList<>();
values.add("ID=JOEBLOGGS; version=1; Path=" + URI_PATH);
values.add("NEW_JSESSIONID=" + (SESSION_ID+1) + "; version=1; Path="
+ URI_PATH +"; HttpOnly");
values.add("NEW_CUSTOMER=WILE_E_COYOTE2; version=1; Path=" + URI_PATH);
respHeaders.put("Set-Cookie", values);
values = new ArrayList<>();
values.add("COOKIE2_CUSTOMER=WILE_E_COYOTE2; version=1; Path="
+ URI_PATH);
respHeaders.put("Set-Cookie2", values);
values.add("COOKIE2_JSESSIONID=" + (SESSION_ID+100)
+ "; version=1; Path=" + URI_PATH +"; HttpOnly");
respHeaders.put("Set-Cookie2", values);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.