Java Code Examples for com.meterware.httpunit.WebResponse#getHeaderFields()

The following examples show how to use com.meterware.httpunit.WebResponse#getHeaderFields() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AggregatorTest.java    From esigate with Apache License 2.0 6 votes vote down vote up
private void doCookieSimpleTest(String page, String resultResource) throws Exception {
    WebRequest req = new GetMethodWebRequest(APPLICATION_PATH + page);
    WebResponse resp = webConversation.getResponse(req);

    assertEquals("Status should be 200", HttpServletResponse.SC_OK, resp.getResponseCode());
    assertEqualsIgnoreWhiteSpaces(getResource(resultResource).replaceAll("JSESSIONID=[^;]+;", ""), resp.getText()
            .replaceAll("JSESSIONID=[^;]+;", ""));

    String[] setcookies = resp.getHeaderFields("Set-Cookie");
    boolean containsHttpOnlyCookie = false;
    for (String setcookie : setcookies) {
        if (setcookie.contains("test0")) {
            assertTrue(setcookie.contains("HttpOnly"));
            containsHttpOnlyCookie = true;
        }
    }
    assertTrue("Response should contains an HttpOnly cookie test0", containsHttpOnlyCookie);

}
 
Example 2
Source File: ResponseHeadersTest.java    From esigate with Apache License 2.0 6 votes vote down vote up
/**
 * Content-type is often modified by tha application server as it automatically sets it if not defined and it is
 * case-insensitive so we can have differences depending on the server vendor and version. So we just test that
 * 'text/plain' was forwarded, no matter the charset.
 * 
 * @throws Exception
 */
public void testContentType() throws Exception {
    // FIXME not easy to test with arbitrary values as application servers
    // automatically set this header.
    // String resp = sendRequestAndExpectResponseHeader("Content-Type",
    // "text/plain");
    // if (!StringUtils.startsWithIgnoreCase(resp, "text/plain")) {
    // fail("HTTP header Content-Type should be forwarded, expected 'text/plain', got '"
    // + resp + "'");
    // }
    WebConversation webConversation = new WebConversation();
    WebRequest req = new GetMethodWebRequest(APPLICATION_PATH + "nocache/ag1/response-headers.jsp");
    WebResponse resp = webConversation.getResponse(req);
    String[] responseHeader = resp.getHeaderFields("Content-type");
    if (responseHeader == null || responseHeader.length != 1) {
        fail("There should be one and only one Content-type header in the response, found " + responseHeader.length);
    }
}
 
Example 3
Source File: AggregatorTest.java    From esigate with Apache License 2.0 5 votes vote down vote up
public void testRedirect() throws Exception {
    WebRequest req = new GetMethodWebRequest(APPLICATION_PATH + "redirect.jsp");
    webConversation.getClientProperties().setAutoRedirect(false);
    WebResponse resp = webConversation.getResponse(req);
    assertEquals("Status should be " + HttpServletResponse.SC_MOVED_TEMPORARILY,
            HttpServletResponse.SC_MOVED_TEMPORARILY, resp.getResponseCode());
    String[] locations = resp.getHeaderFields("Location");
    assertNotNull(locations);
    assertEquals("should be only one location: " + Arrays.asList(locations), 1, locations.length);
    assertEquals("Redirect header did not match", "http://localhost:8080/esigate-app-aggregator/redirected.jsp",
            locations[0]);
}
 
Example 4
Source File: ResponseHeadersTest.java    From esigate with Apache License 2.0 5 votes vote down vote up
private String sendRequestAndExpectResponseHeader(String name, String value) throws Exception {
    WebConversation webConversation = new WebConversation();
    WebRequest req = new GetMethodWebRequest(APPLICATION_PATH + "nocache/ag1/response-headers.jsp");
    req.setHeaderField("X-response-header-" + name, value);
    WebResponse resp = webConversation.getResponse(req);
    String[] responseHeader = resp.getHeaderFields(name);
    if (responseHeader == null || responseHeader.length == 0) {
        return "";
    }
    String result = responseHeader[0];
    for (int i = 1; i < responseHeader.length; i++) {
        result += "\n" + responseHeader[i];
    }
    return result;
}
 
Example 5
Source File: ResponseHeadersTest.java    From esigate with Apache License 2.0 5 votes vote down vote up
public void testDate() throws Exception {
    // Note: Date header set automatically by most application servers,
    // cannot override it on Tomcat 7.0 -> problem with cache validation
    // on Jetty 6 you can override it only with setDateHeader method
    // setHeader method will not work !
    // assertHeaderForwarded("Date", "Fri, 06 Apr 2012 15:18:12 GMT");
    WebConversation webConversation = new WebConversation();
    WebRequest req = new GetMethodWebRequest(APPLICATION_PATH + "nocache/ag1/response-headers.jsp");
    WebResponse resp = webConversation.getResponse(req);
    String[] responseHeader = resp.getHeaderFields("Date");
    if (responseHeader == null || responseHeader.length > 1) {
        fail("There should be one and only one Date header in the response, found " + responseHeader.length);
    }
}
 
Example 6
Source File: MultipleHeadersTest.java    From esigate with Apache License 2.0 5 votes vote down vote up
public void testMultipleHeaders() throws Exception {
    WebRequest req = new GetMethodWebRequest(APPLICATION_PATH + "multiple-headers.jsp");
    WebResponse resp = webConversation.getResponse(req);
    assertEquals("Status should be 200", HttpServletResponse.SC_OK, resp.getResponseCode());
    String[] actual = resp.getHeaderFields("header-name");
    assertNotNull("headers named 'header-name' should appear in response", actual);
    assertEquals("there should be 2 headers", 2, actual.length);
    Arrays.sort(actual);
    assertEquals("unexpected header value", "header-value-01", actual[0]);
    assertEquals("unexpected header value", "header-value-02", actual[1]);
}