11/*
2- * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2323
2424/*
2525 * @test
26- * @bug 8251496
26+ * @bug 8251496 8333590
2727 * @summary Test that UnmodifiableHeaders is in fact immutable
2828 * @modules jdk.httpserver/sun.net.httpserver:+open
2929 * @run testng/othervm UnmodifiableHeadersTest
4444import org .testng .annotations .Test ;
4545import sun .net .httpserver .UnmodifiableHeaders ;
4646import static org .testng .Assert .assertEquals ;
47+ import static org .testng .Assert .assertNotNull ;
4748import static org .testng .Assert .assertThrows ;
49+ import static org .testng .Assert .assertTrue ;
4850
4951public class UnmodifiableHeadersTest {
5052
@@ -70,9 +72,9 @@ public Object[][] headers() {
7072 var exchange = new TestHttpExchange (headers );
7173
7274 return new Object [][] {
73- { exchange .getRequestHeaders () },
74- { Headers .of ("Foo" , "Bar" ) },
75- { Headers .of (Map .of ("Foo" , List .of ("Bar" ))) },
75+ { exchange .getRequestHeaders () },
76+ { Headers .of ("Foo" , "Bar" ) },
77+ { Headers .of (Map .of ("Foo" , List .of ("Bar" ))) },
7678 };
7779 }
7880
@@ -83,6 +85,44 @@ public static void testUnmodifiableHeaders(Headers headers) {
8385 assertUnmodifiableList (headers );
8486 }
8587
88+ @ DataProvider
89+ public Object [][] toStringHeaders () {
90+ final Headers headers = new Headers ();
91+ headers .add ("hello" , "World" );
92+ return new Object [][] {
93+ { headers },
94+ { Headers .of ("abc" , "XYZ" ) },
95+ { Headers .of (Map .of ("foo" , List .of ("Bar" ))) },
96+ { Headers .of (Map .of ("Hello" , List .of ())) },
97+ { Headers .of (Map .of ("one" , List .of ("two" , "THREE" ))) },
98+ };
99+ }
100+
101+ /*
102+ * Verify that the String returned by Headers.toString() contains the expected
103+ * key/value(s)
104+ */
105+ @ Test (dataProvider = "toStringHeaders" )
106+ public void testToString (final Headers headers ) {
107+ final Headers copy = Headers .of (headers );
108+ assertNotNull (copy , "Headers.of() returned null" );
109+ final String actualToString = copy .toString ();
110+ assertNotNull (actualToString , "toString() returned null" );
111+ for (final Map .Entry <String , List <String >> originalHeadersEntry : headers .entrySet ()) {
112+ final String expectedKey = originalHeadersEntry .getKey ();
113+ // We just verify the presence of key and value in the toString()
114+ // return value. We intentionally don't expect or verify that the
115+ // toString() content is in some specific form.
116+ assertTrue (actualToString .contains (expectedKey ),
117+ expectedKey + " missing in output of Headers.of().toString()" );
118+ final List <String > expectedVals = originalHeadersEntry .getValue ();
119+ for (final String val : expectedVals ) {
120+ assertTrue (actualToString .contains (val ), val + " for header key "
121+ + expectedKey + " missing in output of Headers.of().toString()" );
122+ }
123+ }
124+ }
125+
86126 static final Class <UnsupportedOperationException > UOP = UnsupportedOperationException .class ;
87127
88128 static void assertUnsupportedOperation (Headers headers ) {
0 commit comments