|
| 1 | +package org.asynchttpclient; |
| 2 | + |
| 3 | +import com.sun.net.httpserver.HttpExchange; |
| 4 | +import com.sun.net.httpserver.HttpHandler; |
| 5 | +import com.sun.net.httpserver.HttpServer; |
| 6 | +import org.junit.jupiter.api.AfterAll; |
| 7 | +import org.junit.jupiter.api.BeforeAll; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +import java.net.InetSocketAddress; |
| 11 | +import java.util.concurrent.TimeUnit; |
| 12 | + |
| 13 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 15 | + |
| 16 | +public class StripAuthorizationOnRedirectHttpTest { |
| 17 | + private static HttpServer server; |
| 18 | + private static int port; |
| 19 | + private static volatile String lastAuthHeader; |
| 20 | + |
| 21 | + @BeforeAll |
| 22 | + public static void startServer() throws Exception { |
| 23 | + server = HttpServer.create(new InetSocketAddress(0), 0); |
| 24 | + port = server.getAddress().getPort(); |
| 25 | + server.createContext("/redirect", new RedirectHandler()); |
| 26 | + server.createContext("/final", new FinalHandler()); |
| 27 | + server.start(); |
| 28 | + } |
| 29 | + |
| 30 | + @AfterAll |
| 31 | + public static void stopServer() { |
| 32 | + server.stop(0); |
| 33 | + } |
| 34 | + |
| 35 | + static class RedirectHandler implements HttpHandler { |
| 36 | + @Override |
| 37 | + public void handle(HttpExchange exchange) { |
| 38 | + String auth = exchange.getRequestHeaders().getFirst("Authorization"); |
| 39 | + lastAuthHeader = auth; |
| 40 | + exchange.getResponseHeaders().add("Location", "http://localhost:" + port + "/final"); |
| 41 | + try { |
| 42 | + exchange.sendResponseHeaders(302, -1); |
| 43 | + } catch (Exception ignored) { |
| 44 | + } |
| 45 | + exchange.close(); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + static class FinalHandler implements HttpHandler { |
| 50 | + @Override |
| 51 | + public void handle(HttpExchange exchange) { |
| 52 | + String auth = exchange.getRequestHeaders().getFirst("Authorization"); |
| 53 | + lastAuthHeader = auth; |
| 54 | + try { |
| 55 | + exchange.sendResponseHeaders(200, 0); |
| 56 | + exchange.getResponseBody().close(); |
| 57 | + } catch (Exception ignored) { |
| 58 | + } |
| 59 | + exchange.close(); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void testAuthHeaderPropagatedByDefault() throws Exception { |
| 65 | + DefaultAsyncHttpClientConfig config = new DefaultAsyncHttpClientConfig.Builder() |
| 66 | + .setFollowRedirect(true) |
| 67 | + .build(); |
| 68 | + try (DefaultAsyncHttpClient client = new DefaultAsyncHttpClient(config)) { |
| 69 | + lastAuthHeader = null; |
| 70 | + client.prepareGet("http://localhost:" + port + "/redirect") |
| 71 | + .setHeader("Authorization", "Bearer testtoken") |
| 72 | + .execute() |
| 73 | + .get(5, TimeUnit.SECONDS); |
| 74 | + // By default, Authorization header is propagated to /final |
| 75 | + assertEquals("Bearer testtoken", lastAuthHeader, "Authorization header should be present on redirect by default"); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void testAuthHeaderStrippedWhenEnabled() throws Exception { |
| 81 | + DefaultAsyncHttpClientConfig config = new DefaultAsyncHttpClientConfig.Builder() |
| 82 | + .setFollowRedirect(true) |
| 83 | + .setStripAuthorizationOnRedirect(true) |
| 84 | + .build(); |
| 85 | + try (DefaultAsyncHttpClient client = new DefaultAsyncHttpClient(config)) { |
| 86 | + lastAuthHeader = null; |
| 87 | + client.prepareGet("http://localhost:" + port + "/redirect") |
| 88 | + .setHeader("Authorization", "Bearer testtoken") |
| 89 | + .execute() |
| 90 | + .get(5, TimeUnit.SECONDS); |
| 91 | + // When enabled, Authorization header should be stripped on /final |
| 92 | + assertNull(lastAuthHeader, "Authorization header should be stripped on redirect when enabled"); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments