Path: blob/master/test/jdk/java/net/HttpURLConnection/UnmodifiableMaps.java
41149 views
/*1* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 712864826* @library /test/lib27* @modules jdk.httpserver28* @summary HttpURLConnection.getHeaderFields should return an unmodifiable Map29*/3031import java.io.IOException;32import java.net.InetAddress;33import java.net.InetSocketAddress;34import java.net.URI;35import java.net.HttpURLConnection;36import java.util.Collection;37import java.util.ArrayList;38import java.util.List;39import java.util.Map;40import com.sun.net.httpserver.HttpExchange;41import com.sun.net.httpserver.HttpHandler;42import com.sun.net.httpserver.HttpServer;43import com.sun.net.httpserver.Headers;44import static java.net.Proxy.NO_PROXY;45import jdk.test.lib.net.URIBuilder;4647public class UnmodifiableMaps {4849void test(String[] args) throws Exception {50HttpServer server = startHttpServer();51try {52InetSocketAddress address = server.getAddress();53URI uri = URIBuilder.newBuilder()54.scheme("http")55.host(address.getAddress())56.port(address.getPort())57.path("/foo")58.build();59doClient(uri);60} finally {61server.stop(0);62}63}6465void doClient(URI uri) throws Exception {66HttpURLConnection uc = (HttpURLConnection) uri.toURL().openConnection(NO_PROXY);6768// Test1: getRequestProperties is unmodifiable69System.out.println("Check getRequestProperties");70checkUnmodifiable(uc.getRequestProperties());71uc.addRequestProperty("X", "V");72uc.addRequestProperty("X1", "V1");73checkUnmodifiable(uc.getRequestProperties());7475int resp = uc.getResponseCode();76check(resp == 200,77"Unexpected response code. Expected 200, got " + resp);7879// Test2: getHeaderFields is unmodifiable80System.out.println("Check getHeaderFields");81checkUnmodifiable(uc.getHeaderFields());82// If the implementation does caching, check again.83checkUnmodifiable(uc.getHeaderFields());84}8586// HTTP Server87HttpServer startHttpServer() throws IOException {88InetAddress loopback = InetAddress.getLoopbackAddress();89HttpServer httpServer = HttpServer.create(new InetSocketAddress(loopback, 0), 0);90httpServer.createContext("/foo", new SimpleHandler());91httpServer.start();92return httpServer;93}9495class SimpleHandler implements HttpHandler {96@Override97public void handle(HttpExchange t) throws IOException {98Headers respHeaders = t.getResponseHeaders();99// ensure some response headers, over the usual ones100respHeaders.add("RespHdr1", "Value1");101respHeaders.add("RespHdr2", "Value2");102respHeaders.add("RespHdr3", "Value3");103t.sendResponseHeaders(200, -1);104t.close();105}106}107108void checkUnmodifiable(Map<String,List<String>> map) {109checkUnmodifiableMap(map);110111// Now check the individual values112Collection<List<String>> values = map.values();113for (List<String> value : values) {114checkUnmodifiableList(value);115}116}117118void checkUnmodifiableMap(final Map<String,List<String>> map) {119expectThrow( new Runnable() {120public void run() { map.clear(); }});121expectThrow( new Runnable() {122public void run() { map.put("X", new ArrayList<String>()); }});123expectThrow( new Runnable() {124public void run() { map.remove("X"); }});125}126127void checkUnmodifiableList(final List<String> list) {128expectThrow( new Runnable() {129public void run() { list.clear(); }});130expectThrow( new Runnable() {131public void run() { list.add("X"); }});132expectThrow( new Runnable() {133public void run() { list.remove("X"); }});134}135136void expectThrow(Runnable r) {137try { r.run(); fail("Excepted UOE to be thrown."); Thread.dumpStack(); }138catch (UnsupportedOperationException e) { pass(); }139}140141volatile int passed = 0, failed = 0;142void pass() {passed++;}143void fail() {failed++;}144void fail(String msg) {System.err.println(msg); fail();}145void unexpected(Throwable t) {failed++; t.printStackTrace();}146void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}147public static void main(String[] args) throws Throwable {148Class<?> k = new Object(){}.getClass().getEnclosingClass();149try {k.getMethod("instanceMain",String[].class)150.invoke( k.newInstance(), (Object) args);}151catch (Throwable e) {throw e.getCause();}}152public void instanceMain(String[] args) throws Throwable {153try {test(args);} catch (Throwable t) {unexpected(t);}154System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);155if (failed > 0) throw new AssertionError("Some tests failed");}156}157158159