Skip to content

Commit

Permalink
Preserve media type parameters when setting charset
Browse files Browse the repository at this point in the history
Issue: SPR-17040
  • Loading branch information
markhobson committed Jul 14, 2018
1 parent 680afa7 commit 06f5905
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
Expand Up @@ -90,7 +90,7 @@ protected void writeInternal(T wireFeed, HttpOutputMessage outputMessage)
Charset.forName(wireFeed.getEncoding()) : DEFAULT_CHARSET);
MediaType contentType = outputMessage.getHeaders().getContentType();
if (contentType != null) {
contentType = new MediaType(contentType.getType(), contentType.getSubtype(), charset);
contentType = new MediaType(contentType, charset);
outputMessage.getHeaders().setContentType(contentType);
}

Expand Down
Expand Up @@ -24,7 +24,9 @@
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.rometools.rome.feed.atom.Entry;
import com.rometools.rome.feed.atom.Feed;
Expand All @@ -39,6 +41,8 @@
import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.MockHttpOutputMessage;

import static java.util.Collections.singletonMap;

/**
* @author Arjen Poutsma
*/
Expand Down Expand Up @@ -130,4 +134,17 @@ public void writeOtherCharset() throws IOException, SAXException {
outputMessage.getHeaders().getContentType());
}

@Test
public void writeOtherContentTypeParameters() throws IOException {
Feed feed = new Feed("atom_1.0");

MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
converter.write(feed, new MediaType("application", "atom+xml", singletonMap("type", "feed")), outputMessage);

Map<String, String> expectedParameters = new HashMap<>();
expectedParameters.put("charset", "UTF-8");
expectedParameters.put("type", "feed");
assertEquals("Invalid content-type", new MediaType("application", "atom+xml", expectedParameters),
outputMessage.getHeaders().getContentType());
}
}
Expand Up @@ -23,7 +23,9 @@
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.rometools.rome.feed.rss.Channel;
import com.rometools.rome.feed.rss.Item;
Expand All @@ -36,6 +38,8 @@
import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.MockHttpOutputMessage;

import static java.util.Collections.singletonMap;

/**
* @author Arjen Poutsma
*/
Expand Down Expand Up @@ -133,6 +137,23 @@ public void writeOtherCharset() throws IOException, SAXException {
outputMessage.getHeaders().getContentType());
}

@Test
public void writeOtherContentTypeParameters() throws IOException {
Channel channel = new Channel("rss_2.0");
channel.setTitle("title");
channel.setLink("http://example.com");
channel.setDescription("description");

MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
converter.write(channel, new MediaType("application", "rss+xml", singletonMap("x", "y")), outputMessage);

Map<String, String> expectedParameters = new HashMap<>();
expectedParameters.put("charset", "UTF-8");
expectedParameters.put("x", "y");
assertEquals("Invalid content-type", new MediaType("application", "rss+xml", expectedParameters),
outputMessage.getHeaders().getContentType());
}

private static CompareMatcher isSimilarTo(final String content) {
return CompareMatcher.isSimilarTo(content)
.ignoreWhitespace();
Expand Down

0 comments on commit 06f5905

Please sign in to comment.