Search in sources :

Example 46 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project undertow by undertow-io.

the class ParameterEchoTestCase method testPostBoth.

@Test
public void testPostBoth() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
        HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/servletContext/aaa?param1=1&param2=2");
        final List<NameValuePair> values = new ArrayList<>();
        values.add(new BasicNameValuePair("param3", "3"));
        UrlEncodedFormEntity data = new UrlEncodedFormEntity(values, "UTF-8");
        post.setEntity(data);
        HttpResponse result = client.execute(post);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        final String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals(RESPONSE, response);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 47 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project undertow by undertow-io.

the class ParameterEchoTestCase method testPostInUrl.

@Test
public void testPostInUrl() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
        HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/servletContext/aaa?param1=1&param2=2&param3=3");
        final List<NameValuePair> values = new ArrayList<>();
        UrlEncodedFormEntity data = new UrlEncodedFormEntity(values, "UTF-8");
        post.setEntity(data);
        HttpResponse result = client.execute(post);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        final String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals(RESPONSE, response);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 48 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project undertow by undertow-io.

the class ParameterCharacterEncodingTestCase method testFormDataCharacterEncoding.

@Test
public void testFormDataCharacterEncoding() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
        String message = "abcčšž";
        String charset = "UTF-8";
        HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/servletContext");
        final List<NameValuePair> values = new ArrayList<>();
        values.add(new BasicNameValuePair("charset", charset));
        values.add(new BasicNameValuePair("message", message));
        UrlEncodedFormEntity data = new UrlEncodedFormEntity(values, "UTF-8");
        post.setEntity(data);
        HttpResponse result = client.execute(post);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals(message, response);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 49 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project undertow by undertow-io.

the class FormDataParserTestCase method runTest.

private void runTest(final NameValuePair... pairs) throws Exception {
    DefaultServer.setRootHandler(rootHandler);
    TestHttpClient client = new TestHttpClient();
    try {
        final List<NameValuePair> data = new ArrayList<>();
        data.addAll(Arrays.asList(pairs));
        HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/path");
        post.setHeader(Headers.CONTENT_TYPE_STRING, FormEncodedDataDefinition.APPLICATION_X_WWW_FORM_URLENCODED);
        post.setEntity(new UrlEncodedFormEntity(data));
        HttpResponse result = client.execute(post);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        checkResult(data, result);
        HttpClientUtils.readResponse(result);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpPost(org.apache.http.client.methods.HttpPost) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) TestHttpClient(io.undertow.testutils.TestHttpClient)

Example 50 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project undertow by undertow-io.

the class ServletFormAuthTestCase method testServletFormAuthWithOriginalRequestParams.

@Test
public void testServletFormAuthWithOriginalRequestParams() throws IOException {
    TestHttpClient client = new TestHttpClient();
    client.setRedirectStrategy(new DefaultRedirectStrategy() {

        @Override
        public boolean isRedirected(final HttpRequest request, final HttpResponse response, final HttpContext context) throws ProtocolException {
            if (response.getStatusLine().getStatusCode() == StatusCodes.FOUND) {
                return true;
            }
            return super.isRedirected(request, response, context);
        }
    });
    try {
        final String uri = DefaultServer.getDefaultServerURL() + "/servletContext/secured/echoParam?param=developer";
        HttpPost post = new HttpPost(uri);
        post.setEntity(new StringEntity("String Entity"));
        HttpResponse result = client.execute(post);
        assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        String response = HttpClientUtils.readResponse(result);
        Assert.assertTrue(response.startsWith("j_security_check"));
        BasicNameValuePair[] pairs = new BasicNameValuePair[] { new BasicNameValuePair("j_username", "user1"), new BasicNameValuePair("j_password", "password1") };
        final List<NameValuePair> data = new ArrayList<>();
        data.addAll(Arrays.asList(pairs));
        post = new HttpPost(DefaultServer.getDefaultServerURL() + "/servletContext/j_security_check");
        post.setEntity(new UrlEncodedFormEntity(data));
        result = client.execute(post);
        assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        response = HttpClientUtils.readResponse(result);
        assertEquals("developer", response);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpRequest(org.apache.http.HttpRequest) ProtocolException(org.apache.http.ProtocolException) HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpContext(org.apache.http.protocol.HttpContext) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) TestHttpClient(io.undertow.testutils.TestHttpClient) StringEntity(org.apache.http.entity.StringEntity) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) DefaultRedirectStrategy(org.apache.http.impl.client.DefaultRedirectStrategy) Test(org.junit.Test)

Aggregations

UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)134 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)111 HttpPost (org.apache.http.client.methods.HttpPost)105 NameValuePair (org.apache.http.NameValuePair)100 ArrayList (java.util.ArrayList)96 HttpResponse (org.apache.http.HttpResponse)74 IOException (java.io.IOException)47 HttpEntity (org.apache.http.HttpEntity)40 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)31 ClientProtocolException (org.apache.http.client.ClientProtocolException)27 UnsupportedEncodingException (java.io.UnsupportedEncodingException)26 HttpClient (org.apache.http.client.HttpClient)20 Test (org.junit.Test)20 HttpGet (org.apache.http.client.methods.HttpGet)19 Map (java.util.Map)18 JSONObject (org.json.JSONObject)18 TestHttpClient (io.undertow.testutils.TestHttpClient)14 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)14 HashMap (java.util.HashMap)13 Header (org.apache.http.Header)13