1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.net.URI;
import java.util.Locale;
import jdk.internal.net.http.Origin;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
/*
* @test
* @summary verify the behaviour of jdk.internal.net.http.Origin
* @modules java.net.http/jdk.internal.net.http
* @run junit OriginTest
*/
class OriginTest {
@ParameterizedTest
@ValueSource(strings = {"foo", "Bar", "HttPS", "HTTP"})
void testInvalidScheme(final String scheme) throws Exception {
final String validHost = "127.0.0.1";
final int validPort = 80;
final IllegalArgumentException iae = assertThrows(IllegalArgumentException.class, () -> {
new Origin(scheme, validHost, validPort);
});
assertTrue(iae.getMessage().contains("scheme"),
"unexpected exception message: " + iae.getMessage());
}
@ParameterizedTest
@ValueSource(strings = {"http", "https"})
void testValidScheme(final String scheme) throws Exception {
final String validHost = "127.0.0.1";
final int validPort = 80;
final Origin o1 = new Origin(scheme, validHost, validPort);
assertEquals(validHost, o1.host(), "unexpected host");
assertEquals(validPort, o1.port(), "unexpected port");
assertEquals(scheme, o1.scheme(), "unexpected scheme");
final URI uri = URI.create(scheme + "://" + validHost + ":" + validPort);
final Origin o2 = Origin.from(uri);
assertNotNull(o2, "null Origin for URI " + uri);
assertEquals(validHost, o2.host(), "unexpected host");
assertEquals(validPort, o2.port(), "unexpected port");
assertEquals(scheme, o2.scheme(), "unexpected scheme");
}
@ParameterizedTest
@ValueSource(strings = {"JDK.java.net", "[::1]", "[0:0:0:0:0:0:0:1]"})
void testInvalidHost(final String host) throws Exception {
final String validScheme = "http";
final int validPort = 8000;
final IllegalArgumentException iae = assertThrows(IllegalArgumentException.class, () -> {
new Origin(validScheme, host, validPort);
});
assertTrue(iae.getMessage().contains("host"),
"unexpected exception message: " + iae.getMessage());
}
@ParameterizedTest
@ValueSource(strings = {"127.0.0.1", "localhost", "jdk.java.net", "::1", "0:0:0:0:0:0:0:1"})
void testValidHost(final String host) throws Exception {
final String validScheme = "https";
final int validPort = 42;
final Origin o1 = new Origin(validScheme, host, validPort);
assertEquals(host, o1.host(), "unexpected host");
assertEquals(validPort, o1.port(), "unexpected port");
assertEquals(validScheme, o1.scheme(), "unexpected scheme");
String uriHost = host;
if (host.contains(":")) {
uriHost = "[" + host + "]";
}
final URI uri = URI.create(validScheme + "://" + uriHost + ":" + validPort);
final Origin o2 = Origin.from(uri);
assertNotNull(o2, "null Origin for URI " + uri);
assertEquals(host, o2.host(), "unexpected host");
assertEquals(validPort, o2.port(), "unexpected port");
assertEquals(validScheme, o2.scheme(), "unexpected scheme");
}
@ParameterizedTest
@ValueSource(ints = {-1, 0})
void testInvalidPort(final int port) throws Exception {
final String validScheme = "http";
final String validHost = "127.0.0.1";
final IllegalArgumentException iae = assertThrows(IllegalArgumentException.class,
() -> new Origin(validScheme, validHost, port));
assertTrue(iae.getMessage().contains("port"),
"unexpected exception message: " + iae.getMessage());
}
@ParameterizedTest
@ValueSource(ints = {100, 1024, 80, 8080, 42})
void testValidPort(final int port) throws Exception {
final String validScheme = "https";
final String validHost = "localhost";
final Origin o1 = new Origin(validScheme, validHost, port);
assertEquals(validHost, o1.host(), "unexpected host");
assertEquals(port, o1.port(), "unexpected port");
assertEquals(validScheme, o1.scheme(), "unexpected scheme");
final URI uri = URI.create(validScheme + "://" + validHost + ":" + port);
final Origin o2 = Origin.from(uri);
assertNotNull(o2, "null Origin for URI " + uri);
assertEquals(validHost, o2.host(), "unexpected host");
assertEquals(port, o2.port(), "unexpected port");
assertEquals(validScheme, o2.scheme(), "unexpected scheme");
}
@Test
void testInferredPort() throws Exception {
final URI httpURI = URI.create("http://localhost");
final Origin httpOrigin = Origin.from(httpURI);
assertNotNull(httpOrigin, "null Origin for URI " + httpURI);
assertEquals("localhost", httpOrigin.host(), "unexpected host");
assertEquals(80, httpOrigin.port(), "unexpected port");
assertEquals("http", httpOrigin.scheme(), "unexpected scheme");
final URI httpsURI = URI.create("https://[::1]");
final Origin httpsOrigin = Origin.from(httpsURI);
assertNotNull(httpsOrigin, "null Origin for URI " + httpsURI);
assertEquals("::1", httpsOrigin.host(), "unexpected host");
assertEquals(443, httpsOrigin.port(), "unexpected port");
assertEquals("https", httpsOrigin.scheme(), "unexpected scheme");
}
@Test
void testFromURI() {
// non-lower case URI scheme is expected to be converted to lowercase in the Origin
// constructed through Origin.from(URI)
for (final String scheme : new String[]{"httPs", "HTTP"}) {
final String expectedScheme = scheme.toLowerCase(Locale.ROOT);
final URI uri = URI.create(scheme + "://localhost:1234");
final Origin origin = Origin.from(uri);
assertNotNull(origin, "null Origin for URI " + uri);
assertEquals("localhost", origin.host(), "unexpected host");
assertEquals(1234, origin.port(), "unexpected port");
assertEquals(expectedScheme, origin.scheme(), "unexpected scheme");
}
// URI without a port is expected to be defaulted to port 80 or 443 for http and https
// schemes respectively
for (final String scheme : new String[]{"http", "https"}) {
final int expectedPort = switch (scheme) {
case "http" -> 80;
case "https" -> 443;
default -> fail("unexpected scheme: " + scheme);
};
final URI uri = URI.create(scheme + "://localhost");
final Origin origin = Origin.from(uri);
assertNotNull(origin, "null Origin for URI " + uri);
assertEquals("localhost", origin.host(), "unexpected host");
assertEquals(expectedPort, origin.port(), "unexpected port");
assertEquals(scheme, origin.scheme(), "unexpected scheme");
}
}
}
|