/
githubmirror
/
panama-vector
Обзор
Документация
Войти
/
githubmirror
/
panama-vector
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
test/jdk/java/net/HttpURLConnection/HttpURLConnectionHeadersOrder.java
187 строк
7 KB
Daniel Fuchs
8381767: Refactor various java/net/*[URL/Http]*/ TestNG tests to use JUnit
13 апр 2026, 15:44
13 апр 2026, 15:44
03b46a3
Код
Авторство
О чём код?
/* * Copyright (c) 2021, 2026, 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. */ /* * @test * @bug 8133686 * @summary Ensuring that multiple header values for a given field-name are returned in * the order they were added for HttpURLConnection.getRequestProperties * and HttpURLConnection.getHeaderFields * @library /test/lib * @run junit ${test.main.class} */ import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; import jdk.test.lib.net.URIBuilder; import java.io.IOException; import java.net.HttpURLConnection; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.URL; import java.util.Arrays; import java.util.List; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; public class HttpURLConnectionHeadersOrder { private static final String LOCAL_TEST_ENDPOINT = "/headertest"; private static final String ERROR_MESSAGE_TEMPLATE = "Expected Request Properties = %s, Actual Request Properties = %s"; private static final List<String> EXPECTED_HEADER_VALUES = Arrays.asList("a", "b", "c"); private static HttpServer server; private static URL serverUrl; @BeforeAll public static void beforeTest() throws Exception { SimpleHandler handler = new SimpleHandler(); server = createSimpleHttpServer(handler); serverUrl = URIBuilder.newBuilder() .scheme("http") .host(server.getAddress().getAddress()) .port(server.getAddress().getPort()) .path(LOCAL_TEST_ENDPOINT) .toURL(); } @AfterAll public static void afterTest() { if (server != null) server.stop(0); } /** * - This tests requestProperty insertion-order * - on the client side by sending a HTTP GET * - request to a "dummy" server with additional * - custom request properties * * @throws Exception if failed */ @Test public void testRequestPropertiesOrder() throws Exception { final var conn = (HttpURLConnection) serverUrl.openConnection(); conn.addRequestProperty("test_req_prop", "a"); conn.addRequestProperty("test_req_prop", "b"); conn.addRequestProperty("test_req_prop", "c"); conn.setRequestMethod("GET"); var requestProperties = conn.getRequestProperties(); var customRequestProps = requestProperties.get("test_req_prop"); conn.disconnect(); assertNotNull(customRequestProps); assertEquals(EXPECTED_HEADER_VALUES, customRequestProps, "Unexpected value for request header \"test_req_prop\""); } /** * - This tests whether or not the insertion order is preserved for custom headers * - on the server's side. * - The server will return a custom status code (999) if the expected headers * - are not equal to the actual headers * * @throws Exception */ @Test public void testServerSideRequestHeadersOrder() throws Exception { final var conn = (HttpURLConnection) serverUrl.openConnection(); conn.addRequestProperty("test_server_handling", "a"); conn.addRequestProperty("test_server_handling", "b"); conn.addRequestProperty("test_server_handling", "c"); int statusCode = conn.getResponseCode(); conn.disconnect(); assertEquals(999, statusCode, "The insertion-order was not preserved on the server-side response headers handling"); } @Test public void testClientSideResponseHeadersOrder() throws Exception { final var conn = (HttpURLConnection) serverUrl.openConnection(); conn.setRequestMethod("GET"); var actualCustomResponseHeaders = conn.getHeaderFields().get("Test_response"); assertNotNull(actualCustomResponseHeaders, "Error in reading custom response headers"); assertEquals(EXPECTED_HEADER_VALUES, actualCustomResponseHeaders, "Unexpected value for response header field \"Test_response\""); } private static HttpServer createSimpleHttpServer(SimpleHandler handler) throws IOException { var serverAddress = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0); var server = HttpServer.create(serverAddress, 0); server.createContext(LOCAL_TEST_ENDPOINT, handler); server.start(); System.out.println("Server started on " + server.getAddress()); return server; } private static class SimpleHandler implements HttpHandler { @Override public void handle(HttpExchange exchange) throws IOException { int statusCode = testRequestHeadersOrder(exchange); sendCustomResponse(exchange, statusCode); } private int testRequestHeadersOrder(HttpExchange exchange) { var requestHeaders = exchange.getRequestHeaders(); var actualTestRequestHeaders = requestHeaders.get("test_server_handling"); if (actualTestRequestHeaders == null) { System.out.println("Error: requestHeaders.get(\"test_server_handling\") returned null"); return -1; } if (!actualTestRequestHeaders.equals(EXPECTED_HEADER_VALUES)) { System.out.printf("Error for \"test_server_handling\" " + String.format(ERROR_MESSAGE_TEMPLATE, EXPECTED_HEADER_VALUES, actualTestRequestHeaders)); return -1; } return 999; } private void sendCustomResponse(HttpExchange exchange, int statusCode) throws IOException { var responseHeaders = exchange.getResponseHeaders(); responseHeaders.add("test_response", "a"); responseHeaders.add("test_response", "b"); responseHeaders.add("test_response", "c"); var outputStream = exchange.getResponseBody(); var response = "Testing headers"; exchange.sendResponseHeaders(statusCode, response.length()); outputStream.write(response.getBytes()); outputStream.flush(); outputStream.close(); } } }