diff --git a/core-common/src/main/java/org/glassfish/jersey/internal/util/collection/GuardianStringKeyMultivaluedMap.java b/core-common/src/main/java/org/glassfish/jersey/internal/util/collection/GuardianStringKeyMultivaluedMap.java index 5a2425e73f..36412832e3 100644 --- a/core-common/src/main/java/org/glassfish/jersey/internal/util/collection/GuardianStringKeyMultivaluedMap.java +++ b/core-common/src/main/java/org/glassfish/jersey/internal/util/collection/GuardianStringKeyMultivaluedMap.java @@ -381,6 +381,24 @@ public List subList(int fromIndex, int toIndex) { final List sublist = guarded.subList(fromIndex, toIndex); return sublist != null ? new GuardianList<>(sublist, guard) : sublist; } + + @Override + public String toString() { + return guarded.toString(); + } + + @Override + public boolean equals(Object obj) { + if (GuardianList.class.isInstance(obj)) { + return guarded.equals(((GuardianList) obj).guarded); + } + return guarded.equals(obj); + } + + @Override + public int hashCode() { + return guarded.hashCode(); + } } private static class GuardianIterator extends MutableGuardian implements Iterator { @@ -411,6 +429,11 @@ public void remove() { public void forEachRemaining(Consumer action) { guarded.forEachRemaining(action); } + + @Override + public String toString() { + return guarded.toString(); + } } private static class GuardianListIterator extends GuardianIterator implements ListIterator { diff --git a/tests/e2e/src/test/java/org/glassfish/jersey/tests/e2e/ClientTest.java b/tests/e2e/src/test/java/org/glassfish/jersey/tests/e2e/ClientTest.java index 0eb7b83d27..6d94f0bf2c 100644 --- a/tests/e2e/src/test/java/org/glassfish/jersey/tests/e2e/ClientTest.java +++ b/tests/e2e/src/test/java/org/glassfish/jersey/tests/e2e/ClientTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2022 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -16,8 +16,10 @@ package org.glassfish.jersey.tests.e2e; +import java.util.Arrays; import java.util.List; import java.util.Map; +import java.util.Set; import javax.ws.rs.Consumes; import javax.ws.rs.GET; @@ -32,12 +34,14 @@ import javax.ws.rs.core.Context; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; import javax.ws.rs.core.Variant; import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.test.JerseyTest; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -80,6 +84,30 @@ public String contentHeaders(@HeaderParam("custom-header") final String customHe return sb.toString(); } + + @GET + @Path("/tostring") + public String headersGet(@Context HttpHeaders hs) { + StringBuilder sb = new StringBuilder(); + List myHeaders = Arrays.asList("Accept", "Content-Type"); + + try { + MultivaluedMap rqhdrs = hs.getRequestHeaders(); + Set keys = rqhdrs.keySet(); + sb.append("getRequestHeaders= "); + for (String header : myHeaders) { + if (keys.contains(header)) { + sb.append( + "Found " + header + ": " + hs.getRequestHeader(header) + "; "); + } + } + } catch (Throwable ex) { + sb.append("Unexpected exception thrown in getRequestHeaders: " + + ex.getMessage()); + ex.printStackTrace(); + } + return sb.toString(); + } } @Override @@ -97,6 +125,26 @@ public void testAccesingHelloworldResource() { assertEquals(HelloWorldResource.MESSAGE, responseMessage); } + @Test + public void testHeadersToString() { + try (Response response = target("headers").path("tostring").request() + .header(HttpHeaders.ACCEPT, "text/*, text/html, text/html;level=1, */*") + .header(HttpHeaders.CONTENT_TYPE, "application/xml;charset=utf8") + .get()) { + String content = response.readEntity(String.class); + int index = -1; + Assertions.assertTrue((index = content.indexOf("getRequestHeaders=")) != -1); + Assertions.assertTrue((index = content.indexOf("Accept:")) != -1); + Assertions.assertTrue((index = content.indexOf("text/*")) != -1); + Assertions.assertTrue((index = content.indexOf("text/html")) != -1); + Assertions.assertTrue((index = content.indexOf("text/html")) != -1); + Assertions.assertTrue((index = content.indexOf("*/*")) != -1); + Assertions.assertTrue((index = content.indexOf("Content-Type:")) != -1); + Assertions.assertTrue((index = content.indexOf("application/xml")) != -1); + Assertions.assertTrue((index = content.indexOf("charset=utf8")) != -1); + } + } + @Test public void testAccesingMissingResource() { final WebTarget missingResource = target().path("missing");