/** * Determine the Content-Type of the HTTP message based on the * "Content-Type" header or otherwise default to * {@link MediaType#APPLICATION_OCTET_STREAM}. * @param inputMessage the HTTP message * @return the MediaType, possibly {@code null}. */ @Nullable protected MediaType getContentType(HttpMessage inputMessage) { MediaType contentType = inputMessage.getHeaders().getContentType(); return (contentType != null ? contentType : MediaType.APPLICATION_OCTET_STREAM); }
private Mono<Void> writeSingleRegion(ResourceRegion region, ReactiveHttpOutputMessage message, Map<String, Object> hints) { return zeroCopy(region.getResource(), region, message, hints) .orElseGet(() -> { Publisher<? extends ResourceRegion> input = Mono.just(region); MediaType mediaType = message.getHeaders().getContentType(); return encodeAndWriteRegions(input, mediaType, message, hints); }); }
/** * Assert the request content type as a {@link MediaType}. */ public RequestMatcher contentType(final MediaType expectedContentType) { return request -> { MediaType actualContentType = request.getHeaders().getContentType(); assertTrue("Content type not set", actualContentType != null); assertEquals("Content type", expectedContentType, actualContentType); }; }
@Override protected String readInternal(Class<? extends String> clazz, HttpInputMessage inputMessage) throws IOException { Charset charset = getContentTypeCharset(inputMessage.getHeaders().getContentType()); return StreamUtils.copyToString(inputMessage.getBody(), charset); }
private Charset getCharset() { Charset charset = null; MediaType contentType = getHeaders().getContentType(); if (contentType != null) { charset = contentType.getCharset(); } return (charset != null ? charset : StandardCharsets.UTF_8); }
@Override public String getContentType() { MediaType type = this.outputMessage.getHeaders().getContentType(); return (type != null ? type.toString() : null); }
@Test public void writeSAXSource() throws Exception { String xml = "<root>Hello World</root>"; SAXSource saxSource = new SAXSource(new InputSource(new StringReader(xml))); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); converter.write(saxSource, null, outputMessage); assertThat("Invalid result", outputMessage.getBodyAsString(StandardCharsets.UTF_8), isSimilarTo("<root>Hello World</root>")); assertEquals("Invalid content-type", new MediaType("application", "xml"), outputMessage.getHeaders().getContentType()); }
@RequestMapping("/httpHeaders") public void httpHeaders(@RequestHeader HttpHeaders headers, Writer writer) throws IOException { assertEquals("Invalid Content-Type", new MediaType("text", "html"), headers.getContentType()); multiValueMap(headers, writer); }
@Test public void shouldWriteImageResource() throws IOException { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); Resource body = new ClassPathResource("logo.jpg", getClass()); converter.write(body, null, outputMessage); assertEquals("Invalid content-type", MediaType.IMAGE_JPEG, outputMessage.getHeaders().getContentType()); assertEquals("Invalid content-length", body.getFile().length(), outputMessage.getHeaders().getContentLength()); }
@Test public void useNegotiatedMediaTypeCharset() { 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()); }
private ServerWebExchange exchangeMultipart(MultiValueMap<String, ?> multipartData) { MockClientHttpRequest request = new MockClientHttpRequest(HttpMethod.POST, "/"); new MultipartHttpMessageWriter().write(Mono.just(multipartData), forClass(MultiValueMap.class), MediaType.MULTIPART_FORM_DATA, request, Collections.emptyMap()).block(); return MockServerWebExchange.from(MockServerHttpRequest .post("/") .contentType(request.getHeaders().getContentType()) .body(request.getBody())); }
@Test public void multiValueMap() { MultiValueMap<String, String> map = new LinkedMultiValueMap<>(); map.set("Content-Type", "text/plain"); String body = "foo"; HttpEntity<String> entity = new HttpEntity<>(body, map); assertEquals(body, entity.getBody()); assertEquals(MediaType.TEXT_PLAIN, entity.getHeaders().getContentType()); assertEquals("text/plain", entity.getHeaders().getFirst("Content-Type")); }
@Test public void writeUTF8() throws IOException { String body = "H\u00e9llo W\u00f6rld"; this.converter.write(body, TEXT_PLAIN_UTF_8, this.outputMessage); HttpHeaders headers = this.outputMessage.getHeaders(); assertEquals(body, this.outputMessage.getBodyAsString(StandardCharsets.UTF_8)); assertEquals(TEXT_PLAIN_UTF_8, headers.getContentType()); assertEquals(body.getBytes(StandardCharsets.UTF_8).length, headers.getContentLength()); assertFalse(headers.getAcceptCharset().isEmpty()); }
@Test public void writeDefaultCharset() throws IOException { String body = "H\u00e9llo W\u00f6rld"; this.converter.write(body, null, this.outputMessage); HttpHeaders headers = this.outputMessage.getHeaders(); assertEquals(body, this.outputMessage.getBodyAsString(StandardCharsets.ISO_8859_1)); assertEquals(new MediaType("text", "plain", StandardCharsets.ISO_8859_1), headers.getContentType()); assertEquals(body.getBytes(StandardCharsets.ISO_8859_1).length, headers.getContentLength()); assertFalse(headers.getAcceptCharset().isEmpty()); }
@Test public void successWithContentWithoutContentType() throws Exception { DefaultResponseCreator responseCreator = MockRestResponseCreators.withSuccess("foo", null); MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null); assertEquals(HttpStatus.OK, response.getStatusCode()); assertNull(response.getHeaders().getContentType()); assertArrayEquals("foo".getBytes(), StreamUtils.copyToByteArray(response.getBody())); }
@Test public void getResourceFromSubDirectory() { MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("")); setPathWithinHandlerMapping(exchange, "js/foo.js"); this.handler.handle(exchange).block(TIMEOUT); assertEquals(MediaType.parseMediaType("application/javascript"), exchange.getResponse().getHeaders().getContentType()); assertResponseBody(exchange, "function foo() { console.log(\"hello world\"); }"); }
@Test public void eventSourceTransport() throws Exception { EventSourceTransportHandler transportHandler = new EventSourceTransportHandler(); transportHandler.initialize(this.sockJsConfig); StreamingSockJsSession session = transportHandler.createSession("1", this.webSocketHandler, null); transportHandler.handleRequest(this.request, this.response, this.webSocketHandler, session); assertEquals("text/event-stream;charset=UTF-8", this.response.getHeaders().getContentType().toString()); assertTrue("Streaming request not started", this.servletRequest.isAsyncStarted()); verify(this.webSocketHandler).afterConnectionEstablished(session); }
@Test public void writeUTF16() throws IOException { MediaType contentType = new MediaType("application", "json", StandardCharsets.UTF_16BE); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); String body = "H\u00e9llo W\u00f6rld"; converter.write(body, contentType, outputMessage); assertEquals("Invalid result", "\"" + body + "\"", outputMessage.getBodyAsString(StandardCharsets.UTF_16BE)); assertEquals("Invalid content-type", contentType, outputMessage.getHeaders().getContentType()); }
@Test public void contentType() { Mono<ServerResponse> result = ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).build(); StepVerifier.create(result) .expectNextMatches(response -> MediaType.APPLICATION_JSON.equals(response.headers().getContentType())) .expectComplete() .verify(); }
@Test public void write() throws IOException { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); byte[] body = new byte[]{0x1, 0x2}; converter.write(body, null, outputMessage); assertArrayEquals("Invalid result", body, outputMessage.getBodyAsBytes()); assertEquals("Invalid content-type", new MediaType("application", "octet-stream"), outputMessage.getHeaders().getContentType()); assertEquals("Invalid content-length", 2, outputMessage.getHeaders().getContentLength()); }