Search in sources :

Example 11 with HttpResponse

use of com.netflix.client.http.HttpResponse in project ribbon by Netflix.

the class RestClientTest method testExecuteWithoutLB.

@Test
public void testExecuteWithoutLB() throws Exception {
    RestClient client = (RestClient) ClientFactory.getNamedClient("google");
    HttpRequest request = HttpRequest.newBuilder().uri(server.getServerURI()).build();
    HttpResponse response = client.executeWithLoadBalancer(request);
    assertStatusIsOk(response.getStatus());
    response = client.execute(request);
    assertStatusIsOk(response.getStatus());
}
Also used : HttpRequest(com.netflix.client.http.HttpRequest) HttpResponse(com.netflix.client.http.HttpResponse) Test(org.junit.Test)

Example 12 with HttpResponse

use of com.netflix.client.http.HttpResponse in project zuul by Netflix.

the class RibbonCommand method forward.

HttpResponse forward() throws Exception {
    NFRequestContext context = NFRequestContext.getCurrentContext();
    HttpRequest.Builder builder = HttpRequest.newBuilder().verb(verb).uri(uri).entity(requestEntity);
    for (String name : headers.keySet()) {
        List<String> values = headers.get(name);
        for (String value : values) {
            builder.header(name, value);
        }
    }
    for (String name : params.keySet()) {
        List<String> values = params.get(name);
        for (String value : values) {
            builder.queryParams(name, value);
        }
    }
    HttpRequest httpClientRequest = builder.build();
    HttpResponse response = restClient.executeWithLoadBalancer(httpClientRequest);
    context.setZuulResponse(response);
    // chain has already continued without us and therefore won't cleanup itself.
    if (isResponseTimedOut()) {
        response.close();
    }
    return response;
}
Also used : HttpRequest(com.netflix.client.http.HttpRequest) NFRequestContext(com.netflix.zuul.context.NFRequestContext) HttpResponse(com.netflix.client.http.HttpResponse)

Example 13 with HttpResponse

use of com.netflix.client.http.HttpResponse in project ribbon by Netflix.

the class ManyShortLivedRequestsSurvivorTest method survive.

@Test
public void survive() throws IOException, ClientException, URISyntaxException, InterruptedException {
    String clientName = "RibbonClientTest-loadBalancingDefaultPolicyRoundRobin";
    String serverListKey = clientName + ".ribbon.listOfServers";
    int nbHitsPerServer = 60;
    MockWebServer server1 = new MockWebServer();
    MockWebServer server2 = new MockWebServer();
    for (int i = 0; i < nbHitsPerServer; i++) {
        server1.enqueue(new MockResponse().setResponseCode(200).setBody("server1 success <" + i + ">!"));
        server2.enqueue(new MockResponse().setResponseCode(200).setBody("server2 success <" + i + ">!"));
    }
    server1.play();
    server2.play();
    getConfigInstance().setProperty(serverListKey, hostAndPort(server1.getUrl("")) + "," + hostAndPort(server2.getUrl("")));
    RestClient client = (RestClient) ClientFactory.getNamedClient(clientName);
    HttpRequest request;
    for (int i = 0; i < nbHitsPerServer * 2; i++) {
        request = HttpRequest.newBuilder().uri(new URI("/")).build();
        HttpResponse response = client.executeWithLoadBalancer(request);
        response.close();
    }
}
Also used : HttpRequest(com.netflix.client.http.HttpRequest) MockResponse(com.google.mockwebserver.MockResponse) MockWebServer(com.google.mockwebserver.MockWebServer) RestClient(com.netflix.niws.client.http.RestClient) HttpResponse(com.netflix.client.http.HttpResponse) URI(java.net.URI) Test(org.junit.Test)

Example 14 with HttpResponse

use of com.netflix.client.http.HttpResponse in project ribbon by Netflix.

the class FollowRedirectTest method testRedirectNotFollowed.

@Test
public void testRedirectNotFollowed() throws Exception {
    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues("myclient");
    config.setProperty(CommonClientConfigKey.FollowRedirects, Boolean.FALSE);
    ClientFactory.registerClientFromProperties("myclient", config);
    RestClient client = (RestClient) ClientFactory.getNamedClient("myclient");
    HttpRequest request = HttpRequest.newBuilder().uri(new URI("http://localhost:" + redirectingServer.getPort())).build();
    HttpResponse response = client.executeWithLoadBalancer(request);
    assertEquals(302, response.getStatus());
}
Also used : HttpRequest(com.netflix.client.http.HttpRequest) IClientConfig(com.netflix.client.config.IClientConfig) HttpResponse(com.netflix.client.http.HttpResponse) URI(java.net.URI) Test(org.junit.Test)

Example 15 with HttpResponse

use of com.netflix.client.http.HttpResponse in project ribbon by Netflix.

the class RestClientTest method testExecuteWithLB.

@Test
public void testExecuteWithLB() throws Exception {
    ConfigurationManager.getConfigInstance().setProperty("allservices.ribbon." + CommonClientConfigKey.ReadTimeout, "10000");
    ConfigurationManager.getConfigInstance().setProperty("allservices.ribbon." + CommonClientConfigKey.FollowRedirects, "true");
    RestClient client = (RestClient) ClientFactory.getNamedClient("allservices");
    BaseLoadBalancer lb = new BaseLoadBalancer();
    Server[] servers = new Server[] { new Server("localhost", server.getServerPort()) };
    lb.addServers(Arrays.asList(servers));
    client.setLoadBalancer(lb);
    Set<URI> expected = new HashSet<URI>();
    expected.add(new URI(server.getServerPath("/")));
    Set<URI> result = new HashSet<URI>();
    HttpRequest request = HttpRequest.newBuilder().uri(new URI("/")).build();
    for (int i = 0; i < 5; i++) {
        HttpResponse response = client.executeWithLoadBalancer(request);
        assertStatusIsOk(response.getStatus());
        assertTrue(response.isSuccess());
        String content = response.getEntity(String.class);
        response.close();
        assertFalse(content.isEmpty());
        result.add(response.getRequestedURI());
    }
    assertEquals(expected, result);
    request = HttpRequest.newBuilder().uri(server.getServerURI()).build();
    HttpResponse response = client.executeWithLoadBalancer(request);
    assertEquals(200, response.getStatus());
}
Also used : HttpRequest(com.netflix.client.http.HttpRequest) Server(com.netflix.loadbalancer.Server) MockHttpServer(com.netflix.client.testutil.MockHttpServer) HttpResponse(com.netflix.client.http.HttpResponse) BaseLoadBalancer(com.netflix.loadbalancer.BaseLoadBalancer) URI(java.net.URI) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

HttpRequest (com.netflix.client.http.HttpRequest)20 HttpResponse (com.netflix.client.http.HttpResponse)20 URI (java.net.URI)16 Test (org.junit.Test)16 MockHttpServer (com.netflix.client.testutil.MockHttpServer)3 Server (com.netflix.loadbalancer.Server)3 RestClient (com.netflix.niws.client.http.RestClient)3 AbstractConfiguration (org.apache.commons.configuration.AbstractConfiguration)3 ClientException (com.netflix.client.ClientException)2 IClientConfig (com.netflix.client.config.IClientConfig)2 BaseLoadBalancer (com.netflix.loadbalancer.BaseLoadBalancer)2 ServerStats (com.netflix.loadbalancer.ServerStats)2 ZoneAwareLoadBalancer (com.netflix.loadbalancer.ZoneAwareLoadBalancer)2 MockResponse (com.google.mockwebserver.MockResponse)1 MockWebServer (com.google.mockwebserver.MockWebServer)1 NFRequestContext (com.netflix.zuul.context.NFRequestContext)1 MultivaluedMapImpl (com.sun.jersey.core.util.MultivaluedMapImpl)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 HashSet (java.util.HashSet)1