private Charset getCharset() { Charset charset = this.webRequest.getCharset(); return (charset != null ? charset : StandardCharsets.ISO_8859_1); }
private CookieManager getCookieManager() { return this.webClient.getCookieManager(); }
private void headers(MockHttpServletRequest request) { this.webRequest.getAdditionalHeaders().forEach(request::addHeader); }
@Test public void contextPathExplicit() throws IOException { this.webClient.setWebConnection(new MockMvcWebConnection(this.mockMvc, this.webClient, "/context")); Page page = this.webClient.getPage("http://localhost/context/a"); assertThat(page.getWebResponse().getStatusCode(), equalTo(200)); }
@Test public void forward() throws IOException { this.webClient.setWebConnection(new MockMvcWebConnection(this.mockMvc, this.webClient, "")); Page page = this.webClient.getPage("http://localhost/forward"); assertThat(page.getWebResponse().getContentAsString(), equalTo("hello")); }
/** * Build the {@link WebClient} configured via this builder. * <p>The returned client will use the configured {@link MockMvc} instance * for processing any {@linkplain WebRequestMatcher matching} requests * and a delegate {@code WebClient} for all other requests. * <p>If a {@linkplain #withDelegate delegate} has been explicitly configured, * it will be used; otherwise, a default {@code WebClient} will be configured * as the delegate. * @return the {@code WebClient} to use * @see #withDelegate(WebClient) */ public WebClient build() { return (this.webClient != null ? this.webClient : withDelegate(new WebClient()).build()); }
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)); }
@Before public void setup() throws Exception { request = new WebRequest(new URL("http://localhost/")); WebResponseData data = new WebResponseData("".getBytes("UTF-8"), 200, "", Collections.emptyList()); expectedResponse = new WebResponse(data, request, 100L); webConnection = new DelegatingWebConnection(defaultConnection, new DelegateWebConnection(matcher1, connection1), new DelegateWebConnection(matcher2, connection2)); }
private WebResponse getResponse(WebConnection connection, String url) throws IOException { return connection.getResponse(new WebRequest(new URL(url))); }
private WebResponse createResponse(WebClient client, WebRequest request) throws IOException { return client.getWebConnection().getResponse(request); }
/** * Set the {@link WebConnection} to be used with the {@link WebClient}. * @param webConnection the {@code WebConnection} to use */ public void setWebConnection(WebConnection webConnection) { Assert.notNull(webConnection, "WebConnection must not be null"); getWebClient().setWebConnection(webConnection); }
private void contentType(MockHttpServletRequest request) { String contentType = header("Content-Type"); if (contentType == null) { FormEncodingType encodingType = this.webRequest.getEncodingType(); if (encodingType != null) { contentType = encodingType.getName(); } } request.setContentType(contentType != null ? contentType : MediaType.ALL_VALUE); }
private void addSessionCookie(MockHttpServletRequest request, String sessionid) { getCookieManager().addCookie(createCookie(request, sessionid)); }
/** * Access the current {@link WebConnection} for the {@link WebClient}. * @return the current {@code WebConnection} */ public WebConnection getWebConnection() { return getWebClient().getWebConnection(); }
@Override public WebResponse getResponse(WebRequest request) throws IOException { for (DelegateWebConnection connection : this.connections) { if (connection.getMatcher().matches(request)) { return connection.getDelegate().getResponse(request); } } return this.defaultConnection.getResponse(request); }
public WebResponse build() throws IOException { WebResponseData webResponseData = webResponseData(); long endTime = System.currentTimeMillis(); return new WebResponse(webResponseData, this.webRequest, endTime - this.startTime); }
private void content(MockHttpServletRequest request, Charset charset) { String requestBody = this.webRequest.getRequestBody(); if (requestBody == null) { return; } request.setContent(requestBody.getBytes(charset)); }
private void removeSessionCookie(MockHttpServletRequest request, String sessionid) { getCookieManager().removeCookie(createCookie(request, sessionid)); }
@Test public void contextPathNull() throws IOException { this.webClient.setWebConnection(new MockMvcWebConnection(this.mockMvc, this.webClient)); Page page = this.webClient.getPage("http://localhost/context/a"); assertThat(page.getWebResponse().getStatusCode(), equalTo(200)); }
@Test public void contextPathEmpty() throws IOException { this.webClient.setWebConnection(new MockMvcWebConnection(this.mockMvc, this.webClient, "")); Page page = this.webClient.getPage("http://localhost/context/a"); assertThat(page.getWebResponse().getStatusCode(), equalTo(200)); }