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 authored and stsypanov committed Nov 17, 2019
1 parent 8deafd8 commit 83ade3b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
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 @@ -21,7 +21,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 @@ -37,6 +39,7 @@
import org.springframework.http.MockHttpOutputMessage;
import org.springframework.tests.XmlContent;

import static java.util.Collections.singletonMap;
import static org.assertj.core.api.Assertions.assertThat;

/**
Expand Down Expand Up @@ -106,7 +109,9 @@ public void write() throws IOException, SAXException {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
converter.write(feed, null, outputMessage);

assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(new MediaType("application", "atom+xml", StandardCharsets.UTF_8));
assertThat(outputMessage.getHeaders().getContentType())
.as("Invalid content-type")
.isEqualTo(new MediaType("application", "atom+xml", StandardCharsets.UTF_8));
String expected = "<feed xmlns=\"http://www.w3.org/2005/Atom\">" + "<title>title</title>" +
"<entry><id>id1</id><title>title1</title></entry>" +
"<entry><id>id2</id><title>title2</title></entry></feed>";
Expand All @@ -125,7 +130,24 @@ public void writeOtherCharset() throws IOException, SAXException {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
converter.write(feed, null, outputMessage);

assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(new MediaType("application", "atom+xml", Charset.forName(encoding)));
assertThat(outputMessage.getHeaders().getContentType())
.as("Invalid content-type")
.isEqualTo(new MediaType("application", "atom+xml", Charset.forName(encoding)));
}

@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");
assertThat(outputMessage.getHeaders().getContentType())
.as("Invalid content-type")
.isEqualTo(new MediaType("application", "atom+xml", expectedParameters));
}

}
Expand Up @@ -21,7 +21,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 @@ -34,6 +36,7 @@
import org.springframework.http.MockHttpOutputMessage;
import org.springframework.tests.XmlContent;

import static java.util.Collections.singletonMap;
import static org.assertj.core.api.Assertions.assertThat;

/**
Expand Down Expand Up @@ -103,7 +106,9 @@ public void write() throws IOException, SAXException {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
converter.write(channel, null, outputMessage);

assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(new MediaType("application", "rss+xml", StandardCharsets.UTF_8));
assertThat(outputMessage.getHeaders().getContentType())
.as("Invalid content-type")
.isEqualTo(new MediaType("application", "rss+xml", StandardCharsets.UTF_8));
String expected = "<rss version=\"2.0\">" +
"<channel><title>title</title><link>https://example.com</link><description>description</description>" +
"<item><title>title1</title></item>" +
Expand All @@ -129,7 +134,27 @@ public void writeOtherCharset() throws IOException, SAXException {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
converter.write(channel, null, outputMessage);

assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(new MediaType("application", "rss+xml", Charset.forName(encoding)));
assertThat(outputMessage.getHeaders().getContentType())
.as("Invalid content-type")
.isEqualTo(new MediaType("application", "rss+xml", Charset.forName(encoding)));
}

@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");
assertThat(outputMessage.getHeaders().getContentType())
.as("Invalid content-type")
.isEqualTo(new MediaType("application", "rss+xml", expectedParameters));
}

}

0 comments on commit 83ade3b

Please sign in to comment.