Search in sources :

Example 6 with WebResponse

use of com.gargoylesoftware.htmlunit.WebResponse in project spring-framework by spring-projects.

the class DelegatingWebConnectionTests method getResponseSecondMatches.

@Test
public void getResponseSecondMatches() throws Exception {
    when(matcher2.matches(request)).thenReturn(true);
    when(connection2.getResponse(request)).thenReturn(expectedResponse);
    WebResponse response = webConnection.getResponse(request);
    assertThat(response, sameInstance(expectedResponse));
    verify(matcher1).matches(request);
    verify(matcher2).matches(request);
    verifyNoMoreInteractions(connection1, defaultConnection);
    verify(connection2).getResponse(request);
}
Also used : WebResponse(com.gargoylesoftware.htmlunit.WebResponse) Test(org.junit.Test)

Example 7 with WebResponse

use of com.gargoylesoftware.htmlunit.WebResponse in project spring-framework by spring-projects.

the class MockWebResponseBuilderTests method buildResponseHeadersNullDomainDefaulted.

// SPR-14169
@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"));
}
Also used : Cookie(javax.servlet.http.Cookie) NameValuePair(com.gargoylesoftware.htmlunit.util.NameValuePair) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) Test(org.junit.Test)

Example 8 with WebResponse

use of com.gargoylesoftware.htmlunit.WebResponse in project spring-framework by spring-projects.

the class MockWebResponseBuilderTests method buildStatusNotOk.

@Test
public void buildStatusNotOk() throws Exception {
    this.response.setStatus(401);
    WebResponse webResponse = this.responseBuilder.build();
    assertThat(webResponse.getStatusCode(), equalTo(401));
    assertThat(webResponse.getStatusMessage(), equalTo("Unauthorized"));
}
Also used : WebResponse(com.gargoylesoftware.htmlunit.WebResponse) Test(org.junit.Test)

Example 9 with WebResponse

use of com.gargoylesoftware.htmlunit.WebResponse in project spring-framework by spring-projects.

the class MockWebResponseBuilderTests method buildResponseHeaders.

@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;domain=domain;path=/path;expires="));
    assertThat(header.getValue(), endsWith(";secure;httpOnly"));
}
Also used : Cookie(javax.servlet.http.Cookie) NameValuePair(com.gargoylesoftware.htmlunit.util.NameValuePair) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) Test(org.junit.Test)

Example 10 with WebResponse

use of com.gargoylesoftware.htmlunit.WebResponse in project javaee7-samples by javaee-samples.

the class LoginServletTest method testAuthenticatedRequest.

@Test
public void testAuthenticatedRequest() throws IOException, SAXException {
    WebClient webClient = new WebClient();
    WebRequest request = new WebRequest(new URL(base + "/LoginServlet?user=u1&password=p1"), HttpMethod.GET);
    WebResponse response = webClient.getWebConnection().getResponse(request);
    String responseText = response.getContentAsString();
    System.out.println(responseText);
    assert (responseText.contains("isUserInRole?true"));
    assert (responseText.contains("getRemoteUser?u1"));
    assert (responseText.contains("getUserPrincipal?u1"));
    assert (responseText.contains("getAuthType?BASIC"));
}
Also used : WebResponse(com.gargoylesoftware.htmlunit.WebResponse) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) WebClient(com.gargoylesoftware.htmlunit.WebClient) URL(java.net.URL) Test(org.junit.Test)

Aggregations

WebResponse (com.gargoylesoftware.htmlunit.WebResponse)22 Test (org.junit.Test)14 NameValuePair (com.gargoylesoftware.htmlunit.util.NameValuePair)5 WebRequest (com.gargoylesoftware.htmlunit.WebRequest)4 URL (java.net.URL)4 WebClient (com.gargoylesoftware.htmlunit.WebClient)3 WebResponseData (com.gargoylesoftware.htmlunit.WebResponseData)3 StringWebResponse (com.gargoylesoftware.htmlunit.StringWebResponse)2 Cookie (javax.servlet.http.Cookie)2 JvmAttributeGaugeSet (com.codahale.metrics.JvmAttributeGaugeSet)1 MetricRegistry (com.codahale.metrics.MetricRegistry)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 HttpWebConnection (com.gargoylesoftware.htmlunit.HttpWebConnection)1 NicelyResynchronizingAjaxController (com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController)1 WebConnection (com.gargoylesoftware.htmlunit.WebConnection)1 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1