Search in sources :

Example 51 with StatusLine

use of org.apache.http.StatusLine in project wildfly by wildfly.

the class AbstractWebSecurityFORMTestCase method makeCall.

// Protected methods -----------------------------------------------------
/**
     * Makes a HTTP request to the protected web application.
     *
     * @param user
     * @param pass
     * @param expectedStatusCode
     * @throws Exception
     * @see WebSecurityPasswordBasedBase#makeCall(java.lang.String, java.lang.String,
     * int)
     */
@Override
protected void makeCall(String user, String pass, int expectedStatusCode) throws Exception {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {
        String req = url.toExternalForm() + "secured/";
        HttpGet httpget = new HttpGet(req);
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            EntityUtils.consume(entity);
        }
        // We should get the Login Page
        StatusLine statusLine = response.getStatusLine();
        LOGGER.trace("Login form get: " + statusLine);
        assertEquals(200, statusLine.getStatusCode());
        LOGGER.trace("Initial set of cookies:");
        List<Cookie> cookies = httpclient.getCookieStore().getCookies();
        if (cookies.isEmpty()) {
            LOGGER.trace("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                LOGGER.trace("- " + cookies.get(i).toString());
            }
        }
        req = url.toExternalForm() + "secured/j_security_check";
        // We should now login with the user name and password
        HttpPost httpPost = new HttpPost(req);
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("j_username", user));
        nvps.add(new BasicNameValuePair("j_password", pass));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, StandardCharsets.UTF_8));
        response = httpclient.execute(httpPost);
        entity = response.getEntity();
        if (entity != null) {
            EntityUtils.consume(entity);
        }
        statusLine = response.getStatusLine();
        // Post authentication - we have a 302
        assertEquals(302, statusLine.getStatusCode());
        Header locationHeader = response.getFirstHeader("Location");
        String location = locationHeader.getValue();
        HttpGet httpGet = new HttpGet(location);
        response = httpclient.execute(httpGet);
        entity = response.getEntity();
        if (entity != null) {
            EntityUtils.consume(entity);
        }
        LOGGER.trace("Post logon cookies:");
        cookies = httpclient.getCookieStore().getCookies();
        if (cookies.isEmpty()) {
            LOGGER.trace("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                LOGGER.trace("- " + cookies.get(i).toString());
            }
        }
        // Either the authentication passed or failed based on the expected status code
        statusLine = response.getStatusLine();
        assertEquals(expectedStatusCode, statusLine.getStatusCode());
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }
}
Also used : Cookie(org.apache.http.cookie.Cookie) HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) StatusLine(org.apache.http.StatusLine) Header(org.apache.http.Header) BasicNameValuePair(org.apache.http.message.BasicNameValuePair)

Example 52 with StatusLine

use of org.apache.http.StatusLine in project wildfly by wildfly.

the class WebModuleDeploymentTestCase method testSimpleBeanInjected.

@Test
public void testSimpleBeanInjected() throws Exception {
    try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
        HttpGet httpget = new HttpGet(url.toExternalForm() + "/servlet");
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        StatusLine statusLine = response.getStatusLine();
        assertEquals(200, statusLine.getStatusCode());
        String result = EntityUtils.toString(entity);
        Assert.assertEquals(ModuleServlet.MODULE_SERVLET, result);
    }
}
Also used : StatusLine(org.apache.http.StatusLine) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test)

Example 53 with StatusLine

use of org.apache.http.StatusLine in project wildfly by wildfly.

the class DefaultServletMultipartConfigTestCase method testMultipartRequestToDefaultServlet.

@Test
public void testMultipartRequestToDefaultServlet() throws Exception {
    try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
        HttpPost post = new HttpPost(url.toExternalForm() + "/servlet");
        post.setEntity(MultipartEntityBuilder.create().addTextBody("file", MESSAGE).build());
        HttpResponse response = httpClient.execute(post);
        HttpEntity entity = response.getEntity();
        StatusLine statusLine = response.getStatusLine();
        assertEquals(200, statusLine.getStatusCode());
        String result = EntityUtils.toString(entity);
        Assert.assertEquals(MESSAGE, result);
    }
}
Also used : StatusLine(org.apache.http.StatusLine) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test)

Example 54 with StatusLine

use of org.apache.http.StatusLine in project wildfly by wildfly.

the class WebSecurityBASICTestCase method makeCall.

@Override
protected void makeCall(String user, String pass, int expectedStatusCode) throws Exception {
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(url.getHost(), url.getPort()), new UsernamePasswordCredentials(user, pass));
    try (CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build()) {
        HttpGet httpget = new HttpGet(url.toExternalForm() + "secured/");
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        StatusLine statusLine = response.getStatusLine();
        if (entity != null) {
            log.trace("Response content length: " + entity.getContentLength());
        }
        assertEquals(expectedStatusCode, statusLine.getStatusCode());
        EntityUtils.consume(entity);
    }
}
Also used : StatusLine(org.apache.http.StatusLine) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) AuthScope(org.apache.http.auth.AuthScope) HttpResponse(org.apache.http.HttpResponse) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 55 with StatusLine

use of org.apache.http.StatusLine in project wildfly by wildfly.

the class UndertowNonBlockingHandlerTestCase method testNonBlockingHandler.

@Test
public void testNonBlockingHandler() throws Exception {
    try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
        HttpGet httpget = new HttpGet(url.toExternalForm());
        HttpResponse response = httpClient.execute(httpget);
        HttpEntity entity = response.getEntity();
        StatusLine statusLine = response.getStatusLine();
        assertEquals(200, statusLine.getStatusCode());
        String result = EntityUtils.toString(entity);
        Assert.assertEquals(SimpleUndertowExtension.THIS_IS_NOT_A_SERVLET, result);
    }
}
Also used : StatusLine(org.apache.http.StatusLine) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test)

Aggregations

StatusLine (org.apache.http.StatusLine)193 IOException (java.io.IOException)83 HttpResponse (org.apache.http.HttpResponse)76 HttpEntity (org.apache.http.HttpEntity)61 BasicStatusLine (org.apache.http.message.BasicStatusLine)46 ProtocolVersion (org.apache.http.ProtocolVersion)42 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)40 HttpGet (org.apache.http.client.methods.HttpGet)38 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)37 Header (org.apache.http.Header)36 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)34 Test (org.junit.Test)31 HttpPost (org.apache.http.client.methods.HttpPost)26 HashMap (java.util.HashMap)23 HttpResponseException (org.apache.http.client.HttpResponseException)23 StringEntity (org.apache.http.entity.StringEntity)23 URL (java.net.URL)20 BasicHeader (org.apache.http.message.BasicHeader)16 HttpURLConnection (java.net.HttpURLConnection)15 List (java.util.List)15