Search in sources :

Example 16 with HttpHeaders

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

the class SampleActuatorUiApplicationTests method testHome.

@Test
public void testHome() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
    ResponseEntity<String> entity = this.restTemplate.exchange("/", HttpMethod.GET, new HttpEntity<Void>(headers), String.class);
    assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
    assertThat(entity.getBody()).contains("<title>Hello");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 17 with HttpHeaders

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

the class SampleActuatorUiApplicationTests method testError.

@Test
public void testError() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
    ResponseEntity<String> entity = this.restTemplate.exchange("/error", HttpMethod.GET, new HttpEntity<Void>(headers), String.class);
    assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
    assertThat(entity.getBody()).contains("<html>").contains("<body>").contains("Please contact the operator with the above information");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 18 with HttpHeaders

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

the class SampleActuatorApplicationTests method testHtmlErrorPage.

@Test
public void testHtmlErrorPage() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
    HttpEntity<?> request = new HttpEntity<Void>(headers);
    ResponseEntity<String> entity = this.restTemplate.withBasicAuth("user", getPassword()).exchange("/foo", HttpMethod.GET, request, String.class);
    assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
    String body = entity.getBody();
    assertThat(body).as("Body was null").isNotNull();
    assertThat(body).contains("This application has no explicit mapping for /error");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 19 with HttpHeaders

use of org.springframework.http.HttpHeaders in project spring-data-document-examples by spring-projects.

the class AbstractHttpMessageConverter method write.

/**
	 * {@inheritDoc}
	 * <p>This implementation delegates to {@link #getDefaultContentType(Object)} if a content
	 * type was not provided, calls {@link #getContentLength}, and sets the corresponding headers
	 * on the output message. It then calls {@link #writeInternal}.
	 */
public final void write(T t, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    HttpHeaders headers = outputMessage.getHeaders();
    if (headers.getContentType() == null) {
        if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) {
            contentType = getDefaultContentType(t);
        }
        if (contentType != null) {
            headers.setContentType(contentType);
        }
    }
    if (headers.getContentLength() == -1) {
        Long contentLength = getContentLength(t, contentType);
        if (contentLength != null) {
            headers.setContentLength(contentLength);
        }
    }
    writeInternal(t, outputMessage);
    outputMessage.getBody().flush();
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders)

Example 20 with HttpHeaders

use of org.springframework.http.HttpHeaders in project spring-data-document-examples by spring-projects.

the class FormHttpMessageConverter method writePart.

@SuppressWarnings("unchecked")
private void writePart(String name, HttpEntity partEntity, OutputStream os) throws IOException {
    Object partBody = partEntity.getBody();
    Class<?> partType = partBody.getClass();
    HttpHeaders partHeaders = partEntity.getHeaders();
    MediaType partContentType = partHeaders.getContentType();
    for (HttpMessageConverter messageConverter : partConverters) {
        if (messageConverter.canWrite(partType, partContentType)) {
            HttpOutputMessage multipartOutputMessage = new MultipartHttpOutputMessage(os);
            multipartOutputMessage.getHeaders().setContentDispositionFormData(name, getFilename(partBody));
            if (!partHeaders.isEmpty()) {
                multipartOutputMessage.getHeaders().putAll(partHeaders);
            }
            messageConverter.write(partBody, partContentType, multipartOutputMessage);
            return;
        }
    }
    throw new HttpMessageNotWritableException("Could not write request: no suitable HttpMessageConverter found for request type [" + partType.getName() + "]");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpOutputMessage(org.springframework.http.HttpOutputMessage) MediaType(org.springframework.http.MediaType)

Aggregations

HttpHeaders (org.springframework.http.HttpHeaders)425 Test (org.junit.Test)205 ResponseEntity (org.springframework.http.ResponseEntity)63 HttpEntity (org.springframework.http.HttpEntity)62 URI (java.net.URI)52 MediaType (org.springframework.http.MediaType)50 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)42 ByteArrayInputStream (java.io.ByteArrayInputStream)30 HttpStatus (org.springframework.http.HttpStatus)30 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)30 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)30 IOException (java.io.IOException)21 Map (java.util.Map)21 ArrayList (java.util.ArrayList)19 List (java.util.List)18 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)18 MultiValueMap (org.springframework.util.MultiValueMap)18 HttpInputMessage (org.springframework.http.HttpInputMessage)17 TestRestTemplate (org.springframework.boot.test.web.client.TestRestTemplate)15 HttpOutputMessage (org.springframework.http.HttpOutputMessage)14