Search in sources :

Example 86 with HttpResponse

use of org.apache.http.HttpResponse in project opennms by OpenNMS.

the class JUnitHttpServerTest method testBasicAuthSuccess.

@Test
@JUnitHttpServer(port = 9162, basicAuth = true, webapps = { @Webapp(context = "/testContext", path = "src/test/resources/test-webapp") })
public void testBasicAuthSuccess() throws Exception {
    final HttpUriRequest method = new HttpGet("http://localhost:9162/testContext/monkey");
    m_clientWrapper.addBasicCredentials("admin", "istrator");
    final HttpResponse response = m_clientWrapper.execute(method);
    final String responseString = EntityUtils.toString(response.getEntity());
    LOG.debug("got response:\n{}", responseString);
    assertEquals(200, response.getStatusLine().getStatusCode());
    assertTrue(responseString.contains("You are reading this from a servlet!"));
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test) JUnitHttpServer(org.opennms.core.test.http.annotations.JUnitHttpServer)

Example 87 with HttpResponse

use of org.apache.http.HttpResponse in project opennms by OpenNMS.

the class NCSNorthbounderIT method testTestServlet.

@Test
@JUnitHttpServer(port = 10342, https = false, webapps = { @Webapp(context = "/fmpm", path = "src/test/resources/test-webapp") })
public void testTestServlet() throws Exception {
    TestServlet.reset();
    CloseableHttpClient client = HttpClientBuilder.create().build();
    try {
        HttpEntity entity = new StringEntity(xml);
        HttpPost method = new HttpPost("http://localhost:10342/fmpm/restful/NotificationMessageRelay");
        method.setEntity(entity);
        HttpResponse response = client.execute(method);
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals(xml, TestServlet.getPosted());
    } finally {
        IOUtils.closeQuietly(client);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) StringEntity(org.apache.http.entity.StringEntity) HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test) XmlTest(org.opennms.core.test.xml.XmlTest) JUnitHttpServer(org.opennms.core.test.http.annotations.JUnitHttpServer)

Example 88 with HttpResponse

use of org.apache.http.HttpResponse in project android_frameworks_base by DirtyUnicorns.

the class AbstractProxyTest method testParamPreferredOverSystemProperty.

private void testParamPreferredOverSystemProperty(ProxyConfig proxyConfig) throws Exception {
    server.enqueue(new MockResponse().setBody("Via request parameter proxy!"));
    server.play();
    System.setProperty("http.proxyHost", "proxy.foo");
    System.setProperty("http.proxyPort", "8080");
    HttpClient client = newHttpClient();
    HttpGet request = new HttpGet("http://origin.foo/bar");
    proxyConfig.configure(server, client, request);
    HttpResponse response = client.execute(request);
    assertEquals("Via request parameter proxy!", contentToString(response));
    RecordedRequest recordedRequest = server.takeRequest();
    assertEquals("GET http://origin.foo/bar HTTP/1.1", recordedRequest.getRequestLine());
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse)

Example 89 with HttpResponse

use of org.apache.http.HttpResponse in project android_frameworks_base by DirtyUnicorns.

the class CookiesTest method testCookiesWithNonMatchingCase.

/**
     * Test that cookies aren't case-sensitive with respect to hostname.
     * http://b/3167208
     */
public void testCookiesWithNonMatchingCase() throws Exception {
    // use a proxy so we can manipulate the origin server's host name
    server = new MockWebServer();
    server.enqueue(new MockResponse().addHeader("Set-Cookie: a=first; Domain=my.t-mobile.com").addHeader("Set-Cookie: b=second; Domain=.T-mobile.com").addHeader("Set-Cookie: c=third; Domain=.t-mobile.com").setBody("This response sets some cookies."));
    server.enqueue(new MockResponse().setBody("This response gets those cookies back."));
    server.play();
    HttpClient client = new DefaultHttpClient();
    client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost("localhost", server.getPort()));
    HttpResponse getCookies = client.execute(new HttpGet("http://my.t-mobile.com/"));
    getCookies.getEntity().consumeContent();
    server.takeRequest();
    HttpResponse sendCookies = client.execute(new HttpGet("http://my.t-mobile.com/"));
    sendCookies.getEntity().consumeContent();
    RecordedRequest sendCookiesRequest = server.takeRequest();
    assertContains(sendCookiesRequest.getHeaders(), "Cookie: a=first; b=second; c=third");
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) HttpHost(org.apache.http.HttpHost) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) MockWebServer(com.google.mockwebserver.MockWebServer) HttpResponse(org.apache.http.HttpResponse) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 90 with HttpResponse

use of org.apache.http.HttpResponse in project android_frameworks_base by ParanoidAndroid.

the class BandwidthTestUtil method postFileToServer.

/**
     * Post a given file for a given device and timestamp to the server.
     * @param server {@link String} url of test server
     * @param deviceId {@link String} device id that is uploading
     * @param timestamp {@link String} timestamp
     * @param file {@link File} to upload
     * @return true if it succeeded
     */
public static boolean postFileToServer(String server, String deviceId, String timestamp, File file) {
    try {
        Log.d(LOG_TAG, "Uploading begining");
        HttpClient httpClient = new DefaultHttpClient();
        String uri = server;
        if (!uri.endsWith("/")) {
            uri += "/";
        }
        uri += "upload";
        Log.d(LOG_TAG, "Upload url:" + uri);
        HttpPost postRequest = new HttpPost(uri);
        Part[] parts = { new StringPart("device_id", deviceId), new StringPart("timestamp", timestamp), new FilePart("file", file) };
        MultipartEntity reqEntity = new MultipartEntity(parts, postRequest.getParams());
        postRequest.setEntity(reqEntity);
        HttpResponse res = httpClient.execute(postRequest);
        res.getEntity().getContent().close();
    } catch (IOException e) {
        Log.e(LOG_TAG, "Could not upload file with error: " + e);
        return false;
    }
    return true;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) MultipartEntity(com.android.internal.http.multipart.MultipartEntity) Part(com.android.internal.http.multipart.Part) FilePart(com.android.internal.http.multipart.FilePart) StringPart(com.android.internal.http.multipart.StringPart) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) StringPart(com.android.internal.http.multipart.StringPart) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) FilePart(com.android.internal.http.multipart.FilePart)

Aggregations

HttpResponse (org.apache.http.HttpResponse)4168 Test (org.junit.Test)2110 HttpGet (org.apache.http.client.methods.HttpGet)1770 IOException (java.io.IOException)1000 URI (java.net.URI)817 HttpPost (org.apache.http.client.methods.HttpPost)699 HttpClient (org.apache.http.client.HttpClient)550 HttpEntity (org.apache.http.HttpEntity)496 TestHttpClient (io.undertow.testutils.TestHttpClient)403 InputStream (java.io.InputStream)373 Header (org.apache.http.Header)370 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)333 StringEntity (org.apache.http.entity.StringEntity)332 HttpPut (org.apache.http.client.methods.HttpPut)310 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)307 ArrayList (java.util.ArrayList)296 Identity (org.olat.core.id.Identity)262 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)237 File (java.io.File)192 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)191