|
| 1 | +/* |
| 2 | + * Copyright 2023 Red Hat, Inc. |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 6 | + * and Apache License v2.0 which accompanies this distribution. |
| 7 | + * |
| 8 | + * The Eclipse Public License is available at |
| 9 | + * http://www.eclipse.org/legal/epl-v10.html |
| 10 | + * |
| 11 | + * The Apache License v2.0 is available at |
| 12 | + * http://www.opensource.org/licenses/apache2.0.php |
| 13 | + * |
| 14 | + * You may elect to redistribute this code under either of these licenses. |
| 15 | + */ |
| 16 | +package io.vertx.ext.web.impl; |
| 17 | + |
| 18 | +import io.netty.handler.codec.http.HttpResponseStatus; |
| 19 | +import io.vertx.core.http.HttpMethod; |
| 20 | +import io.vertx.core.net.HostAndPort; |
| 21 | +import io.vertx.ext.web.WebTestBase; |
| 22 | +import org.junit.Test; |
| 23 | +import org.junit.runner.RunWith; |
| 24 | +import org.junit.runners.Parameterized; |
| 25 | + |
| 26 | +import java.util.Arrays; |
| 27 | + |
| 28 | +@RunWith(Parameterized.class) |
| 29 | +public class AbsoluteUriTest extends WebTestBase { |
| 30 | + |
| 31 | + @Parameterized.Parameters(name = "target={0}, host={1}, query={2}") |
| 32 | + public static Iterable<Object[]> data() { |
| 33 | + return Arrays.asList( |
| 34 | + // absolute URI, no explicit authority, no query -> the authority in the URI is ignored |
| 35 | + new Object[]{"http://www.example.com:1234/some/path", null, null, "http://localhost:8080/some/path"}, |
| 36 | + // absolute URI, no explicit authority, query present -> the authority in the URI is ignored |
| 37 | + new Object[]{"http://www.example.com:1234/some/path", null, "a=1&b=2", "http://localhost:8080/some/path?a=1&b=2"}, |
| 38 | + // absolute URI, same explicit authority, no query |
| 39 | + new Object[]{"http://www.example.com:1234/some/path", "www.example.com:1234", null, "http://www.example.com:1234/some/path"}, |
| 40 | + // absolute URI, same explicit authority, query present |
| 41 | + new Object[]{"http://www.example.com:1234/some/path", "www.example.com:1234", "a=1&b=2", "http://www.example.com:1234/some/path?a=1&b=2"}, |
| 42 | + // absolute URI, different explicit authority, no query |
| 43 | + new Object[]{"http://www.example.com:1234/some/path", "another-host.com:5678", null, "http://another-host.com:5678/some/path"}, |
| 44 | + // absolute URI, different explicit authority, query present |
| 45 | + new Object[]{"http://www.example.com:1234/some/path", "another-host.com:5678", "a=1&b=2", "http://another-host.com:5678/some/path?a=1&b=2"}, |
| 46 | + // relative URI, no query |
| 47 | + new Object[]{"/some/path", "www.example.com:1234", null, "http://www.example.com:1234/some/path"}, |
| 48 | + // relative URI, query present |
| 49 | + new Object[]{"/some/path", "www.example.com:1234", "a=1&b=2", "http://www.example.com:1234/some/path?a=1&b=2"} |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + private final String target; |
| 54 | + private final String authority; |
| 55 | + private final String query; |
| 56 | + private final String expectedAbsoluteUri; |
| 57 | + |
| 58 | + public AbsoluteUriTest(String target, String authority, String query, String expectedAbsoluteUri) { |
| 59 | + this.target = target; |
| 60 | + this.authority = authority; |
| 61 | + this.query = query; |
| 62 | + this.expectedAbsoluteUri = expectedAbsoluteUri; |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testAbsoluteUri() throws Exception { |
| 67 | + String uri = target + (query != null ? "?" + query : ""); |
| 68 | + |
| 69 | + router.route().handler(event -> { |
| 70 | + assertEquals(expectedAbsoluteUri, event.request().absoluteURI()); |
| 71 | + assertEquals(uri, event.request().uri()); |
| 72 | + event.response().end(); |
| 73 | + }); |
| 74 | + |
| 75 | + testRequest(HttpMethod.GET, uri, request -> { |
| 76 | + if (authority != null) { |
| 77 | + request.authority(HostAndPort.parseAuthority(authority, 80)); |
| 78 | + } |
| 79 | + }, HttpResponseStatus.OK.code(), HttpResponseStatus.OK.reasonPhrase(), null); |
| 80 | + } |
| 81 | +} |
0 commit comments