@Test(groups = "online", enabled = false) public void redirected302InvalidTest() throws Exception { isSet.getAndSet(false); Exception e = null; try (AsyncHttpClient c = asyncHttpClient()) { c.preparePost(getTargetUrl()).setFollowRedirect(true).setHeader("X-redirect", String.format("http://localhost:%d/", port2)).execute().get(); } catch (ExecutionException ex) { e = ex; } assertNotNull(e); Throwable cause = e.getCause(); assertTrue(cause instanceof ConnectException); assertTrue(cause.getMessage().contains(":" + port2)); }
@Test(groups = "online", enabled = false) public void redirected302Test() throws Exception { isSet.getAndSet(false); try (AsyncHttpClient c = asyncHttpClient()) { Response response = c.prepareGet(getTargetUrl()).setFollowRedirect(true).setHeader("X-redirect", "https://www.microsoft.com/").execute().get(); assertNotNull(response); assertEquals(response.getStatusCode(), 200); String anyMicrosoftPage = "https://www.microsoft.com[^:]*:443"; String baseUrl = getBaseUrl(response.getUri()); assertTrue(baseUrl.matches(anyMicrosoftPage), "response does not show redirection to " + anyMicrosoftPage); } }
@Test(enabled = false) 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", enabled = false) 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); } }
private JsonObject performRequest(BoundRequestBuilder requestBuilder, JsonObject body, int expectedStatusCode, String errorMessage) { if (body != null) { requestBuilder.setBody(body.toJson()); } addAuth(requestBuilder); requestBuilder.setFollowRedirect(true); requestBuilder.addHeader("Content-Type", JSON.toString()); requestBuilder.addHeader("Accept", JSON.toString()); requestBuilder.setCharset(StandardCharsets.UTF_8); Request request = requestBuilder.build(); MDC.put(MDC_URL_KEY, request.getUrl()); MDC.put(MDC_METHOD_KEY, request.getMethod()); try { Response response = httpClient.executeRequest(request).get(stashTimeout, TimeUnit.MILLISECONDS); validateResponse(response, expectedStatusCode, errorMessage); return extractResponse(response); } catch (ExecutionException | TimeoutException e) { throw new StashClientException(request.getUrl(), e); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new StashClientException(request.getUrl(), e); } finally { MDC.remove(MDC_URL_KEY); MDC.remove(MDC_METHOD_KEY); } }