Example usage for org.springframework.http HttpHeaders toSingleValueMap

List of usage examples for org.springframework.http HttpHeaders toSingleValueMap

Introduction

In this page you can find the example usage for org.springframework.http HttpHeaders toSingleValueMap.

Prototype

@Override
    public Map<String, String> toSingleValueMap() 

Source Link

Usage

From source file:com.alexshabanov.springrestapi.restapitest.RestOperationsTestClient.java

private MockHttpServletRequest toMockHttpServletRequest(URI url, HttpMethod method,
        ClientHttpRequest clientHttpRequest) throws IOException {
    final MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(method.name(),
            url.getPath());//  w w  w .  j  a  va2s  .com

    // copy headers
    final HttpHeaders headers = clientHttpRequest.getHeaders();
    for (final String headerKey : headers.toSingleValueMap().keySet()) {
        final List<String> headerValues = headers.get(headerKey);
        for (final String headerValue : headerValues) {
            mockHttpServletRequest.addHeader(headerKey, headerValue);
        }
    }

    // copy query parameters
    final String query = clientHttpRequest.getURI().getQuery();
    if (query != null) {
        mockHttpServletRequest.setQueryString(query);
        final String[] queryParameters = query.split("&");
        for (String keyValueParam : queryParameters) {
            final String[] components = keyValueParam.split("=");
            if (components.length == 1) {
                continue; // optional parameter
            }

            Assert.isTrue(components.length == 2,
                    "Can't split query parameters " + keyValueParam + " by key-value pair");
            mockHttpServletRequest.setParameter(components[0], components[1]);
        }
    }

    // copy request body
    // TODO: another byte copying approach here
    // TODO: for now we rely to the fact that request body is always presented as byte array output stream
    final OutputStream requestBodyStream = clientHttpRequest.getBody();
    if (requestBodyStream instanceof ByteArrayOutputStream) {
        mockHttpServletRequest.setContent(((ByteArrayOutputStream) requestBodyStream).toByteArray());
    } else {
        throw new AssertionError("Ooops, client http request has non-ByteArrayOutputStream body");
    }

    return mockHttpServletRequest;
}

From source file:org.zalando.riptide.ActionsTest.java

@Test
public void shouldMapHeaders() {
    server.expect(requestTo(url)).andRespond(
            withSuccess().body(new ClassPathResource("account.json")).contentType(APPLICATION_JSON));

    final HttpHeaders headers = unit.execute(HEAD, url)
            .dispatch(status(), on(OK).capture(headers()), anyStatus().call(this::fail)).to(HttpHeaders.class);

    assertThat(headers.toSingleValueMap(), hasEntry("Content-Type", APPLICATION_JSON_VALUE));
}

From source file:com.netflix.genie.web.controllers.JobRestController.java

private void copyResponseHeaders(final HttpServletResponse response, final ClientHttpResponse forwardResponse) {
    final HttpHeaders headers = forwardResponse.getHeaders();
    for (final Map.Entry<String, String> header : headers.toSingleValueMap().entrySet()) {
        ////from w ww  .  ja v  a2  s .  c  o m
        // Do not add transfer encoding header since it forces Apache to truncate the response. Ideally we should
        // only copy headers that are needed.
        //
        if (!TRANSFER_ENCODING_HEADER.equalsIgnoreCase(header.getKey())) {
            response.setHeader(header.getKey(), header.getValue());
        }
    }
}