Search in sources :

Example 31 with Header

use of org.apache.commons.httpclient.Header in project sling by apache.

the class RangeStreamingTest method test_multiple_ranges.

public void test_multiple_ranges() throws IOException {
    GetMethod get = new GetMethod(rootUrl);
    get.setRequestHeader(new Header("Range", "bytes 0-9,-10"));
    int status = httpClient.executeMethod(get);
    assertEquals("Expect 206/PARTIAL CONTENT", 206, status);
    String contentType = get.getResponseHeader("Content-Type").getValue();
    assertTrue("Content Type must be multipart/byteranges", contentType.contains("multipart/byteranges"));
    String boundary = contentType.substring(contentType.indexOf("boundary=") + "boundary=".length());
    BufferedReader reader = new BufferedReader(new InputStreamReader(get.getResponseBodyAsStream()));
    String line = reader.readLine();
    while (!("--" + boundary).equals(line)) {
        line = reader.readLine();
    }
    assertEquals("Expected content to start with boundary", "--" + boundary, line);
    assertEntityHeaders(reader, "text/plain", "bytes 0-9/79");
    assertEquals("The quick ", reader.readLine());
    assertEquals("Expected content to start with boundary", "--" + boundary, reader.readLine());
    assertEntityHeaders(reader, "text/plain", "bytes 69-78/79");
    assertEquals("corpus sic", reader.readLine());
    char[] buf = new char[boundary.length() + 4];
    reader.read(buf);
    assertEquals("Expected content to start with boundary", "--" + boundary + "--", new String(buf));
}
Also used : Header(org.apache.commons.httpclient.Header) InputStreamReader(java.io.InputStreamReader) GetMethod(org.apache.commons.httpclient.methods.GetMethod) BufferedReader(java.io.BufferedReader)

Example 32 with Header

use of org.apache.commons.httpclient.Header in project sling by apache.

the class HttpPingTest method testWebServerRoot.

public void testWebServerRoot() throws Exception {
    // by default, the Launchpad default servlet redirects / to index.html
    final String url = HTTP_BASE_URL + "/";
    final GetMethod get = new GetMethod(url);
    get.setFollowRedirects(false);
    final int status = httpClient.executeMethod(get);
    assertEquals("Status must be 302 for " + url, 302, status);
    final Header h = get.getResponseHeader("Location");
    assertNotNull("Location header must be provided", h);
    assertTrue("Location header must end with index.html", h.getValue().endsWith("index.html"));
}
Also used : Header(org.apache.commons.httpclient.Header) GetMethod(org.apache.commons.httpclient.methods.GetMethod)

Example 33 with Header

use of org.apache.commons.httpclient.Header in project sling by apache.

the class PostServletOutputContentTypeTest method runTest.

private void runTest(String acceptHeaderValue, boolean useHttpEquiv, String expectedContentType) throws Exception {
    final String info = (useHttpEquiv ? "Using http-equiv parameter" : "Using Accept header") + ": ";
    final String url = HTTP_BASE_URL + MY_TEST_PATH;
    final PostMethod post = new PostMethod(url);
    post.setFollowRedirects(false);
    if (acceptHeaderValue != null) {
        if (useHttpEquiv) {
            post.addParameter(":http-equiv-accept", acceptHeaderValue);
        } else {
            post.addRequestHeader("Accept", acceptHeaderValue);
        }
    }
    final int status = httpClient.executeMethod(post) / 100;
    assertEquals(info + "Expected status 20x for POST at " + url, 2, status);
    final Header h = post.getResponseHeader("Content-Type");
    assertNotNull(info + "Expected Content-Type header", h);
    final String ct = h.getValue();
    assertTrue(info + "Expected Content-Type '" + expectedContentType + "' for Accept header=" + acceptHeaderValue + " but got '" + ct + "'", ct.startsWith(expectedContentType));
}
Also used : Header(org.apache.commons.httpclient.Header) PostMethod(org.apache.commons.httpclient.methods.PostMethod)

Example 34 with Header

use of org.apache.commons.httpclient.Header in project sling by apache.

the class AuthenticationResponseCodeTest method testWithNonHtmlAcceptHeaderIncorrectCredentials.

@Test
public void testWithNonHtmlAcceptHeaderIncorrectCredentials() throws Exception {
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new NameValuePair("j_username", "garbage"));
    params.add(new NameValuePair("j_password", "garbage"));
    List<Header> headers = new ArrayList<Header>();
    headers.add(new Header("User-Agent", "Mozilla/5.0 Sling Integration Test"));
    assertPostStatus(HttpTest.HTTP_BASE_URL + "/j_security_check", HttpServletResponse.SC_MOVED_TEMPORARILY, params, headers, null);
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) Header(org.apache.commons.httpclient.Header) ArrayList(java.util.ArrayList) HttpTest(org.apache.sling.commons.testing.integration.HttpTest) Test(org.junit.Test)

Example 35 with Header

use of org.apache.commons.httpclient.Header in project sling by apache.

the class AuthenticationResponseCodeTest method testValidatingIncorrectCookie.

@Test
public void testValidatingIncorrectCookie() throws Exception {
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new NameValuePair("j_validate", "true"));
    List<Header> headers = new ArrayList<Header>();
    headers.add(new Header("Cookie", "sling.formauth=garbage"));
    HttpMethod post = assertPostStatus(HttpTest.HTTP_BASE_URL + "/j_security_check", HttpServletResponse.SC_FORBIDDEN, params, headers, null);
    assertXReason(post);
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) Header(org.apache.commons.httpclient.Header) ArrayList(java.util.ArrayList) HttpMethod(org.apache.commons.httpclient.HttpMethod) HttpTest(org.apache.sling.commons.testing.integration.HttpTest) Test(org.junit.Test)

Aggregations

Header (org.apache.commons.httpclient.Header)93 GetMethod (org.apache.commons.httpclient.methods.GetMethod)23 Test (org.junit.Test)22 PostMethod (org.apache.commons.httpclient.methods.PostMethod)21 IOException (java.io.IOException)20 HttpClient (org.apache.commons.httpclient.HttpClient)20 NameValuePair (org.apache.commons.httpclient.NameValuePair)18 HttpMethod (org.apache.commons.httpclient.HttpMethod)17 ArrayList (java.util.ArrayList)16 InputStream (java.io.InputStream)14 HttpException (org.apache.commons.httpclient.HttpException)13 PutMethod (org.apache.commons.httpclient.methods.PutMethod)10 Account (com.zimbra.cs.account.Account)9 ServiceException (com.zimbra.common.service.ServiceException)6 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)6 HttpTest (org.apache.sling.commons.testing.integration.HttpTest)6 Pair (com.zimbra.common.util.Pair)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 HashMap (java.util.HashMap)5 DefaultHttpMethodRetryHandler (org.apache.commons.httpclient.DefaultHttpMethodRetryHandler)5