Browse Source

Merge pull request #1885

Closes gh-1885
pull/24047/head
Rossen Stoyanchev 6 years ago
parent
commit
9ec67bac82
  1. 4
      spring-web/src/main/java/org/springframework/http/converter/feed/AbstractWireFeedHttpMessageConverter.java
  2. 41
      spring-web/src/test/java/org/springframework/http/converter/feed/AtomFeedHttpMessageConverterTests.java
  3. 50
      spring-web/src/test/java/org/springframework/http/converter/feed/RssChannelHttpMessageConverterTests.java

4
spring-web/src/main/java/org/springframework/http/converter/feed/AbstractWireFeedHttpMessageConverter.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -90,7 +90,7 @@ public abstract class AbstractWireFeedHttpMessageConverter<T extends WireFeed>
Charset.forName(wireFeed.getEncoding()) : DEFAULT_CHARSET); Charset.forName(wireFeed.getEncoding()) : DEFAULT_CHARSET);
MediaType contentType = outputMessage.getHeaders().getContentType(); MediaType contentType = outputMessage.getHeaders().getContentType();
if (contentType != null) { if (contentType != null) {
contentType = new MediaType(contentType.getType(), contentType.getSubtype(), charset); contentType = new MediaType(contentType, charset);
outputMessage.getHeaders().setContentType(contentType); outputMessage.getHeaders().setContentType(contentType);
} }

41
spring-web/src/test/java/org/springframework/http/converter/feed/AtomFeedHttpMessageConverterTests.java

@ -27,7 +27,6 @@ import com.rometools.rome.feed.atom.Entry;
import com.rometools.rome.feed.atom.Feed; import com.rometools.rome.feed.atom.Feed;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.xml.sax.SAXException;
import org.xmlunit.diff.DefaultNodeMatcher; import org.xmlunit.diff.DefaultNodeMatcher;
import org.xmlunit.diff.ElementSelectors; import org.xmlunit.diff.ElementSelectors;
import org.xmlunit.diff.NodeMatcher; import org.xmlunit.diff.NodeMatcher;
@ -37,6 +36,7 @@ import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.MockHttpOutputMessage; import org.springframework.http.MockHttpOutputMessage;
import org.springframework.tests.XmlContent; import org.springframework.tests.XmlContent;
import static java.util.Collections.singletonMap;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/** /**
@ -44,6 +44,10 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
public class AtomFeedHttpMessageConverterTests { public class AtomFeedHttpMessageConverterTests {
private static final MediaType ATOM_XML_UTF8 =
new MediaType(MediaType.APPLICATION_ATOM_XML, StandardCharsets.UTF_8);
private AtomFeedHttpMessageConverter converter; private AtomFeedHttpMessageConverter converter;
@ -55,21 +59,21 @@ public class AtomFeedHttpMessageConverterTests {
@Test @Test
public void canRead() { public void canRead() {
assertThat(converter.canRead(Feed.class, new MediaType("application", "atom+xml"))).isTrue(); assertThat(converter.canRead(Feed.class, MediaType.APPLICATION_ATOM_XML)).isTrue();
assertThat(converter.canRead(Feed.class, new MediaType("application", "atom+xml", StandardCharsets.UTF_8))).isTrue(); assertThat(converter.canRead(Feed.class, ATOM_XML_UTF8)).isTrue();
} }
@Test @Test
public void canWrite() { public void canWrite() {
assertThat(converter.canWrite(Feed.class, new MediaType("application", "atom+xml"))).isTrue(); assertThat(converter.canWrite(Feed.class, MediaType.APPLICATION_ATOM_XML)).isTrue();
assertThat(converter.canWrite(Feed.class, new MediaType("application", "atom+xml", StandardCharsets.UTF_8))).isTrue(); assertThat(converter.canWrite(Feed.class, ATOM_XML_UTF8)).isTrue();
} }
@Test @Test
public void read() throws IOException { public void read() throws IOException {
InputStream is = getClass().getResourceAsStream("atom.xml"); InputStream is = getClass().getResourceAsStream("atom.xml");
MockHttpInputMessage inputMessage = new MockHttpInputMessage(is); MockHttpInputMessage inputMessage = new MockHttpInputMessage(is);
inputMessage.getHeaders().setContentType(new MediaType("application", "atom+xml", StandardCharsets.UTF_8)); inputMessage.getHeaders().setContentType(ATOM_XML_UTF8);
Feed result = converter.read(Feed.class, inputMessage); Feed result = converter.read(Feed.class, inputMessage);
assertThat(result.getTitle()).isEqualTo("title"); assertThat(result.getTitle()).isEqualTo("title");
assertThat(result.getSubtitle().getValue()).isEqualTo("subtitle"); assertThat(result.getSubtitle().getValue()).isEqualTo("subtitle");
@ -86,7 +90,7 @@ public class AtomFeedHttpMessageConverterTests {
} }
@Test @Test
public void write() throws IOException, SAXException { public void write() throws IOException {
Feed feed = new Feed("atom_1.0"); Feed feed = new Feed("atom_1.0");
feed.setTitle("title"); feed.setTitle("title");
@ -106,7 +110,9 @@ public class AtomFeedHttpMessageConverterTests {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
converter.write(feed, null, outputMessage); 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(ATOM_XML_UTF8);
String expected = "<feed xmlns=\"http://www.w3.org/2005/Atom\">" + "<title>title</title>" + String expected = "<feed xmlns=\"http://www.w3.org/2005/Atom\">" + "<title>title</title>" +
"<entry><id>id1</id><title>title1</title></entry>" + "<entry><id>id1</id><title>title1</title></entry>" +
"<entry><id>id2</id><title>title2</title></entry></feed>"; "<entry><id>id2</id><title>title2</title></entry></feed>";
@ -116,7 +122,7 @@ public class AtomFeedHttpMessageConverterTests {
} }
@Test @Test
public void writeOtherCharset() throws IOException, SAXException { public void writeOtherCharset() throws IOException {
Feed feed = new Feed("atom_1.0"); Feed feed = new Feed("atom_1.0");
feed.setTitle("title"); feed.setTitle("title");
String encoding = "ISO-8859-1"; String encoding = "ISO-8859-1";
@ -125,7 +131,22 @@ public class AtomFeedHttpMessageConverterTests {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
converter.write(feed, null, outputMessage); 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 {
MockHttpOutputMessage message = new MockHttpOutputMessage();
MediaType contentType = new MediaType("application", "atom+xml", singletonMap("type", "feed"));
converter.write(new Feed("atom_1.0"), contentType, message);
assertThat(message.getHeaders().getContentType().getParameters())
.as("Invalid content-type")
.hasSize(2)
.containsEntry("type", "feed")
.containsEntry("charset", "UTF-8");
} }
} }

50
spring-web/src/test/java/org/springframework/http/converter/feed/RssChannelHttpMessageConverterTests.java

@ -27,13 +27,13 @@ import com.rometools.rome.feed.rss.Channel;
import com.rometools.rome.feed.rss.Item; import com.rometools.rome.feed.rss.Item;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.xml.sax.SAXException;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.MockHttpInputMessage; import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.MockHttpOutputMessage; import org.springframework.http.MockHttpOutputMessage;
import org.springframework.tests.XmlContent; import org.springframework.tests.XmlContent;
import static java.util.Collections.singletonMap;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/** /**
@ -41,6 +41,10 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
public class RssChannelHttpMessageConverterTests { public class RssChannelHttpMessageConverterTests {
private static final MediaType RSS_XML_UTF8 =
new MediaType(MediaType.APPLICATION_RSS_XML, StandardCharsets.UTF_8);
private RssChannelHttpMessageConverter converter; private RssChannelHttpMessageConverter converter;
@ -51,22 +55,19 @@ public class RssChannelHttpMessageConverterTests {
@Test @Test
public void canRead() { public void canReadAndWrite() {
assertThat(converter.canRead(Channel.class, new MediaType("application", "rss+xml"))).isTrue(); assertThat(converter.canRead(Channel.class, MediaType.APPLICATION_RSS_XML)).isTrue();
assertThat(converter.canRead(Channel.class, new MediaType("application", "rss+xml", StandardCharsets.UTF_8))).isTrue(); assertThat(converter.canRead(Channel.class, RSS_XML_UTF8)).isTrue();
}
@Test assertThat(converter.canWrite(Channel.class, MediaType.APPLICATION_RSS_XML)).isTrue();
public void canWrite() { assertThat(converter.canWrite(Channel.class, RSS_XML_UTF8)).isTrue();
assertThat(converter.canWrite(Channel.class, new MediaType("application", "rss+xml"))).isTrue();
assertThat(converter.canWrite(Channel.class, new MediaType("application", "rss+xml", StandardCharsets.UTF_8))).isTrue();
} }
@Test @Test
public void read() throws IOException { public void read() throws IOException {
InputStream is = getClass().getResourceAsStream("rss.xml"); InputStream is = getClass().getResourceAsStream("rss.xml");
MockHttpInputMessage inputMessage = new MockHttpInputMessage(is); MockHttpInputMessage inputMessage = new MockHttpInputMessage(is);
inputMessage.getHeaders().setContentType(new MediaType("application", "rss+xml", StandardCharsets.UTF_8)); inputMessage.getHeaders().setContentType(RSS_XML_UTF8);
Channel result = converter.read(Channel.class, inputMessage); Channel result = converter.read(Channel.class, inputMessage);
assertThat(result.getTitle()).isEqualTo("title"); assertThat(result.getTitle()).isEqualTo("title");
assertThat(result.getLink()).isEqualTo("https://example.com"); assertThat(result.getLink()).isEqualTo("https://example.com");
@ -83,7 +84,7 @@ public class RssChannelHttpMessageConverterTests {
} }
@Test @Test
public void write() throws IOException, SAXException { public void write() throws IOException {
Channel channel = new Channel("rss_2.0"); Channel channel = new Channel("rss_2.0");
channel.setTitle("title"); channel.setTitle("title");
channel.setLink("https://example.com"); channel.setLink("https://example.com");
@ -103,7 +104,9 @@ public class RssChannelHttpMessageConverterTests {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
converter.write(channel, null, outputMessage); 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(RSS_XML_UTF8);
String expected = "<rss version=\"2.0\">" + String expected = "<rss version=\"2.0\">" +
"<channel><title>title</title><link>https://example.com</link><description>description</description>" + "<channel><title>title</title><link>https://example.com</link><description>description</description>" +
"<item><title>title1</title></item>" + "<item><title>title1</title></item>" +
@ -114,7 +117,7 @@ public class RssChannelHttpMessageConverterTests {
} }
@Test @Test
public void writeOtherCharset() throws IOException, SAXException { public void writeOtherCharset() throws IOException {
Channel channel = new Channel("rss_2.0"); Channel channel = new Channel("rss_2.0");
channel.setTitle("title"); channel.setTitle("title");
channel.setLink("https://example.com"); channel.setLink("https://example.com");
@ -129,7 +132,26 @@ public class RssChannelHttpMessageConverterTests {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
converter.write(channel, null, outputMessage); 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 message = new MockHttpOutputMessage();
converter.write(channel, new MediaType("application", "rss+xml", singletonMap("x", "y")), message);
assertThat(message.getHeaders().getContentType().getParameters())
.as("Invalid content-type")
.hasSize(2)
.containsEntry("x", "y")
.containsEntry("charset", "UTF-8");
} }
} }

Loading…
Cancel
Save