Search in sources :

Example 1 with TestHttpClient

use of testutils.TestHttpClient in project runwar by cfmlprojects.

the class BasicAuthTest method testChallengeSent.

@Test
public void testChallengeSent() throws Exception {
    TestHttpClient client = new TestHttpClient();
    String url = DefaultServer.getDefaultServerURL() + "/dumprunwarrequest";
    HttpGet get = new HttpGet(url);
    HttpResponse result = client.execute(get);
    HttpClientUtils.readResponse(result);
    assertEquals(StatusCodes.UNAUTHORIZED, result.getStatusLine().getStatusCode());
    Header[] values = result.getHeaders(WWW_AUTHENTICATE.toString());
    assertEquals(1, values.length);
    String value = values[0].getValue();
    assertTrue(value.startsWith("Basic"));
}
Also used : Header(org.apache.http.Header) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(testutils.TestHttpClient) Test(org.junit.jupiter.api.Test)

Example 2 with TestHttpClient

use of testutils.TestHttpClient in project runwar by cfmlprojects.

the class HTTP2Test method http2testProxiedRequest.

@Test
public void http2testProxiedRequest() throws IOException {
    final TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/dumprunwarrequest");
        client.setRedirectStrategy(new LaxRedirectStrategy());
        HttpResponse result = client.execute(get);
        assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        JSONObject responseData = (JSONObject) JSONValue.parse(HttpClientUtils.readResponse(result));
        assertTrue(Boolean.parseBoolean(responseData.get("isHTTP2").toString()), get.getURI().toString() + " failed to be http2");
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : JSONObject(net.minidev.json.JSONObject) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) LaxRedirectStrategy(org.apache.http.impl.client.LaxRedirectStrategy) TestHttpClient(testutils.TestHttpClient) Test(org.junit.jupiter.api.Test)

Example 3 with TestHttpClient

use of testutils.TestHttpClient in project runwar by cfmlprojects.

the class ProxyPeerAddressTest method testForwardForHostPort.

@Test
public void testForwardForHostPort() throws IOException {
    String forwardFor = "localhost";
    String forwardPort = "8765";
    assertTrue(DefaultServer.getServerOptions().isProxyPeerAddressEnabled());
    final TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/dumprunwarrequest");
        get.addHeader(Headers.X_FORWARDED_FOR_STRING, forwardFor);
        get.addHeader(Headers.X_FORWARDED_HOST_STRING, forwardFor);
        get.addHeader(Headers.X_FORWARDED_PORT_STRING, forwardPort);
        HttpResponse result = client.execute(get);
        assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        JSONObject responseData = (JSONObject) JSONValue.parse(HttpClientUtils.readResponse(result));
        assertEquals(forwardFor + ":" + forwardPort, responseData.get("host"));
        assertEquals(forwardFor, responseData.get("remoteHost"));
        assertEquals(forwardFor, ((JSONObject) responseData.get("headers")).get(Headers.X_FORWARDED_FOR_STRING));
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : JSONObject(net.minidev.json.JSONObject) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(testutils.TestHttpClient) Test(org.junit.jupiter.api.Test)

Example 4 with TestHttpClient

use of testutils.TestHttpClient in project runwar by cfmlprojects.

the class BasicAuthTest method testCall.

public void testCall(final String path, final String expectedResponse, Charset charset, String userAgent, String user, String password, int expect) throws Exception {
    TestHttpClient client = new TestHttpClient();
    try {
        String url = DefaultServer.getDefaultServerURL() + "/dumprunwarrequest?userpath=" + path;
        HttpGet get = new HttpGet(url);
        get = new HttpGet(url);
        get.addHeader(Headers.USER_AGENT_STRING, userAgent);
        get.addHeader(AUTHORIZATION.toString(), BASIC + " " + FlexBase64.encodeString((user + ":" + password).getBytes(charset), false));
        HttpResponse result = client.execute(get);
        assertEquals(expect, result.getStatusLine().getStatusCode());
        final String response = HttpClientUtils.readResponse(result);
        System.out.println(response);
        if (expect == 200) {
            JSONObject responseData = (JSONObject) JSONValue.parse(response);
            String authType = responseData.get(path) != null ? responseData.get(path).toString() : "";
            String authHeader = ((JSONObject) responseData.get("headers")).get(Headers.AUTHORIZATION_STRING).toString();
            assertEquals(expectedResponse, authType);
        }
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : JSONObject(net.minidev.json.JSONObject) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(testutils.TestHttpClient)

Example 5 with TestHttpClient

use of testutils.TestHttpClient in project runwar by cfmlprojects.

the class HTTP2Test method http2testRequest.

@Test
public void http2testRequest() throws IOException {
    final TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerHTTP2Address() + "/dumprunwarrequest");
        HttpResponse result = client.execute(get);
        assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        JSONObject responseData = (JSONObject) JSONValue.parse(HttpClientUtils.readResponse(result));
        assertTrue(Boolean.parseBoolean(responseData.get("isHTTP2").toString()));
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : JSONObject(net.minidev.json.JSONObject) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(testutils.TestHttpClient) Test(org.junit.jupiter.api.Test)

Aggregations

HttpResponse (org.apache.http.HttpResponse)6 HttpGet (org.apache.http.client.methods.HttpGet)6 TestHttpClient (testutils.TestHttpClient)6 JSONObject (net.minidev.json.JSONObject)5 Test (org.junit.jupiter.api.Test)5 Header (org.apache.http.Header)1 LaxRedirectStrategy (org.apache.http.impl.client.LaxRedirectStrategy)1