Search in sources :

Example 81 with Header

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

the class SelectorAuthenticationResponseCodeTest method testWithAcceptHeaderIncorrectCredentials.

// this method assumes the use of the selector auth bundle
@Test
public void testWithAcceptHeaderIncorrectCredentials() throws Exception {
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new NameValuePair("j_username", "garbage"));
    params.add(new NameValuePair("j_password", "garbage"));
    // simulate a browser request
    List<Header> headers = new ArrayList<Header>();
    headers.add(new Header("User-Agent", "Mozilla/5.0 Sling Integration Test"));
    HttpMethod post = assertPostStatus(HttpTest.HTTP_BASE_URL + "/j_security_check", HttpServletResponse.SC_MOVED_TEMPORARILY, params, headers, null);
    final String location = post.getResponseHeader("Location").getValue();
    assertNotNull(location);
    assertTrue(location.startsWith(HttpTest.HTTP_BASE_URL + "/system/sling/selector/login?"));
    assertTrue(location.contains("resource=%2F"));
    assertTrue(location.contains("j_reason=INVALID_CREDENTIALS"));
}
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)

Example 82 with Header

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

the class AuthenticatedTestUtil method getAuthenticatedPostContent.

/** retrieve the contents of given URL and assert its content type
     * @param expectedContentType use CONTENT_TYPE_DONTCARE if must not be checked
     * @throws IOException
     * @throws HttpException */
public String getAuthenticatedPostContent(Credentials creds, String url, String expectedContentType, List<NameValuePair> postParams, int expectedStatusCode) throws IOException {
    final PostMethod post = new PostMethod(url);
    URL baseUrl = new URL(HTTP_BASE_URL);
    AuthScope authScope = new AuthScope(baseUrl.getHost(), baseUrl.getPort(), AuthScope.ANY_REALM);
    post.setDoAuthentication(true);
    Credentials oldCredentials = httpClient.getState().getCredentials(authScope);
    try {
        httpClient.getState().setCredentials(authScope, creds);
        if (postParams != null) {
            final NameValuePair[] nvp = {};
            post.setRequestBody(postParams.toArray(nvp));
        }
        final int status = httpClient.executeMethod(post);
        final InputStream is = post.getResponseBodyAsStream();
        final StringBuffer content = new StringBuffer();
        final String charset = post.getResponseCharSet();
        final byte[] buffer = new byte[16384];
        int n = 0;
        while ((n = is.read(buffer, 0, buffer.length)) > 0) {
            content.append(new String(buffer, 0, n, charset));
        }
        assertEquals("Expected status " + expectedStatusCode + " for " + url + " (content=" + content + ")", expectedStatusCode, status);
        final Header h = post.getResponseHeader("Content-Type");
        if (expectedContentType == null) {
            if (h != null) {
                fail("Expected null Content-Type, got " + h.getValue());
            }
        } else if (CONTENT_TYPE_DONTCARE.equals(expectedContentType)) {
        // no check
        } else if (h == null) {
            fail("Expected Content-Type that starts with '" + expectedContentType + " but got no Content-Type header at " + url);
        } else {
            assertTrue("Expected Content-Type that starts with '" + expectedContentType + "' for " + url + ", got '" + h.getValue() + "'", h.getValue().startsWith(expectedContentType));
        }
        return content.toString();
    } finally {
        httpClient.getState().setCredentials(authScope, oldCredentials);
    }
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) Header(org.apache.commons.httpclient.Header) PostMethod(org.apache.commons.httpclient.methods.PostMethod) InputStream(java.io.InputStream) AuthScope(org.apache.commons.httpclient.auth.AuthScope) URL(java.net.URL) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials)

Example 83 with Header

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

the class AuthenticatedTestUtil method getAuthenticatedContent.

/** retrieve the contents of given URL and assert its content type
     * @param expectedContentType use CONTENT_TYPE_DONTCARE if must not be checked
     * @throws IOException
     * @throws HttpException */
public String getAuthenticatedContent(Credentials creds, String url, String expectedContentType, List<NameValuePair> params, int expectedStatusCode) throws IOException {
    final GetMethod get = new GetMethod(url);
    URL baseUrl = new URL(HTTP_BASE_URL);
    AuthScope authScope = new AuthScope(baseUrl.getHost(), baseUrl.getPort(), AuthScope.ANY_REALM);
    get.setDoAuthentication(true);
    Credentials oldCredentials = httpClient.getState().getCredentials(authScope);
    try {
        httpClient.getState().setCredentials(authScope, creds);
        if (params != null) {
            final NameValuePair[] nvp = new NameValuePair[0];
            get.setQueryString(params.toArray(nvp));
        }
        final int status = httpClient.executeMethod(get);
        final InputStream is = get.getResponseBodyAsStream();
        final StringBuffer content = new StringBuffer();
        final String charset = get.getResponseCharSet();
        final byte[] buffer = new byte[16384];
        int n = 0;
        while ((n = is.read(buffer, 0, buffer.length)) > 0) {
            content.append(new String(buffer, 0, n, charset));
        }
        assertEquals("Expected status " + expectedStatusCode + " for " + url + " (content=" + content + ")", expectedStatusCode, status);
        final Header h = get.getResponseHeader("Content-Type");
        if (expectedContentType == null) {
            if (h != null) {
                fail("Expected null Content-Type, got " + h.getValue());
            }
        } else if (CONTENT_TYPE_DONTCARE.equals(expectedContentType)) {
        // no check
        } else if (h == null) {
            fail("Expected Content-Type that starts with '" + expectedContentType + " but got no Content-Type header at " + url);
        } else {
            assertTrue("Expected Content-Type that starts with '" + expectedContentType + "' for " + url + ", got '" + h.getValue() + "'", h.getValue().startsWith(expectedContentType));
        }
        return content.toString();
    } finally {
        httpClient.getState().setCredentials(authScope, oldCredentials);
    }
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) Header(org.apache.commons.httpclient.Header) InputStream(java.io.InputStream) GetMethod(org.apache.commons.httpclient.methods.GetMethod) AuthScope(org.apache.commons.httpclient.auth.AuthScope) URL(java.net.URL) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials)

Example 84 with Header

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

the class WebdavOptionsTest method doTestOnPath.

/** OPTIONS request at given path must return a WWW-Authenticate header */
private void doTestOnPath(String path) throws Exception {
    final HttpClient noCredentialsClient = new HttpClient();
    final HttpAnyMethod opt = new HttpAnyMethod("OPTIONS", HTTP_BASE_URL + path);
    final int status = noCredentialsClient.executeMethod(opt);
    assertEquals("Expecting matching status on OPTIONS request at " + path, 401, status);
    final Header h = opt.getResponseHeader(WWW_Authenticate);
    assertNotNull("Expecting " + WWW_Authenticate + " header in response at " + path, h);
}
Also used : Header(org.apache.commons.httpclient.Header) HttpClient(org.apache.commons.httpclient.HttpClient) HttpAnyMethod(org.apache.sling.commons.testing.integration.HttpAnyMethod)

Example 85 with Header

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

the class AuthenticationResponseCodeTest method assertXReason.

private void assertXReason(final HttpMethod method) throws IOException {
    // expected the X-Reason header
    final Header reason = method.getResponseHeader("X-Reason");
    assertNotNull(reason);
    // expect the response to be the same as the reason (SLING-1831)
    assertEquals(reason.getValue(), method.getResponseBodyAsString().trim());
}
Also used : Header(org.apache.commons.httpclient.Header)

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