How do I get Jersey Test/Client to not fill in a default Accept header?

前端 未结 3 1197
挽巷
挽巷 2021-01-18 03:51

I\'m trying to handle a request with no Accept header in a particular way, but Jersey seems hell-bent on filling one in, no matter what I do, so it always looks

3条回答
  •  南方客
    南方客 (楼主)
    2021-01-18 04:37

    Not sure if this helps anyone else but we were seeing similar behavior calling a service in production with our client code. We are using Jersey 2.21.1.

    Expanded the code from the original post and found the following to be true:

    • if Accept header is null then Jersey adds the default
    • if Accept header is an empty String then an empty Accept header is used
    • if Accept header has a value it is used

    I'm not sure if there is a way to tell Jersey not to add the default value when a null is used.

    public class JerseyAcceptHeaderTest extends JerseyTest {
    
        @Path("hello")
        public static class HelloResource {
            @GET
            public String getHello(@Context HttpHeaders httpHeaders) {
                String acceptHeader = httpHeaders.getHeaderString(HttpHeaders.ACCEPT);
                System.out.println("SERVER RECEIVED:" + acceptHeader);
    
                if (acceptHeader == null) {
                    return "Null Accept Header";
                } else if (acceptHeader.equals("")) {
                    return "No Accept Header";
                } else {
                    return acceptHeader;
                }
            }
        }
    
        @Override
        protected Application configure() {
            return new ResourceConfig(HelloResource.class);
        }
    
        /**
         * this seems to be a bug in Jersey
         * it overrides a null Accept header
         */
        @Test
        public void test_accept_header_with_null() {
            final String acceptHeader = target("hello").request()
                    .header(HttpHeaders.ACCEPT, null)
                    .get(String.class);
            assertEquals("Null Accept Header", acceptHeader);
        }
    
        @Test
        public void test_accept_header_with_empty_string() {
            final String acceptHeader = target("hello").request()
                    .header(HttpHeaders.ACCEPT, "")
                    .get(String.class);
            assertEquals("No Accept Header", acceptHeader);
        }
    
        @Test
        public void test_accept_header_with_spaced_string() {
            final String acceptHeader = target("hello").request()
                    .header(HttpHeaders.ACCEPT, "  ")
                    .get(String.class);
            assertEquals("No Accept Header", acceptHeader);
        }
    
        @Test
        public void test_accept_header_with_value() {
            final String acceptHeader = target("hello").request()
                    .header(HttpHeaders.ACCEPT, "application/json")
                    .get(String.class);
            assertEquals("application/json", acceptHeader);
        }
    
    }
    

提交回复
热议问题