I am trying to process a request without an Accept
header in a specific way, but Jersey seems to struggle to fill it, no matter what I do, so it always looks like the request has Accept
, even if it is not.
import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.test.JerseyTest; import org.junit.Test; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.core.Application; import javax.ws.rs.core.Context; import javax.ws.rs.core.HttpHeaders; import static org.junit.Assert.assertEquals; public class JerseyTestTest extends JerseyTest { @Path("hello") public static class HelloResource { @GET public String getHello(@Context HttpHeaders httpHeaders) { String acceptHeader = httpHeaders.getHeaderString(HttpHeaders.ACCEPT); return acceptHeader != null ? acceptHeader : "No Accept Header"; } } @Override protected Application configure() { return new ResourceConfig(HelloResource.class); } @Test public void test() { final String hello = target("hello").request() .header(HttpHeaders.ACCEPT, null)
This test result:
org.junit.ComparisonFailure: Expected :No Accept Header Actual :text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Somehow there is a default Accept
header text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
, which is set somewhere. This is not documented, and I would like to figure out how to disable it. I looked at a source in Jersey but cannot find where this is happening or why.
Update: when I use curl to get to the endpoint without an Accept header, there is no Accept header generated, so the problem is that the problem is in the Jersey client or in the Jersey test environment.
Update 2: This error occurs when using the default Grizzly2 test container or the JDK test container, but NOT with the In Memory test container.