Search in sources :

Example 61 with HttpHeaders

use of org.springframework.http.HttpHeaders in project spring-framework by spring-projects.

the class RequestMappingMessageConversionIntegrationTests method resource.

@Test
public void resource() throws Exception {
    ResponseEntity<byte[]> response = performGet("/resource", new HttpHeaders(), byte[].class);
    assertEquals(HttpStatus.OK, response.getStatusCode());
    assertTrue(response.hasBody());
    assertEquals(951, response.getHeaders().getContentLength());
    assertEquals(951, response.getBody().length);
    assertEquals(new MediaType("image", "x-png"), response.getHeaders().getContentType());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 62 with HttpHeaders

use of org.springframework.http.HttpHeaders in project spring-framework by spring-projects.

the class MockMultipartHttpServletRequest method getMultipartHeaders.

@Override
public HttpHeaders getMultipartHeaders(String paramOrFileName) {
    String contentType = getMultipartContentType(paramOrFileName);
    if (contentType != null) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", contentType);
        return headers;
    } else {
        return null;
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders)

Example 63 with HttpHeaders

use of org.springframework.http.HttpHeaders in project spring-framework by spring-projects.

the class MockMvcClientHttpRequestFactory method createRequest.

@Override
public ClientHttpRequest createRequest(final URI uri, final HttpMethod httpMethod) throws IOException {
    return new MockClientHttpRequest(httpMethod, uri) {

        @Override
        public ClientHttpResponse executeInternal() throws IOException {
            try {
                MockHttpServletRequestBuilder requestBuilder = request(httpMethod, uri);
                requestBuilder.content(getBodyAsBytes());
                requestBuilder.headers(getHeaders());
                MvcResult mvcResult = MockMvcClientHttpRequestFactory.this.mockMvc.perform(requestBuilder).andReturn();
                MockHttpServletResponse servletResponse = mvcResult.getResponse();
                HttpStatus status = HttpStatus.valueOf(servletResponse.getStatus());
                byte[] body = servletResponse.getContentAsByteArray();
                HttpHeaders headers = getResponseHeaders(servletResponse);
                MockClientHttpResponse clientResponse = new MockClientHttpResponse(body, status);
                clientResponse.getHeaders().putAll(headers);
                return clientResponse;
            } catch (Exception ex) {
                byte[] body = ex.toString().getBytes(StandardCharsets.UTF_8);
                return new MockClientHttpResponse(body, HttpStatus.INTERNAL_SERVER_ERROR);
            }
        }
    };
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpStatus(org.springframework.http.HttpStatus) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) MockClientHttpRequest(org.springframework.mock.http.client.MockClientHttpRequest) MvcResult(org.springframework.test.web.servlet.MvcResult) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) IOException(java.io.IOException) MockClientHttpResponse(org.springframework.mock.http.client.MockClientHttpResponse)

Example 64 with HttpHeaders

use of org.springframework.http.HttpHeaders in project spring-framework by spring-projects.

the class HeaderAssertionsTests method valueMatches.

@Test
public void valueMatches() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
    HeaderAssertions assertions = headerAssertions(headers);
    // Success
    assertions.valueMatches("Content-Type", ".*UTF-8.*");
    try {
        assertions.valueMatches("Content-Type", ".*ISO-8859-1.*");
        fail("Wrong pattern expected");
    } catch (AssertionError error) {
        Throwable cause = error.getCause();
        assertNotNull(cause);
        assertEquals("Response header [Content-Type]='application/json;charset=UTF-8' " + "does not match '.*ISO-8859-1.*'", cause.getMessage());
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) Test(org.junit.Test)

Example 65 with HttpHeaders

use of org.springframework.http.HttpHeaders in project spring-framework by spring-projects.

the class ReactorClientHttpResponse method getHeaders.

@Override
public HttpHeaders getHeaders() {
    HttpHeaders headers = new HttpHeaders();
    this.response.responseHeaders().entries().forEach(e -> headers.add(e.getKey(), e.getValue()));
    return headers;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders)

Aggregations

HttpHeaders (org.springframework.http.HttpHeaders)1676 Test (org.junit.Test)426 ResponseEntity (org.springframework.http.ResponseEntity)383 HttpEntity (org.springframework.http.HttpEntity)345 Test (org.junit.jupiter.api.Test)273 HashMap (java.util.HashMap)184 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)154 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)127 MediaType (org.springframework.http.MediaType)121 URI (java.net.URI)111 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)102 Map (java.util.Map)97 IOException (java.io.IOException)83 RestTemplate (org.springframework.web.client.RestTemplate)78 ArrayList (java.util.ArrayList)75 MessageHeaders (org.springframework.messaging.MessageHeaders)74 MultiValueMap (org.springframework.util.MultiValueMap)74 HttpStatus (org.springframework.http.HttpStatus)71 List (java.util.List)65 Timed (com.codahale.metrics.annotation.Timed)54