JerseyTest Client with MessageBodyWriter don't pass headers to my MessageBodyReader server-side

529 Views Asked by At

My headers are not correctly passed from client to server when using JerseyTest.

Both server and client have MessageBodyWriter and MessageBodyReader configured respectively with JerseyTest::configure and JerseyTest::configureClient.

My MessageBodyWriter add some custom headers to query :

@Override
public void writeTo(MyPojo myPojo, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
    httpHeaders.put("X-Custom-Header", singletonList(myPojo.header()));
}

but when MessageBodyReader is hit by server, it contains only standard headers.

Any clues on what I'm missing ?

1

There are 1 best solutions below

0
On BEST ANSWER

Ok seems a bug with InMemoryConnector :

public ClientResponse apply(final ClientRequest clientRequest) {
    PropertiesDelegate propertiesDelegate = new MapPropertiesDelegate();

    final ContainerRequest containerRequest = new ContainerRequest(baseUri,
            clientRequest.getUri(), clientRequest.getMethod(),
            null, propertiesDelegate);

    containerRequest.getHeaders().putAll(clientRequest.getStringHeaders());

    final ByteArrayOutputStream clientOutput = new ByteArrayOutputStream();
    if (clientRequest.getEntity() != null) {
        clientRequest.setStreamProvider(new OutboundMessageContext.StreamProvider() {
            @Override
            public OutputStream getOutputStream(int contentLength) throws IOException {
                final MultivaluedMap<String, Object> clientHeaders = clientRequest.getHeaders();
                if (contentLength != -1 && !clientHeaders.containsKey(HttpHeaders.CONTENT_LENGTH)) {
                    containerRequest.getHeaders().putSingle(HttpHeaders.CONTENT_LENGTH, String.valueOf(contentLength));
                }
                return clientOutput;
            }
        });

All standard headers are set with :

containerRequest.getHeaders().putAll(clientRequest.getStringHeaders());

But if you provide custom headers, they are read but never added to containerRequest :

final MultivaluedMap<String, Object> clientHeaders = clientRequest.getHeaders();

also https://java.net/jira/browse/JERSEY-2341 suggest it won't be fixed soon.