|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +import jdk.test.lib.net.SimpleSSLContext; |
| 25 | +import jdk.test.lib.net.URIBuilder; |
| 26 | +import org.junit.jupiter.api.AfterAll; |
| 27 | +import org.junit.jupiter.api.BeforeAll; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.junit.jupiter.api.TestInstance; |
| 30 | + |
| 31 | +import javax.net.ssl.SSLContext; |
| 32 | +import java.io.IOException; |
| 33 | +import java.net.URI; |
| 34 | +import java.net.http.HttpClient; |
| 35 | +import java.net.http.HttpRequest; |
| 36 | +import java.net.http.HttpResponse; |
| 37 | + |
| 38 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 39 | + |
| 40 | + |
| 41 | +/** |
| 42 | + * @test |
| 43 | + * @bug 8292876 |
| 44 | + * @library /test/lib server |
| 45 | + * @modules java.base/sun.net.www.http |
| 46 | + * java.net.http/jdk.internal.net.http.common |
| 47 | + * java.net.http/jdk.internal.net.http.frame |
| 48 | + * java.net.http/jdk.internal.net.http.hpack |
| 49 | + * @run junit UserInfoTest |
| 50 | + */ |
| 51 | + |
| 52 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 53 | +public class UserInfoTest { |
| 54 | + |
| 55 | + Http2TestServer server; |
| 56 | + int port; |
| 57 | + SSLContext sslContext; |
| 58 | + |
| 59 | + @BeforeAll |
| 60 | + void before() throws Exception { |
| 61 | + sslContext = new SimpleSSLContext().get(); |
| 62 | + server = createServer(sslContext); |
| 63 | + port = server.getAddress().getPort(); |
| 64 | + server.start(); |
| 65 | + } |
| 66 | + |
| 67 | + @AfterAll |
| 68 | + void after() throws Exception { |
| 69 | + server.close(); |
| 70 | + } |
| 71 | + |
| 72 | + static class Http2TestHandler implements Http2Handler { |
| 73 | + @Override |
| 74 | + public void handle(Http2TestExchange e) throws IOException { |
| 75 | + String authorityHeader = e.getRequestHeaders().firstValue(":authority").orElse(null); |
| 76 | + if (authorityHeader == null || authorityHeader.contains("user@")) { |
| 77 | + e.sendResponseHeaders(500, -1); |
| 78 | + } else { |
| 79 | + e.sendResponseHeaders(200, -1); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private static Http2TestServer createServer(SSLContext sslContext) throws Exception { |
| 85 | + Http2TestServer http2TestServer = new Http2TestServer("localhost", true, sslContext); |
| 86 | + Http2TestHandler handler = new Http2TestHandler(); |
| 87 | + http2TestServer.addHandler(handler, "/"); |
| 88 | + return http2TestServer; |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testAuthorityHeader() throws Exception { |
| 93 | + HttpClient client = HttpClient |
| 94 | + .newBuilder() |
| 95 | + .proxy(HttpClient.Builder.NO_PROXY) |
| 96 | + .sslContext(sslContext) |
| 97 | + .build(); |
| 98 | + |
| 99 | + URI uri = URIBuilder.newBuilder() |
| 100 | + .scheme("https") |
| 101 | + .userInfo("user") |
| 102 | + .loopback() |
| 103 | + .port(port) |
| 104 | + .build(); |
| 105 | + |
| 106 | + HttpRequest request = HttpRequest |
| 107 | + .newBuilder(uri) |
| 108 | + .GET() |
| 109 | + .build(); |
| 110 | + |
| 111 | + HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 112 | + |
| 113 | + assertEquals(200, response.statusCode(), "Test Failed : " + response.uri().getAuthority()); |
| 114 | + } |
| 115 | +} |
0 commit comments