private List<NameValuePair> responseHeaders() { Collection<String> headerNames = this.response.getHeaderNames(); List<NameValuePair> responseHeaders = new ArrayList<>(headerNames.size()); for (String headerName : headerNames) { List<Object> headerValues = this.response.getHeaderValues(headerName); for (Object value : headerValues) { responseHeaders.add(new NameValuePair(headerName, String.valueOf(value))); } } String location = this.response.getRedirectedUrl(); if (location != null) { responseHeaders.add(new NameValuePair("Location", location)); } return responseHeaders; }
private com.gargoylesoftware.htmlunit.util.Cookie createCookie(MockHttpServletRequest request, String sessionid) { return new com.gargoylesoftware.htmlunit.util.Cookie(request.getServerName(), "JSESSIONID", sessionid, request.getContextPath() + "/", null, request.isSecure(), true); }
/** * {@inheritDoc} */ @Override public String toString() { return getName() + "=" + getValue() + (getDomain() != null ? ";domain=" + getDomain() : "") + (getPath() != null ? ";path=" + getPath() : "") + (getExpires() != null ? ";expires=" + getExpires() : "") + (isSecure() ? ";secure" : "") + (isHttpOnly() ? ";httpOnly" : ""); }
private void cookies(MockHttpServletRequest request) { List<Cookie> cookies = new ArrayList<>(); String cookieHeaderValue = header("Cookie"); if (cookieHeaderValue != null) { StringTokenizer tokens = new StringTokenizer(cookieHeaderValue, "=;"); while (tokens.hasMoreTokens()) { String cookieName = tokens.nextToken().trim(); Assert.isTrue(tokens.hasMoreTokens(), () -> "Expected value for cookie name '" + cookieName + "': full cookie header was [" + cookieHeaderValue + "]"); String cookieValue = tokens.nextToken().trim(); processCookie(request, cookies, new Cookie(cookieName, cookieValue)); } } Set<com.gargoylesoftware.htmlunit.util.Cookie> managedCookies = this.webClient.getCookies(this.webRequest.getUrl()); for (com.gargoylesoftware.htmlunit.util.Cookie cookie : managedCookies) { processCookie(request, cookies, new Cookie(cookie.getName(), cookie.getValue())); } Cookie[] parentCookies = request.getCookies(); if (parentCookies != null) { for (Cookie cookie : parentCookies) { cookies.add(cookie); } } if (!ObjectUtils.isEmpty(cookies)) { request.setCookies(cookies.toArray(new Cookie[0])); } }
private void assertSingleSessionCookie(String expected) { com.gargoylesoftware.htmlunit.util.Cookie jsessionidCookie = webClient.getCookieManager().getCookie("JSESSIONID"); if (expected == null || expected.contains("Expires=Thu, 01-Jan-1970 00:00:01 GMT")) { assertThat(jsessionidCookie, nullValue()); return; } String actual = jsessionidCookie.getValue(); assertThat("JSESSIONID=" + actual + "; Path=/test; Domain=example.com", equalTo(expected)); }
private void storeCookies(WebRequest webRequest, javax.servlet.http.Cookie[] cookies) { Date now = new Date(); CookieManager cookieManager = this.webClient.getCookieManager(); for (javax.servlet.http.Cookie cookie : cookies) { if (cookie.getDomain() == null) { cookie.setDomain(webRequest.getUrl().getHost()); } Cookie toManage = createCookie(cookie); Date expires = toManage.getExpires(); if (expires == null || expires.after(now)) { cookieManager.addCookie(toManage); } else { cookieManager.removeCookie(toManage); } } }
private void params(MockHttpServletRequest request, UriComponents uriComponents) { uriComponents.getQueryParams().forEach((name, values) -> { String urlDecodedName = urlDecode(name); values.forEach(value -> { value = (value != null ? urlDecode(value) : ""); request.addParameter(urlDecodedName, value); }); }); for (NameValuePair param : this.webRequest.getRequestParameters()) { request.addParameter(param.getName(), param.getValue()); } }
private List<NameValuePair> responseHeaders() { HttpHeaders responseHeaders = this.exchangeResult.getResponseHeaders(); List<NameValuePair> result = new ArrayList<>(responseHeaders.size()); responseHeaders.forEach( (headerName, headerValues) -> headerValues.forEach( headerValue -> result.add(new NameValuePair(headerName, headerValue)) ) ); return result; }
private void cookies(WebTestClient.RequestBodySpec request, WebRequest webRequest) { String cookieHeaderValue = header("Cookie", webRequest); if (cookieHeaderValue != null) { StringTokenizer tokens = new StringTokenizer(cookieHeaderValue, "=;"); while (tokens.hasMoreTokens()) { String cookieName = tokens.nextToken().trim(); Assert.isTrue(tokens.hasMoreTokens(), () -> "Expected value for cookie name '" + cookieName + "': full cookie header was [" + cookieHeaderValue + "]"); String cookieValue = tokens.nextToken().trim(); request.cookie(cookieName, cookieValue); } } Set<com.gargoylesoftware.htmlunit.util.Cookie> managedCookies = this.webClient.getCookies(webRequest.getUrl()); for (com.gargoylesoftware.htmlunit.util.Cookie cookie : managedCookies) { request.cookie(cookie.getName(), cookie.getValue()); } }
private static com.gargoylesoftware.htmlunit.util.Cookie createCookie(javax.servlet.http.Cookie cookie) { Date expires = null; if (cookie.getMaxAge() > -1) { expires = new Date(System.currentTimeMillis() + cookie.getMaxAge() * 1000); } BasicClientCookie result = new BasicClientCookie(cookie.getName(), cookie.getValue()); result.setDomain(cookie.getDomain()); result.setComment(cookie.getComment()); result.setExpiryDate(expires); result.setPath(cookie.getPath()); result.setSecure(cookie.getSecure()); if (cookie.isHttpOnly()) { result.setAttribute("httponly", "true"); } return new com.gargoylesoftware.htmlunit.util.Cookie(result); }
@Test public void buildResponseHeadersNullDomainDefaulted() throws Exception { Cookie cookie = new Cookie("cookieA", "valueA"); this.response.addCookie(cookie); WebResponse webResponse = this.responseBuilder.build(); List<NameValuePair> responseHeaders = webResponse.getResponseHeaders(); assertThat(responseHeaders.size(), equalTo(1)); NameValuePair header = responseHeaders.get(0); assertThat(header.getName(), equalTo("Set-Cookie")); assertThat(header.getValue(), equalTo("cookieA=valueA")); }
@Test public void buildRequestParameterMapViaWebRequestDotSetRequestParametersWithMultipleRequestParams() { webRequest.setRequestParameters(asList(new NameValuePair("name1", "value1"), new NameValuePair("name2", "value2"))); MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext); assertThat(actualRequest.getParameterMap().size(), equalTo(2)); assertThat(actualRequest.getParameter("name1"), equalTo("value1")); assertThat(actualRequest.getParameter("name2"), equalTo("value2")); }
@Test // SPR-14066 public void cookieManagerShared() throws Exception { this.mockMvc = MockMvcBuilders.standaloneSetup(new CookieController()).build(); WebClient client = MockMvcWebClientBuilder.mockMvcSetup(this.mockMvc).build(); assertThat(getResponse(client, "http://localhost/").getContentAsString(), equalTo("NA")); client.getCookieManager().addCookie(new Cookie("localhost", "cookie", "cookieManagerShared")); assertThat(getResponse(client, "http://localhost/").getContentAsString(), equalTo("cookieManagerShared")); }
@Test public void buildResponseHeaders() throws Exception { this.response.addHeader("Content-Type", "text/html"); this.response.addHeader("X-Test", "value"); Cookie cookie = new Cookie("cookieA", "valueA"); cookie.setDomain("domain"); cookie.setPath("/path"); cookie.setMaxAge(1800); cookie.setSecure(true); cookie.setHttpOnly(true); this.response.addCookie(cookie); WebResponse webResponse = this.responseBuilder.build(); List<NameValuePair> responseHeaders = webResponse.getResponseHeaders(); assertThat(responseHeaders.size(), equalTo(3)); NameValuePair header = responseHeaders.get(0); assertThat(header.getName(), equalTo("Content-Type")); assertThat(header.getValue(), equalTo("text/html")); header = responseHeaders.get(1); assertThat(header.getName(), equalTo("X-Test")); assertThat(header.getValue(), equalTo("value")); header = responseHeaders.get(2); assertThat(header.getName(), equalTo("Set-Cookie")); assertThat(header.getValue(), startsWith("cookieA=valueA; Path=/path; Domain=domain; Max-Age=1800; Expires=")); assertThat(header.getValue(), endsWith("; Secure; HttpOnly")); }
@Test public void buildRequestParameterMapViaWebRequestDotSetRequestParametersWithSingleRequestParamWithValueSetToSpace() { webRequest.setRequestParameters(asList(new NameValuePair("name", " "))); MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext); assertThat(actualRequest.getParameterMap().size(), equalTo(1)); assertThat(actualRequest.getParameter("name"), equalTo(" ")); }
@Test // SPR-14066 public void cookieManagerShared() throws Exception { WebConnectionHtmlUnitDriver otherDriver = new WebConnectionHtmlUnitDriver(); this.mockMvc = MockMvcBuilders.standaloneSetup(new CookieController()).build(); this.driver = MockMvcHtmlUnitDriverBuilder.mockMvcSetup(this.mockMvc) .withDelegate(otherDriver).build(); assertThat(get("http://localhost/"), equalTo("")); Cookie cookie = new Cookie("localhost", "cookie", "cookieManagerShared"); otherDriver.getWebClient().getCookieManager().addCookie(cookie); assertThat(get("http://localhost/"), equalTo("cookieManagerShared")); }
@Test public void buildRequestParameterMapViaWebRequestDotSetRequestParametersWithSingleRequestParamWithNullValue() { webRequest.setRequestParameters(asList(new NameValuePair("name", null))); MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext); assertThat(actualRequest.getParameterMap().size(), equalTo(1)); assertThat(actualRequest.getParameter("name"), nullValue()); }
@Test public void buildRequestParameterMapViaWebRequestDotSetRequestParametersWithSingleRequestParam() { webRequest.setRequestParameters(asList(new NameValuePair("name", "value"))); MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext); assertThat(actualRequest.getParameterMap().size(), equalTo(1)); assertThat(actualRequest.getParameter("name"), equalTo("value")); }
@Test public void buildRequestParameterMapViaWebRequestDotSetRequestParametersWithSingleRequestParamWithEmptyValue() { webRequest.setRequestParameters(asList(new NameValuePair("name", ""))); MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext); assertThat(actualRequest.getParameterMap().size(), equalTo(1)); assertThat(actualRequest.getParameter("name"), equalTo("")); }