Search in sources :

Example 26 with MediaType

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

the class RequestPartServletServerHttpRequest method determineCharset.

private Charset determineCharset() {
    MediaType contentType = getHeaders().getContentType();
    if (contentType != null) {
        Charset charset = contentType.getCharset();
        if (charset != null) {
            return charset;
        }
    }
    String encoding = this.multipartRequest.getCharacterEncoding();
    return (encoding != null ? Charset.forName(encoding) : FORM_CHARSET);
}
Also used : MediaType(org.springframework.http.MediaType) Charset(java.nio.charset.Charset)

Example 27 with MediaType

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

the class EncoderHttpMessageWriterTests method testDefaultMediaType.

private void testDefaultMediaType(MediaType negotiatedMediaType) {
    this.response = new MockServerHttpResponse();
    this.mediaTypeCaptor = ArgumentCaptor.forClass(MediaType.class);
    MimeType defaultContentType = MimeTypeUtils.TEXT_XML;
    HttpMessageWriter<String> writer = getWriter(defaultContentType);
    writer.write(Mono.just("body"), forClass(String.class), negotiatedMediaType, this.response, NO_HINTS);
    assertEquals(defaultContentType, this.response.getHeaders().getContentType());
    assertEquals(defaultContentType, this.mediaTypeCaptor.getValue());
}
Also used : MediaType(org.springframework.http.MediaType) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) MimeType(org.springframework.util.MimeType)

Example 28 with MediaType

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

the class EncoderHttpMessageWriterTests method useDefaultMediaTypeCharset.

@Test
public void useDefaultMediaTypeCharset() throws Exception {
    HttpMessageWriter<String> writer = getWriter(TEXT_PLAIN_UTF_8, TEXT_HTML);
    writer.write(Mono.just("body"), forClass(String.class), TEXT_HTML, response, NO_HINTS);
    assertEquals(new MediaType("text", "html", UTF_8), this.response.getHeaders().getContentType());
    assertEquals(new MediaType("text", "html", UTF_8), this.mediaTypeCaptor.getValue());
}
Also used : MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 29 with MediaType

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

the class EncoderHttpMessageWriterTests method useNegotiatedMediaTypeCharset.

@Test
public void useNegotiatedMediaTypeCharset() throws Exception {
    MediaType negotiatedMediaType = new MediaType("text", "html", ISO_8859_1);
    HttpMessageWriter<String> writer = getWriter(TEXT_PLAIN_UTF_8, TEXT_HTML);
    writer.write(Mono.just("body"), forClass(String.class), negotiatedMediaType, this.response, NO_HINTS);
    assertEquals(negotiatedMediaType, this.response.getHeaders().getContentType());
    assertEquals(negotiatedMediaType, this.mediaTypeCaptor.getValue());
}
Also used : MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 30 with MediaType

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

the class BufferedImageHttpMessageConverterTests method read.

@Test
public void read() throws IOException {
    Resource logo = new ClassPathResource("logo.jpg", BufferedImageHttpMessageConverterTests.class);
    byte[] body = FileCopyUtils.copyToByteArray(logo.getInputStream());
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
    inputMessage.getHeaders().setContentType(new MediaType("image", "jpeg"));
    BufferedImage result = converter.read(BufferedImage.class, inputMessage);
    assertEquals("Invalid height", 500, result.getHeight());
    assertEquals("Invalid width", 750, result.getWidth());
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) MediaType(org.springframework.http.MediaType) ClassPathResource(org.springframework.core.io.ClassPathResource) BufferedImage(java.awt.image.BufferedImage) Test(org.junit.Test)

Aggregations

MediaType (org.springframework.http.MediaType)253 Test (org.junit.Test)157 HttpHeaders (org.springframework.http.HttpHeaders)49 MockHttpInputMessage (org.springframework.http.MockHttpInputMessage)31 MockHttpOutputMessage (org.springframework.http.MockHttpOutputMessage)24 Charset (java.nio.charset.Charset)21 ArrayList (java.util.ArrayList)21 URI (java.net.URI)20 ByteArrayInputStream (java.io.ByteArrayInputStream)15 IOException (java.io.IOException)15 ServerWebExchange (org.springframework.web.server.ServerWebExchange)15 List (java.util.List)14 HttpInputMessage (org.springframework.http.HttpInputMessage)14 HttpStatus (org.springframework.http.HttpStatus)11 Resource (org.springframework.core.io.Resource)10 HttpEntity (org.springframework.http.HttpEntity)10 HttpMessageNotReadableException (org.springframework.http.converter.HttpMessageNotReadableException)9 MediaTypeRequestMatcher (org.springframework.security.web.util.matcher.MediaTypeRequestMatcher)9 HttpMediaTypeNotAcceptableException (org.springframework.web.HttpMediaTypeNotAcceptableException)9 InputStream (java.io.InputStream)8