|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2014 the original author or authors. |
|
|
|
* Copyright 2002-2015 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. |
|
|
|
@ -26,10 +26,8 @@ import com.fasterxml.jackson.dataformat.xml.XmlMapper; |
|
|
|
import org.junit.Rule; |
|
|
|
import org.junit.Rule; |
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.rules.ExpectedException; |
|
|
|
import org.junit.rules.ExpectedException; |
|
|
|
import org.xml.sax.SAXException; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.core.io.ClassPathResource; |
|
|
|
import org.springframework.core.io.ClassPathResource; |
|
|
|
import org.springframework.core.io.Resource; |
|
|
|
|
|
|
|
import org.springframework.http.HttpOutputMessage; |
|
|
|
import org.springframework.http.HttpOutputMessage; |
|
|
|
import org.springframework.http.MediaType; |
|
|
|
import org.springframework.http.MediaType; |
|
|
|
import org.springframework.http.MockHttpInputMessage; |
|
|
|
import org.springframework.http.MockHttpInputMessage; |
|
|
|
@ -45,6 +43,7 @@ import static org.junit.Assert.*; |
|
|
|
* Jackson 2.x XML converter tests. |
|
|
|
* Jackson 2.x XML converter tests. |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Sebastien Deleuze |
|
|
|
* @author Sebastien Deleuze |
|
|
|
|
|
|
|
* @author Rossen Stoyanchev |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class MappingJackson2XmlHttpMessageConverterTests { |
|
|
|
public class MappingJackson2XmlHttpMessageConverterTests { |
|
|
|
|
|
|
|
|
|
|
|
@ -70,17 +69,16 @@ public class MappingJackson2XmlHttpMessageConverterTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void read() throws IOException { |
|
|
|
public void read() throws IOException { |
|
|
|
String body = |
|
|
|
String body = "<MyBean><string>Foo</string><number>42</number><fraction>42.0</fraction><array><array>Foo</array><array>Bar</array></array><bool>true</bool><bytes>AQI=</bytes></MyBean>"; |
|
|
|
"<MyBean><string>Foo</string><number>42</number><fraction>42.0</fraction><array><array>Foo</array><array>Bar</array></array><bool>true</bool><bytes>AQI=</bytes></MyBean>"; |
|
|
|
|
|
|
|
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); |
|
|
|
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); |
|
|
|
inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); |
|
|
|
inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); |
|
|
|
MyBean result = (MyBean) converter.read(MyBean.class, inputMessage); |
|
|
|
MyBean result = (MyBean) converter.read(MyBean.class, inputMessage); |
|
|
|
assertEquals("Foo", result.getString()); |
|
|
|
assertEquals("Foo", result.getString()); |
|
|
|
assertEquals(42, result.getNumber()); |
|
|
|
assertEquals(42, result.getNumber()); |
|
|
|
assertEquals(42F, result.getFraction(), 0F); |
|
|
|
assertEquals(42F, result.getFraction(), 0F); |
|
|
|
assertArrayEquals(new String[] {"Foo", "Bar"}, result.getArray()); |
|
|
|
assertArrayEquals(new String[]{"Foo", "Bar"}, result.getArray()); |
|
|
|
assertTrue(result.isBool()); |
|
|
|
assertTrue(result.isBool()); |
|
|
|
assertArrayEquals(new byte[] {0x1, 0x2}, result.getBytes()); |
|
|
|
assertArrayEquals(new byte[]{0x1, 0x2}, result.getBytes()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@ -149,7 +147,6 @@ public class MappingJackson2XmlHttpMessageConverterTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void readWithExternalReference() throws IOException { |
|
|
|
public void readWithExternalReference() throws IOException { |
|
|
|
|
|
|
|
|
|
|
|
String body = "<!DOCTYPE MyBean SYSTEM \"http://192.168.28.42/1.jsp\" [" + |
|
|
|
String body = "<!DOCTYPE MyBean SYSTEM \"http://192.168.28.42/1.jsp\" [" + |
|
|
|
" <!ELEMENT root ANY >\n" + |
|
|
|
" <!ELEMENT root ANY >\n" + |
|
|
|
" <!ENTITY ext SYSTEM \"" + |
|
|
|
" <!ENTITY ext SYSTEM \"" + |
|
|
|
@ -160,14 +157,11 @@ public class MappingJackson2XmlHttpMessageConverterTests { |
|
|
|
inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); |
|
|
|
inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); |
|
|
|
|
|
|
|
|
|
|
|
this.thrown.expect(HttpMessageNotReadableException.class); |
|
|
|
this.thrown.expect(HttpMessageNotReadableException.class); |
|
|
|
this.thrown.expectMessage("entity \"ext\""); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.converter.read(MyBean.class, inputMessage); |
|
|
|
this.converter.read(MyBean.class, inputMessage); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void readWithXmlBomb() throws IOException { |
|
|
|
public void readWithXmlBomb() throws IOException { |
|
|
|
|
|
|
|
|
|
|
|
// https://en.wikipedia.org/wiki/Billion_laughs
|
|
|
|
// https://en.wikipedia.org/wiki/Billion_laughs
|
|
|
|
// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
|
|
|
|
// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
|
|
|
|
String body = "<?xml version=\"1.0\"?>\n" + |
|
|
|
String body = "<?xml version=\"1.0\"?>\n" + |
|
|
|
@ -190,15 +184,11 @@ public class MappingJackson2XmlHttpMessageConverterTests { |
|
|
|
inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); |
|
|
|
inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); |
|
|
|
|
|
|
|
|
|
|
|
this.thrown.expect(HttpMessageNotReadableException.class); |
|
|
|
this.thrown.expect(HttpMessageNotReadableException.class); |
|
|
|
this.thrown.expectMessage("entity \"lol9\""); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.converter.read(MyBean.class, inputMessage); |
|
|
|
this.converter.read(MyBean.class, inputMessage); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void writeInternal(Object object, HttpOutputMessage outputMessage) |
|
|
|
private void writeInternal(Object object, HttpOutputMessage outputMessage) throws Exception { |
|
|
|
throws NoSuchMethodException, InvocationTargetException, |
|
|
|
|
|
|
|
IllegalAccessException { |
|
|
|
|
|
|
|
Method method = AbstractJackson2HttpMessageConverter.class.getDeclaredMethod( |
|
|
|
Method method = AbstractJackson2HttpMessageConverter.class.getDeclaredMethod( |
|
|
|
"writeInternal", Object.class, HttpOutputMessage.class); |
|
|
|
"writeInternal", Object.class, HttpOutputMessage.class); |
|
|
|
method.setAccessible(true); |
|
|
|
method.setAccessible(true); |
|
|
|
@ -269,9 +259,12 @@ public class MappingJackson2XmlHttpMessageConverterTests { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private interface MyJacksonView1 {}; |
|
|
|
private interface MyJacksonView1 {}; |
|
|
|
|
|
|
|
|
|
|
|
private interface MyJacksonView2 {}; |
|
|
|
private interface MyJacksonView2 {}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unused") |
|
|
|
@SuppressWarnings("unused") |
|
|
|
private static class JacksonViewBean { |
|
|
|
private static class JacksonViewBean { |
|
|
|
|
|
|
|
|
|
|
|
@ -308,6 +301,7 @@ public class MappingJackson2XmlHttpMessageConverterTests { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("serial") |
|
|
|
@SuppressWarnings("serial") |
|
|
|
private static class MyXmlMapper extends XmlMapper { |
|
|
|
private static class MyXmlMapper extends XmlMapper { |
|
|
|
} |
|
|
|
} |
|
|
|
|