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
|
/*
* Copyright (c) 2021, 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 testng HttpURLConnectionHeadersOrder
*/
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 org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.io.IOException;
import java.net.*;
import java.util.Arrays;
import java.util.List;
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;
@BeforeTest
public 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();
}
@AfterTest
public 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
*/
@Test (priority = 1)
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();
Assert.assertNotNull(customRequestProps);
Assert.assertEquals(customRequestProps, EXPECTED_HEADER_VALUES, String.format(ERROR_MESSAGE_TEMPLATE, EXPECTED_HEADER_VALUES.toString(), customRequestProps.toString()));
}
/**
* - 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 (priority = 2)
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();
Assert.assertEquals(statusCode, 999, "The insertion-order was not preserved on the server-side response headers handling");
}
@Test (priority = 3)
public void testClientSideResponseHeadersOrder() throws Exception {
final var conn = (HttpURLConnection) serverUrl.openConnection();
conn.setRequestMethod("GET");
var actualCustomResponseHeaders = conn.getHeaderFields().get("Test_response");
Assert.assertNotNull(actualCustomResponseHeaders, "Error in reading custom response headers");
Assert.assertEquals(EXPECTED_HEADER_VALUES, actualCustomResponseHeaders, String.format(ERROR_MESSAGE_TEMPLATE, EXPECTED_HEADER_VALUES.toString(), actualCustomResponseHeaders.toString()));
}
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.println("Error: " + String.format(ERROR_MESSAGE_TEMPLATE, EXPECTED_HEADER_VALUES.toString(), actualTestRequestHeaders.toString()));
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();
}
}
}
|