@Test public void asyncStream302RedirectWithBody() throws Throwable { withClient(config().setFollowRedirect(true)) .run( client -> { withServer(server) .run( server -> { String originalUrl = server.getHttpUrl() + "/original"; String redirectUrl = server.getHttpUrl() + "/redirect"; server.enqueueResponse( response -> { response.setStatus(302); response.setHeader(LOCATION.toString(), redirectUrl); response .getOutputStream() .println("You are being asked to redirect to " + redirectUrl); }); server.enqueueOk(); Response response = client.prepareGet(originalUrl).execute().get(20, TimeUnit.SECONDS); assertEquals(response.getStatusCode(), 200); assertTrue(response.getResponseBody().isEmpty()); }); }); }
@Test public void closeConnectionTest() throws Throwable { withClient() .run( client -> { withServer(server) .run( server -> { server.enqueueEcho(); Response r = client .prepareGet(getTargetUrl()) .execute( new AsyncHandler<Response>() { private Response.ResponseBuilder builder = new Response.ResponseBuilder(); public State onHeadersReceived(HttpResponseHeaders content) throws Exception { builder.accumulate(content); return State.CONTINUE; } public void onThrowable(Throwable t) {} public State onBodyPartReceived(HttpResponseBodyPart content) throws Exception { builder.accumulate(content); return content.isLast() ? State.ABORT : State.CONTINUE; } public State onStatusReceived( HttpResponseStatus responseStatus) throws Exception { builder.accumulate(responseStatus); return State.CONTINUE; } public Response onCompleted() throws Exception { return builder.build(); } }) .get(); assertNotNull(r); assertEquals(r.getStatusCode(), 200); }); }); }
// @Test(groups = "online") public void notRedirected302Test() throws Exception { isSet.getAndSet(false); try (AsyncHttpClient c = asyncHttpClient(config().setFollowRedirect(true))) { Response response = c.prepareGet(getTargetUrl()) .setFollowRedirect(false) .setHeader("X-redirect", "http://www.microsoft.com/") .execute() .get(); assertNotNull(response); assertEquals(response.getStatusCode(), 302); } }
// @Test(groups = "standalone") public void relativeLocationUrl() throws Exception { isSet.getAndSet(false); try (AsyncHttpClient c = asyncHttpClient()) { Response response = c.preparePost(getTargetUrl()) .setFollowRedirect(true) .setHeader("X-redirect", "/foo/test") .execute() .get(); assertNotNull(response); assertEquals(response.getStatusCode(), 200); assertEquals(response.getUri().toString(), getTargetUrl()); } }
// @Test(groups = "online") public void redirected302Test() throws Exception { isSet.getAndSet(false); try (AsyncHttpClient c = asyncHttpClient()) { Response response = c.prepareGet(getTargetUrl()) .setFollowRedirect(true) .setHeader("X-redirect", "http://www.microsoft.com/") .execute() .get(); assertNotNull(response); assertEquals(response.getStatusCode(), 200); String anyMicrosoftPage = "http://www.microsoft.com[^:]*:80"; String baseUrl = getBaseUrl(response.getUri()); assertTrue( baseUrl.matches(anyMicrosoftPage), "response does not show redirection to " + anyMicrosoftPage); } }