|
|
|
@ -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"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|