diff --git a/spring-web/src/main/java/org/springframework/http/converter/BufferedImageHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/BufferedImageHttpMessageConverter.java index dc981519af..47bc0336f6 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/BufferedImageHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/BufferedImageHttpMessageConverter.java @@ -45,6 +45,7 @@ import org.springframework.http.MediaType; import org.springframework.http.StreamingHttpOutputMessage; import org.springframework.lang.Nullable; import org.springframework.util.Assert; +import org.springframework.util.StreamUtils; import org.springframework.util.StringUtils; /** @@ -207,6 +208,7 @@ public class BufferedImageHttpMessageConverter implements HttpMessageConverter Charset charset = (contentType != null && contentType.getCharset() != null ? contentType.getCharset() : DEFAULT_CHARSET); try { - Reader reader = new InputStreamReader(inputMessage.getBody(), charset); + InputStream inputStream = StreamUtils.nonClosing(inputMessage.getBody()); + Reader reader = new InputStreamReader(inputStream, charset); return (T) feedInput.build(reader); } catch (FeedException ex) { diff --git a/spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java index 9be9d73ef7..e6cca1f379 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,7 @@ package org.springframework.http.converter.json; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.Reader; @@ -354,24 +355,25 @@ public abstract class AbstractJackson2HttpMessageConverter extends AbstractGener "UTF-16".equals(charset.name()) || "UTF-32".equals(charset.name()); try { + InputStream inputStream = StreamUtils.nonClosing(inputMessage.getBody()); if (inputMessage instanceof MappingJacksonInputMessage mappingJacksonInputMessage) { Class deserializationView = mappingJacksonInputMessage.getDeserializationView(); if (deserializationView != null) { ObjectReader objectReader = objectMapper.readerWithView(deserializationView).forType(javaType); if (isUnicode) { - return objectReader.readValue(inputMessage.getBody()); + return objectReader.readValue(inputStream); } else { - Reader reader = new InputStreamReader(inputMessage.getBody(), charset); + Reader reader = new InputStreamReader(inputStream, charset); return objectReader.readValue(reader); } } } if (isUnicode) { - return objectMapper.readValue(inputMessage.getBody(), javaType); + return objectMapper.readValue(inputStream, javaType); } else { - Reader reader = new InputStreamReader(inputMessage.getBody(), charset); + Reader reader = new InputStreamReader(inputStream, charset); return objectMapper.readValue(reader, javaType); } } diff --git a/spring-web/src/main/java/org/springframework/http/converter/xml/AbstractXmlHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/xml/AbstractXmlHttpMessageConverter.java index 9da6bcb42c..19d95e4bef 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/xml/AbstractXmlHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/xml/AbstractXmlHttpMessageConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,7 @@ package org.springframework.http.converter.xml; import java.io.IOException; +import java.io.InputStream; import javax.xml.transform.Result; import javax.xml.transform.Source; @@ -33,6 +34,7 @@ import org.springframework.http.converter.AbstractHttpMessageConverter; import org.springframework.http.converter.HttpMessageConversionException; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.http.converter.HttpMessageNotWritableException; +import org.springframework.util.StreamUtils; /** * Abstract base class for {@link org.springframework.http.converter.HttpMessageConverter HttpMessageConverters} @@ -66,7 +68,8 @@ public abstract class AbstractXmlHttpMessageConverter extends AbstractHttpMes throws IOException, HttpMessageNotReadableException { try { - return readFromSource(clazz, inputMessage.getHeaders(), new StreamSource(inputMessage.getBody())); + InputStream inputStream = StreamUtils.nonClosing(inputMessage.getBody()); + return readFromSource(clazz, inputMessage.getHeaders(), new StreamSource(inputStream)); } catch (IOException | HttpMessageConversionException ex) { throw ex; diff --git a/spring-web/src/main/java/org/springframework/http/converter/xml/SourceHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/xml/SourceHttpMessageConverter.java index 3f6be8788b..3301d96027 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/xml/SourceHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/xml/SourceHttpMessageConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -148,7 +148,7 @@ public class SourceHttpMessageConverter extends AbstractHttpMe protected T readInternal(Class clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { - InputStream body = inputMessage.getBody(); + InputStream body = StreamUtils.nonClosing(inputMessage.getBody()); if (DOMSource.class == clazz) { return (T) readDOMSource(body, inputMessage); } diff --git a/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java b/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java index 75bbdd0dd7..39ccb06f8f 100644 --- a/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -158,7 +158,9 @@ public class ServletServerHttpRequest implements ServerHttpRequest { String requestContentType = this.servletRequest.getContentType(); if (StringUtils.hasLength(requestContentType)) { contentType = MediaType.parseMediaType(requestContentType); - this.headers.setContentType(contentType); + if (contentType.isConcrete()) { + this.headers.setContentType(contentType); + } } } if (contentType != null && contentType.getCharset() == null) { diff --git a/spring-web/src/test/java/org/springframework/http/converter/BufferedImageHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/BufferedImageHttpMessageConverterTests.java index b91f6b429e..79f45c1e41 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/BufferedImageHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/BufferedImageHttpMessageConverterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/spring-web/src/test/java/org/springframework/http/converter/ByteArrayHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/ByteArrayHttpMessageConverterTests.java index d868e2666e..63c9b5aa32 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/ByteArrayHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/ByteArrayHttpMessageConverterTests.java @@ -63,8 +63,10 @@ public class ByteArrayHttpMessageConverterTests { byte[] body = new byte[]{0x1, 0x2}; converter.write(body, null, outputMessage); assertThat(outputMessage.getBodyAsBytes()).as("Invalid result").isEqualTo(body); - assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(new MediaType("application", "octet-stream")); - assertThat(outputMessage.getHeaders().getContentLength()).as("Invalid content-length").isEqualTo(2); + assertThat(outputMessage.getHeaders().getContentType()) + .as("Invalid content-type").isEqualTo(MediaType.APPLICATION_OCTET_STREAM); + assertThat(outputMessage.getHeaders().getContentLength()) + .as("Invalid content-length").isEqualTo(2); } } diff --git a/spring-web/src/test/java/org/springframework/http/converter/FormHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/FormHttpMessageConverterTests.java index 9f307b4503..87608056fc 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/FormHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/FormHttpMessageConverterTests.java @@ -146,9 +146,12 @@ public class FormHttpMessageConverterTests { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); this.converter.write(body, APPLICATION_FORM_URLENCODED, outputMessage); - assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_8)).as("Invalid result").isEqualTo("name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3"); - assertThat(outputMessage.getHeaders().getContentType().toString()).as("Invalid content-type").isEqualTo("application/x-www-form-urlencoded;charset=UTF-8"); - assertThat(outputMessage.getHeaders().getContentLength()).as("Invalid content-length").isEqualTo(outputMessage.getBodyAsBytes().length); + assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_8)) + .as("Invalid result").isEqualTo("name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3"); + assertThat(outputMessage.getHeaders().getContentType().toString()) + .as("Invalid content-type").isEqualTo("application/x-www-form-urlencoded;charset=UTF-8"); + assertThat(outputMessage.getHeaders().getContentLength()) + .as("Invalid content-length").isEqualTo(outputMessage.getBodyAsBytes().length); } @Test diff --git a/spring-web/src/test/java/org/springframework/http/converter/ResourceHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/ResourceHttpMessageConverterTests.java index 3c98fe538d..3bf1b9d3e3 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/ResourceHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/ResourceHttpMessageConverterTests.java @@ -106,8 +106,10 @@ public class ResourceHttpMessageConverterTests { Resource body = new ClassPathResource("logo.jpg", getClass()); converter.write(body, null, outputMessage); - assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(MediaType.IMAGE_JPEG); - assertThat(outputMessage.getHeaders().getContentLength()).as("Invalid content-length").isEqualTo(body.getFile().length()); + assertThat(outputMessage.getHeaders().getContentType()) + .as("Invalid content-type").isEqualTo(MediaType.IMAGE_JPEG); + assertThat(outputMessage.getHeaders().getContentLength()) + .as("Invalid content-length").isEqualTo(body.getFile().length()); } @Test // SPR-10848 diff --git a/spring-web/src/test/java/org/springframework/http/converter/feed/AtomFeedHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/feed/AtomFeedHttpMessageConverterTests.java index 856dcd7870..05e09b3700 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/feed/AtomFeedHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/feed/AtomFeedHttpMessageConverterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -71,8 +71,8 @@ public class AtomFeedHttpMessageConverterTests { @Test public void read() throws IOException { - InputStream is = getClass().getResourceAsStream("atom.xml"); - MockHttpInputMessage inputMessage = new MockHttpInputMessage(is); + InputStream inputStream = getClass().getResourceAsStream("atom.xml"); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(inputStream); inputMessage.getHeaders().setContentType(ATOM_XML_UTF8); Feed result = converter.read(Feed.class, inputMessage); assertThat(result.getTitle()).isEqualTo("title"); diff --git a/spring-web/src/test/java/org/springframework/http/converter/feed/RssChannelHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/feed/RssChannelHttpMessageConverterTests.java index 5d1e01f328..72dda284ef 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/feed/RssChannelHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/feed/RssChannelHttpMessageConverterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -56,8 +56,8 @@ public class RssChannelHttpMessageConverterTests { @Test public void read() throws IOException { - InputStream is = getClass().getResourceAsStream("rss.xml"); - MockHttpInputMessage inputMessage = new MockHttpInputMessage(is); + InputStream inputStream = getClass().getResourceAsStream("rss.xml"); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(inputStream); inputMessage.getHeaders().setContentType(RSS_XML_UTF8); Channel result = converter.read(Channel.class, inputMessage); assertThat(result.getTitle()).isEqualTo("title"); diff --git a/spring-web/src/test/java/org/springframework/http/converter/json/GsonHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/json/GsonHttpMessageConverterTests.java index aa2c777ae4..29cca1deef 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/json/GsonHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/json/GsonHttpMessageConverterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -72,7 +72,7 @@ public class GsonHttpMessageConverterTests { public void readTyped() throws IOException { String body = "{\"bytes\":[1,2],\"array\":[\"Foo\",\"Bar\"]," + "\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8)); inputMessage.getHeaders().setContentType(new MediaType("application", "json")); MyBean result = (MyBean) this.converter.read(MyBean.class, inputMessage); @@ -131,7 +131,8 @@ public class GsonHttpMessageConverterTests { assertThat(result.contains("\"array\":[\"Foo\",\"Bar\"]")).isTrue(); assertThat(result.contains("\"bool\":true")).isTrue(); assertThat(result.contains("\"bytes\":[1,2]")).isTrue(); - assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(new MediaType("application", "json", utf8)); + assertThat(outputMessage.getHeaders().getContentType()) + .as("Invalid content-type").isEqualTo(new MediaType("application", "json", utf8)); } @Test @@ -153,7 +154,8 @@ public class GsonHttpMessageConverterTests { assertThat(result.contains("\"array\":[\"Foo\",\"Bar\"]")).isTrue(); assertThat(result.contains("\"bool\":true")).isTrue(); assertThat(result.contains("\"bytes\":[1,2]")).isTrue(); - assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(new MediaType("application", "json", utf8)); + assertThat(outputMessage.getHeaders().getContentType()) + .as("Invalid content-type").isEqualTo(new MediaType("application", "json", utf8)); } @Test @@ -169,7 +171,7 @@ public class GsonHttpMessageConverterTests { @Test public void readInvalidJson() throws IOException { String body = "FooBar"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8)); inputMessage.getHeaders().setContentType(new MediaType("application", "json")); assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> this.converter.read(MyBean.class, inputMessage)); diff --git a/spring-web/src/test/java/org/springframework/http/converter/json/JsonbHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/json/JsonbHttpMessageConverterTests.java index 44e14ac576..df3052bbae 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/json/JsonbHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/json/JsonbHttpMessageConverterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -131,7 +131,8 @@ public class JsonbHttpMessageConverterTests { assertThat(result.contains("\"array\":[\"Foo\",\"Bar\"]")).isTrue(); assertThat(result.contains("\"bool\":true")).isTrue(); assertThat(result.contains("\"bytes\":[1,2]")).isTrue(); - assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(new MediaType("application", "json", utf8)); + assertThat(outputMessage.getHeaders().getContentType()) + .as("Invalid content-type").isEqualTo(new MediaType("application", "json", utf8)); } @Test @@ -153,7 +154,8 @@ public class JsonbHttpMessageConverterTests { assertThat(result.contains("\"array\":[\"Foo\",\"Bar\"]")).isTrue(); assertThat(result.contains("\"bool\":true")).isTrue(); assertThat(result.contains("\"bytes\":[1,2]")).isTrue(); - assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(new MediaType("application", "json", utf8)); + assertThat(outputMessage.getHeaders().getContentType()) + .as("Invalid content-type").isEqualTo(new MediaType("application", "json", utf8)); } @Test diff --git a/spring-web/src/test/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverterTests.java index cef67ddb80..76358796ea 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -132,7 +132,7 @@ public class MappingJackson2HttpMessageConverterTests { "\"string\":\"Foo\"," + "\"bool\":true," + "\"fraction\":42.0}"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8)); inputMessage.getHeaders().setContentType(new MediaType("application", "json")); MyBean result = (MyBean) converter.read(MyBean.class, inputMessage); assertThat(result.getString()).isEqualTo("Foo"); @@ -153,7 +153,7 @@ public class MappingJackson2HttpMessageConverterTests { "\"string\":\"Foo\"," + "\"bool\":true," + "\"fraction\":42.0}"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8)); inputMessage.getHeaders().setContentType(new MediaType("application", "json")); HashMap result = (HashMap) converter.read(HashMap.class, inputMessage); assertThat(result.get("string")).isEqualTo("Foo"); @@ -185,7 +185,8 @@ public class MappingJackson2HttpMessageConverterTests { assertThat(result.contains("\"array\":[\"Foo\",\"Bar\"]")).isTrue(); assertThat(result.contains("\"bool\":true")).isTrue(); assertThat(result.contains("\"bytes\":\"AQI=\"")).isTrue(); - assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(MediaType.APPLICATION_JSON); + assertThat(outputMessage.getHeaders().getContentType()) + .as("Invalid content-type").isEqualTo(MediaType.APPLICATION_JSON); } @Test @@ -206,7 +207,8 @@ public class MappingJackson2HttpMessageConverterTests { assertThat(result.contains("\"array\":[\"Foo\",\"Bar\"]")).isTrue(); assertThat(result.contains("\"bool\":true")).isTrue(); assertThat(result.contains("\"bytes\":\"AQI=\"")).isTrue(); - assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(MediaType.APPLICATION_JSON); + assertThat(outputMessage.getHeaders().getContentType()) + .as("Invalid content-type").isEqualTo(MediaType.APPLICATION_JSON); } @Test @@ -222,16 +224,16 @@ public class MappingJackson2HttpMessageConverterTests { @Test public void readInvalidJson() throws IOException { String body = "FooBar"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8)); inputMessage.getHeaders().setContentType(new MediaType("application", "json")); - assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> - converter.read(MyBean.class, inputMessage)); + assertThatExceptionOfType(HttpMessageNotReadableException.class) + .isThrownBy(() -> converter.read(MyBean.class, inputMessage)); } @Test public void readValidJsonWithUnknownProperty() throws IOException { String body = "{\"string\":\"string\",\"unknownProperty\":\"value\"}"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8)); inputMessage.getHeaders().setContentType(new MediaType("application", "json")); converter.read(MyBean.class, inputMessage); // Assert no HttpMessageNotReadableException is thrown @@ -258,7 +260,7 @@ public class MappingJackson2HttpMessageConverterTests { "\"string\":\"Foo\"," + "\"bool\":true," + "\"fraction\":42.0}]"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8)); inputMessage.getHeaders().setContentType(new MediaType("application", "json")); List results = (List) converter.read(List.class, inputMessage); @@ -272,7 +274,7 @@ public class MappingJackson2HttpMessageConverterTests { assertThat(result.getBytes()).isEqualTo(new byte[] {0x1, 0x2}); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); - converter.write(results, new MediaType("application", "json"), outputMessage); + converter.write(results, MediaType.APPLICATION_JSON, outputMessage); JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true); } @@ -288,8 +290,8 @@ public class MappingJackson2HttpMessageConverterTests { "\"string\":\"Foo\"," + "\"bool\":true," + "\"fraction\":42.0}]"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); - inputMessage.getHeaders().setContentType(new MediaType("application", "json")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8)); + inputMessage.getHeaders().setContentType(MediaType.APPLICATION_JSON); MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); List results = (List) converter.read(beansList.getType(), null, inputMessage); @@ -303,7 +305,7 @@ public class MappingJackson2HttpMessageConverterTests { assertThat(result.getBytes()).isEqualTo(new byte[] {0x1, 0x2}); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); - converter.write(results, beansList.getType(), new MediaType("application", "json"), outputMessage); + converter.write(results, beansList.getType(), MediaType.APPLICATION_JSON, outputMessage); JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true); } @@ -320,8 +322,8 @@ public class MappingJackson2HttpMessageConverterTests { "\"string\":\"Foo\"," + "\"bool\":true," + "\"fraction\":42.0}]"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); - inputMessage.getHeaders().setContentType(new MediaType("application", "json")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8)); + inputMessage.getHeaders().setContentType(MediaType.APPLICATION_JSON); MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); List results = (List) converter.read(beansList.getType(), null, inputMessage); @@ -335,7 +337,7 @@ public class MappingJackson2HttpMessageConverterTests { assertThat(result.getBytes()).isEqualTo(new byte[] {0x1, 0x2}); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); - converter.write(results, baseList.getType(), new MediaType("application", "json"), outputMessage); + converter.write(results, baseList.getType(), MediaType.APPLICATION_JSON, outputMessage); JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true); } @@ -481,7 +483,7 @@ public class MappingJackson2HttpMessageConverterTests { public void readWithNoDefaultConstructor() throws Exception { String body = "{\"property1\":\"foo\",\"property2\":\"bar\"}"; MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8)); - inputMessage.getHeaders().setContentType(new MediaType("application", "json")); + inputMessage.getHeaders().setContentType(MediaType.APPLICATION_JSON); assertThatExceptionOfType(HttpMessageConversionException.class).isThrownBy(() -> converter.read(BeanWithNoDefaultConstructor.class, inputMessage)) .withMessageStartingWith("Type definition error:"); diff --git a/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverterTests.java index 92705a4dcb..7e03d05da8 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverterTests.java @@ -17,7 +17,6 @@ package org.springframework.http.converter.protobuf; import java.io.IOException; -import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import com.google.protobuf.ExtensionRegistry; @@ -128,7 +127,7 @@ public class ProtobufHttpMessageConverterTests { assertThat(outputMessage.getHeaders().getContentType()).isEqualTo(contentType); - final String body = outputMessage.getBodyAsString(Charset.forName("UTF-8")); + final String body = outputMessage.getBodyAsString(StandardCharsets.UTF_8); assertThat(body.isEmpty()).as("body is empty").isFalse(); Msg.Builder builder = Msg.newBuilder(); @@ -167,8 +166,9 @@ public class ProtobufHttpMessageConverterTests { } @Test - public void defaultContentType() { - assertThat(this.converter.getDefaultContentType(this.testMsg)).isEqualTo(ProtobufHttpMessageConverter.PROTOBUF); + public void defaultContentType() throws Exception { + assertThat(this.converter.getDefaultContentType(this.testMsg)) + .isEqualTo(ProtobufHttpMessageConverter.PROTOBUF); } @Test diff --git a/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufJsonFormatHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufJsonFormatHttpMessageConverterTests.java index a2d549daab..bafe9aa290 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufJsonFormatHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufJsonFormatHttpMessageConverterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/spring-web/src/test/java/org/springframework/http/converter/smile/MappingJackson2SmileHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/smile/MappingJackson2SmileHttpMessageConverterTests.java index 2cbb22bdef..f11d4b015e 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/smile/MappingJackson2SmileHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/smile/MappingJackson2SmileHttpMessageConverterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -87,7 +87,8 @@ public class MappingJackson2SmileHttpMessageConverterTests { body.setBytes(new byte[]{0x1, 0x2}); converter.write(body, null, outputMessage); assertThat(outputMessage.getBodyAsBytes()).isEqualTo(mapper.writeValueAsBytes(body)); - assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(new MediaType("application", "x-jackson-smile")); + assertThat(outputMessage.getHeaders().getContentType()) + .as("Invalid content-type").isEqualTo(new MediaType("application", "x-jackson-smile")); } diff --git a/spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2CollectionHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2CollectionHttpMessageConverterTests.java index c3c176bea6..c12f97c05b 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2CollectionHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2CollectionHttpMessageConverterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,7 @@ package org.springframework.http.converter.xml; import java.lang.reflect.Type; +import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.List; import java.util.Set; @@ -79,7 +80,7 @@ public class Jaxb2CollectionHttpMessageConverterTests { @SuppressWarnings("unchecked") public void readXmlRootElementList() throws Exception { String content = ""; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8)); List result = (List) converter.read(rootElementListType, null, inputMessage); assertThat(result.size()).as("Invalid result").isEqualTo(2); @@ -91,7 +92,7 @@ public class Jaxb2CollectionHttpMessageConverterTests { @SuppressWarnings("unchecked") public void readXmlRootElementSet() throws Exception { String content = ""; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8)); Set result = (Set) converter.read(rootElementSetType, null, inputMessage); assertThat(result.size()).as("Invalid result").isEqualTo(2); @@ -103,7 +104,7 @@ public class Jaxb2CollectionHttpMessageConverterTests { @SuppressWarnings("unchecked") public void readXmlTypeList() throws Exception { String content = ""; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8)); List result = (List) converter.read(typeListType, null, inputMessage); assertThat(result.size()).as("Invalid result").isEqualTo(2); @@ -115,7 +116,7 @@ public class Jaxb2CollectionHttpMessageConverterTests { @SuppressWarnings("unchecked") public void readXmlTypeSet() throws Exception { String content = ""; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8)); Set result = (Set) converter.read(typeSetType, null, inputMessage); assertThat(result.size()).as("Invalid result").isEqualTo(2); @@ -131,7 +132,7 @@ public class Jaxb2CollectionHttpMessageConverterTests { " \n" + " ]>" + " &ext;"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8)); converter = new Jaxb2CollectionHttpMessageConverter>() { @Override @@ -160,7 +161,7 @@ public class Jaxb2CollectionHttpMessageConverterTests { " \n" + " ]>" + " &ext;"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8)); Jaxb2CollectionHttpMessageConverter c = new Jaxb2CollectionHttpMessageConverter>() { @Override @@ -195,10 +196,10 @@ public class Jaxb2CollectionHttpMessageConverterTests { " \n" + "]>\n" + "&lol9;"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8")); - assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> - this.converter.read(this.rootElementListType, null, inputMessage)) - .withMessageContaining("\"lol9\""); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8)); + assertThatExceptionOfType(HttpMessageNotReadableException.class) + .isThrownBy(() -> this.converter.read(this.rootElementListType, null, inputMessage)) + .withMessageContaining("\"lol9\""); } diff --git a/spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverterTests.java index dc210e628d..614711c1ca 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -79,21 +79,27 @@ public class Jaxb2RootElementHttpMessageConverterTests { @Test public void canRead() { - assertThat(converter.canRead(RootElement.class, null)).as("Converter does not support reading @XmlRootElement").isTrue(); - assertThat(converter.canRead(Type.class, null)).as("Converter does not support reading @XmlType").isTrue(); + assertThat(converter.canRead(RootElement.class, null)) + .as("Converter does not support reading @XmlRootElement").isTrue(); + assertThat(converter.canRead(Type.class, null)) + .as("Converter does not support reading @XmlType").isTrue(); } @Test public void canWrite() { - assertThat(converter.canWrite(RootElement.class, null)).as("Converter does not support writing @XmlRootElement").isTrue(); - assertThat(converter.canWrite(RootElementSubclass.class, null)).as("Converter does not support writing @XmlRootElement subclass").isTrue(); - assertThat(converter.canWrite(rootElementCglib.getClass(), null)).as("Converter does not support writing @XmlRootElement subclass").isTrue(); - assertThat(converter.canWrite(Type.class, null)).as("Converter supports writing @XmlType").isFalse(); + assertThat(converter.canWrite(RootElement.class, null)) + .as("Converter does not support writing @XmlRootElement").isTrue(); + assertThat(converter.canWrite(RootElementSubclass.class, null)) + .as("Converter does not support writing @XmlRootElement subclass").isTrue(); + assertThat(converter.canWrite(rootElementCglib.getClass(), null)) + .as("Converter does not support writing @XmlRootElement subclass").isTrue(); + assertThat(converter.canWrite(Type.class, null)) + .as("Converter supports writing @XmlType").isFalse(); } @Test public void readXmlRootElement() throws Exception { - byte[] body = "".getBytes("UTF-8"); + byte[] body = "".getBytes(StandardCharsets.UTF_8); MockHttpInputMessage inputMessage = new MockHttpInputMessage(body); RootElement result = (RootElement) converter.read(RootElement.class, inputMessage); assertThat(result.type.s).as("Invalid result").isEqualTo("Hello World"); @@ -101,7 +107,7 @@ public class Jaxb2RootElementHttpMessageConverterTests { @Test public void readXmlRootElementSubclass() throws Exception { - byte[] body = "".getBytes("UTF-8"); + byte[] body = "".getBytes(StandardCharsets.UTF_8); MockHttpInputMessage inputMessage = new MockHttpInputMessage(body); RootElementSubclass result = (RootElementSubclass) converter.read(RootElementSubclass.class, inputMessage); assertThat(result.getType().s).as("Invalid result").isEqualTo("Hello World"); @@ -109,7 +115,7 @@ public class Jaxb2RootElementHttpMessageConverterTests { @Test public void readXmlType() throws Exception { - byte[] body = "".getBytes("UTF-8"); + byte[] body = "".getBytes(StandardCharsets.UTF_8); MockHttpInputMessage inputMessage = new MockHttpInputMessage(body); Type result = (Type) converter.read(Type.class, inputMessage); assertThat(result.s).as("Invalid result").isEqualTo("Hello World"); @@ -122,7 +128,7 @@ public class Jaxb2RootElementHttpMessageConverterTests { " \n" + " ]>" + " &ext;"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8)); converter.setSupportDtd(true); RootElement rootElement = (RootElement) converter.read(RootElement.class, inputMessage); @@ -136,7 +142,7 @@ public class Jaxb2RootElementHttpMessageConverterTests { " \n" + " ]>" + " &ext;"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8)); this.converter.setProcessExternalEntities(true); RootElement rootElement = (RootElement) converter.read(RootElement.class, inputMessage); @@ -162,7 +168,7 @@ public class Jaxb2RootElementHttpMessageConverterTests { " \n" + "]>\n" + "&lol9;"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8)); assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> this.converter.read(RootElement.class, inputMessage)) .withMessageContaining("DOCTYPE"); @@ -172,7 +178,8 @@ public class Jaxb2RootElementHttpMessageConverterTests { public void writeXmlRootElement() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); converter.write(rootElement, null, outputMessage); - assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(new MediaType("application", "xml")); + assertThat(outputMessage.getHeaders().getContentType()) + .as("Invalid content-type").isEqualTo(MediaType.APPLICATION_XML); DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE)); assertThat(XmlContent.of(outputMessage.getBodyAsString(StandardCharsets.UTF_8))) .isSimilarTo("", ev); @@ -182,7 +189,8 @@ public class Jaxb2RootElementHttpMessageConverterTests { public void writeXmlRootElementSubclass() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); converter.write(rootElementCglib, null, outputMessage); - assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(new MediaType("application", "xml")); + assertThat(outputMessage.getHeaders().getContentType()) + .as("Invalid content-type").isEqualTo(MediaType.APPLICATION_XML); DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE)); assertThat(XmlContent.of(outputMessage.getBodyAsString(StandardCharsets.UTF_8))) .isSimilarTo("", ev); @@ -202,7 +210,7 @@ public class Jaxb2RootElementHttpMessageConverterTests { @Test public void customizeUnmarshaller() throws Exception { - byte[] body = "a|||b".getBytes("UTF-8"); + byte[] body = "a|||b".getBytes(StandardCharsets.UTF_8); MyJaxb2RootElementHttpMessageConverter myConverter = new MyJaxb2RootElementHttpMessageConverter(); MockHttpInputMessage inputMessage = new MockHttpInputMessage(body); MyRootElement result = (MyRootElement) myConverter.read(MyRootElement.class, inputMessage); diff --git a/spring-web/src/test/java/org/springframework/http/converter/xml/MappingJackson2XmlHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/xml/MappingJackson2XmlHttpMessageConverterTests.java index ab8c617b6f..5681fe383a 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/xml/MappingJackson2XmlHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/xml/MappingJackson2XmlHttpMessageConverterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -74,7 +74,7 @@ public class MappingJackson2XmlHttpMessageConverterTests { "Bar" + "true" + "AQI="; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8)); inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); MyBean result = (MyBean) converter.read(MyBean.class, inputMessage); assertThat(result.getString()).isEqualTo("Foo"); @@ -103,14 +103,15 @@ public class MappingJackson2XmlHttpMessageConverterTests { assertThat(result.contains("FooBar")).isTrue(); assertThat(result.contains("true")).isTrue(); assertThat(result.contains("AQI=")).isTrue(); - assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(new MediaType("application", "xml", StandardCharsets.UTF_8)); + assertThat(outputMessage.getHeaders().getContentType()) + .as("Invalid content-type").isEqualTo(new MediaType("application", "xml", StandardCharsets.UTF_8)); } @Test public void readInvalidXml() throws IOException { String body = "FooBar"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); - inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8)); + inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML); assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> converter.read(MyBean.class, inputMessage)); } @@ -118,8 +119,8 @@ public class MappingJackson2XmlHttpMessageConverterTests { @Test public void readValidXmlWithUnknownProperty() throws IOException { String body = "stringvalue"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); - inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8)); + inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML); converter.read(MyBean.class, inputMessage); // Assert no HttpMessageNotReadableException is thrown } @@ -156,8 +157,8 @@ public class MappingJackson2XmlHttpMessageConverterTests { new ClassPathResource("external.txt", getClass()).getURI() + "\" >]>&ext;"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); - inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8)); + inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML); assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> this.converter.read(MyBean.class, inputMessage)); @@ -183,8 +184,8 @@ public class MappingJackson2XmlHttpMessageConverterTests { "]>\n" + "&lol9;"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); - inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8)); + inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML); assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> this.converter.read(MyBean.class, inputMessage)); diff --git a/spring-web/src/test/java/org/springframework/http/converter/xml/MarshallingHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/xml/MarshallingHttpMessageConverterTests.java index b3f95c9c81..888ad9b44b 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/xml/MarshallingHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/xml/MarshallingHttpMessageConverterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,8 @@ package org.springframework.http.converter.xml; +import java.nio.charset.StandardCharsets; + import javax.xml.transform.Result; import javax.xml.transform.stream.StreamSource; @@ -81,7 +83,7 @@ public class MarshallingHttpMessageConverterTests { @Test public void read() throws Exception { String body = "Hello World"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8)); Unmarshaller unmarshaller = mock(Unmarshaller.class); given(unmarshaller.unmarshal(isA(StreamSource.class))).willReturn(body); @@ -99,12 +101,12 @@ public class MarshallingHttpMessageConverterTests { Marshaller marshaller = mock(Marshaller.class); Unmarshaller unmarshaller = mock(Unmarshaller.class); - given(unmarshaller.unmarshal(isA(StreamSource.class))).willReturn(Integer.valueOf(3)); + given(unmarshaller.unmarshal(isA(StreamSource.class))).willReturn(3); MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller, unmarshaller); - assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> - converter.read(String.class, inputMessage)) - .withCauseInstanceOf(TypeMismatchException.class); + assertThatExceptionOfType(HttpMessageNotReadableException.class) + .isThrownBy(() -> converter.read(String.class, inputMessage)) + .withCauseInstanceOf(TypeMismatchException.class); } @Test @@ -118,9 +120,8 @@ public class MarshallingHttpMessageConverterTests { MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(); converter.setUnmarshaller(unmarshaller); - assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> - converter.read(Object.class, inputMessage)) - .withCause(ex); + assertThatExceptionOfType(HttpMessageNotReadableException.class) + .isThrownBy(() -> converter.read(Object.class, inputMessage)).withCause(ex); } @Test @@ -134,7 +135,8 @@ public class MarshallingHttpMessageConverterTests { MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller); converter.write(body, null, outputMessage); - assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(new MediaType("application", "xml")); + assertThat(outputMessage.getHeaders().getContentType()) + .as("Invalid content-type").isEqualTo(new MediaType("application", "xml")); } @Test @@ -147,13 +149,12 @@ public class MarshallingHttpMessageConverterTests { willThrow(ex).given(marshaller).marshal(eq(body), isA(Result.class)); MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller); - assertThatExceptionOfType(HttpMessageNotWritableException.class).isThrownBy(() -> - converter.write(body, null, outputMessage)) - .withCause(ex); + assertThatExceptionOfType(HttpMessageNotWritableException.class) + .isThrownBy(() -> converter.write(body, null, outputMessage)).withCause(ex); } @Test - public void supports() throws Exception { + public void supports() { assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> new MarshallingHttpMessageConverter().supports(Object.class)); } diff --git a/spring-web/src/test/java/org/springframework/http/converter/xml/SourceHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/xml/SourceHttpMessageConverterTests.java index a54e6895ef..6de216b504 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/xml/SourceHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/xml/SourceHttpMessageConverterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -90,8 +90,8 @@ public class SourceHttpMessageConverterTests { @Test public void readDOMSource() throws Exception { - MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes("UTF-8")); - inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes(StandardCharsets.UTF_8)); + inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML); DOMSource result = (DOMSource) converter.read(DOMSource.class, inputMessage); Document document = (Document) result.getNode(); assertThat(document.getDocumentElement().getLocalName()).as("Invalid result").isEqualTo("root"); @@ -99,8 +99,8 @@ public class SourceHttpMessageConverterTests { @Test public void readDOMSourceExternal() throws Exception { - MockHttpInputMessage inputMessage = new MockHttpInputMessage(bodyExternal.getBytes("UTF-8")); - inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(bodyExternal.getBytes(StandardCharsets.UTF_8)); + inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML); converter.setSupportDtd(true); DOMSource result = (DOMSource) converter.read(DOMSource.class, inputMessage); Document document = (Document) result.getNode(); @@ -127,7 +127,7 @@ public class SourceHttpMessageConverterTests { " \n" + "]>\n" + "&lol9;"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8)); assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> this.converter.read(DOMSource.class, inputMessage)) @@ -136,8 +136,8 @@ public class SourceHttpMessageConverterTests { @Test public void readSAXSource() throws Exception { - MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes("UTF-8")); - inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes(StandardCharsets.UTF_8)); + inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML); SAXSource result = (SAXSource) converter.read(SAXSource.class, inputMessage); InputSource inputSource = result.getInputSource(); String s = FileCopyUtils.copyToString(new InputStreamReader(inputSource.getByteStream())); @@ -146,8 +146,8 @@ public class SourceHttpMessageConverterTests { @Test public void readSAXSourceExternal() throws Exception { - MockHttpInputMessage inputMessage = new MockHttpInputMessage(bodyExternal.getBytes("UTF-8")); - inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(bodyExternal.getBytes(StandardCharsets.UTF_8)); + inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML); converter.setSupportDtd(true); SAXSource result = (SAXSource) converter.read(SAXSource.class, inputMessage); InputSource inputSource = result.getInputSource(); @@ -182,20 +182,19 @@ public class SourceHttpMessageConverterTests { "]>\n" + "&lol9;"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8)); SAXSource result = (SAXSource) this.converter.read(SAXSource.class, inputMessage); InputSource inputSource = result.getInputSource(); XMLReader reader = result.getXMLReader(); - assertThatExceptionOfType(SAXException.class).isThrownBy(() -> - reader.parse(inputSource)) - .withMessageContaining("DOCTYPE"); + assertThatExceptionOfType(SAXException.class) + .isThrownBy(() -> reader.parse(inputSource)).withMessageContaining("DOCTYPE"); } @Test public void readStAXSource() throws Exception { - MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes("UTF-8")); - inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes(StandardCharsets.UTF_8)); + inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML); StAXSource result = (StAXSource) converter.read(StAXSource.class, inputMessage); XMLStreamReader streamReader = result.getXMLStreamReader(); assertThat(streamReader.hasNext()).isTrue(); @@ -209,8 +208,8 @@ public class SourceHttpMessageConverterTests { @Test public void readStAXSourceExternal() throws Exception { - MockHttpInputMessage inputMessage = new MockHttpInputMessage(bodyExternal.getBytes("UTF-8")); - inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(bodyExternal.getBytes(StandardCharsets.UTF_8)); + inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML); converter.setSupportDtd(true); StAXSource result = (StAXSource) converter.read(StAXSource.class, inputMessage); XMLStreamReader streamReader = result.getXMLStreamReader(); @@ -248,7 +247,7 @@ public class SourceHttpMessageConverterTests { " \n" + "]>\n" + "&lol9;"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8)); StAXSource result = (StAXSource) this.converter.read(StAXSource.class, inputMessage); XMLStreamReader streamReader = result.getXMLStreamReader(); @@ -257,15 +256,14 @@ public class SourceHttpMessageConverterTests { streamReader.next(); String s = streamReader.getLocalName(); assertThat(s).isEqualTo("root"); - assertThatExceptionOfType(XMLStreamException.class).isThrownBy(() -> - streamReader.getElementText()) - .withMessageContaining("\"lol9\""); + assertThatExceptionOfType(XMLStreamException.class) + .isThrownBy(streamReader::getElementText).withMessageContaining("\"lol9\""); } @Test public void readStreamSource() throws Exception { - MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes("UTF-8")); - inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes(StandardCharsets.UTF_8)); + inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML); StreamSource result = (StreamSource) converter.read(StreamSource.class, inputMessage); String s = FileCopyUtils.copyToString(new InputStreamReader(result.getInputStream())); assertThat(XmlContent.of(s)).isSimilarTo(BODY); @@ -273,8 +271,8 @@ public class SourceHttpMessageConverterTests { @Test public void readSource() throws Exception { - MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes("UTF-8")); - inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes(StandardCharsets.UTF_8)); + inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML); converter.read(Source.class, inputMessage); } @@ -292,8 +290,10 @@ public class SourceHttpMessageConverterTests { converter.write(domSource, null, outputMessage); assertThat(XmlContent.of(outputMessage.getBodyAsString(StandardCharsets.UTF_8))) .isSimilarTo("Hello World"); - assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(new MediaType("application", "xml")); - assertThat(outputMessage.getHeaders().getContentLength()).as("Invalid content-length").isEqualTo(outputMessage.getBodyAsBytes().length); + assertThat(outputMessage.getHeaders().getContentType()) + .as("Invalid content-type").isEqualTo(MediaType.APPLICATION_XML); + assertThat(outputMessage.getHeaders().getContentLength()) + .as("Invalid content-length").isEqualTo(outputMessage.getBodyAsBytes().length); } @Test @@ -305,7 +305,8 @@ public class SourceHttpMessageConverterTests { converter.write(saxSource, null, outputMessage); assertThat(XmlContent.of(outputMessage.getBodyAsString(StandardCharsets.UTF_8))) .isSimilarTo("Hello World"); - assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(new MediaType("application", "xml")); + assertThat(outputMessage.getHeaders().getContentType()) + .as("Invalid content-type").isEqualTo(MediaType.APPLICATION_XML); } @Test @@ -317,7 +318,8 @@ public class SourceHttpMessageConverterTests { converter.write(streamSource, null, outputMessage); assertThat(XmlContent.of(outputMessage.getBodyAsString(StandardCharsets.UTF_8))) .isSimilarTo("Hello World"); - assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(new MediaType("application", "xml")); + assertThat(outputMessage.getHeaders().getContentType()) + .as("Invalid content-type").isEqualTo(MediaType.APPLICATION_XML); } } diff --git a/spring-web/src/test/java/org/springframework/http/server/ServletServerHttpRequestTests.java b/spring-web/src/test/java/org/springframework/http/server/ServletServerHttpRequestTests.java index de0779a4c3..6b24f91613 100644 --- a/spring-web/src/test/java/org/springframework/http/server/ServletServerHttpRequestTests.java +++ b/spring-web/src/test/java/org/springframework/http/server/ServletServerHttpRequestTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -151,6 +151,13 @@ public class ServletServerHttpRequestTests { assertThat(headers.getContentType()).isNull(); } + @Test // gh-27957 + void getHeadersWithWildcardContentType() { + mockRequest.setContentType("*/*"); + mockRequest.removeHeader("Content-Type"); + assertThat(request.getHeaders()).as("Invalid content-type should not raise exception").hasSize(0); + } + @Test void getBody() throws IOException { byte[] content = "Hello World".getBytes(StandardCharsets.UTF_8);