Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RESTEASY-2033 and RESTEASY-2034] Changes to
MessageSanitizerContainerResponseFilter 1. It handles MediaType. 2. It can be disabled. [RESTEASY-2033 and RESTEASY-2034] Minor correction.
- Loading branch information
Showing
11 changed files
with
310 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...tests/src/test/java/org/jboss/resteasy/test/providers/html/HtmlSanitizerOptionalTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package org.jboss.resteasy.test.providers.html; | ||
|
||
import javax.ws.rs.client.ClientBuilder; | ||
import javax.ws.rs.core.Response; | ||
|
||
import org.jboss.arquillian.container.test.api.Deployment; | ||
import org.jboss.arquillian.container.test.api.RunAsClient; | ||
import org.jboss.arquillian.junit.Arquillian; | ||
import org.jboss.resteasy.client.jaxrs.ResteasyClient; | ||
import org.jboss.resteasy.test.providers.html.resource.HtmlSanitizerOptionalResource; | ||
import org.jboss.resteasy.utils.PortProviderUtil; | ||
import org.jboss.resteasy.utils.TestUtil; | ||
import org.jboss.shrinkwrap.api.Archive; | ||
import org.jboss.shrinkwrap.api.spec.WebArchive; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
/** | ||
* @tpSubChapter | ||
* @tpChapter Integration tests | ||
* @tpTestCaseDetails Regression test for RESTEASY-2034 | ||
* @tpSince RESTEasy 4.0.0 | ||
*/ | ||
@RunWith(Arquillian.class) | ||
@RunAsClient | ||
public class HtmlSanitizerOptionalTest { | ||
|
||
static ResteasyClient client; | ||
|
||
private static final String ENABLED = "_enabled"; | ||
private static final String DISABLED = "_disabled"; | ||
private static final String DEFAULT = "_default"; | ||
|
||
static public final String input = "<html <\"abc\" 'xyz'>/>"; | ||
static private final String output = "<html &lt;"abc" 'xyz'&gt;/>"; | ||
|
||
@Deployment(name = ENABLED, order = 1) | ||
public static Archive<?> createTestArchive1() { | ||
WebArchive war = TestUtil.prepareArchive(HtmlSanitizerOptionalTest.class.getSimpleName() + ENABLED); | ||
war.addAsWebInfResource(HtmlSanitizerOptionalTest.class.getPackage(), "HtmlSanitizerOptional_Enabled_web.xml", "web.xml"); | ||
return TestUtil.finishContainerPrepare(war, null, HtmlSanitizerOptionalResource.class); | ||
} | ||
|
||
@Deployment(name = DISABLED, order = 2) | ||
public static Archive<?> createTestArchive2() { | ||
WebArchive war = TestUtil.prepareArchive(HtmlSanitizerOptionalTest.class.getSimpleName() + DISABLED); | ||
war.addAsWebInfResource(HtmlSanitizerOptionalTest.class.getPackage(), "HtmlSanitizerOptional_Disabled_web.xml", "web.xml"); | ||
return TestUtil.finishContainerPrepare(war, null, HtmlSanitizerOptionalResource.class); | ||
} | ||
|
||
@Deployment(name = DEFAULT, order = 3) | ||
public static Archive<?> createTestArchive3() { | ||
WebArchive war = TestUtil.prepareArchive(HtmlSanitizerOptionalTest.class.getSimpleName() + DEFAULT); | ||
war.addAsWebInfResource(HtmlSanitizerOptionalTest.class.getPackage(), "HtmlSanitizerOptional_Default_web.xml", "web.xml"); | ||
return TestUtil.finishContainerPrepare(war, null, HtmlSanitizerOptionalResource.class); | ||
} | ||
|
||
private String generateURL(String path, String version) { | ||
return PortProviderUtil.generateURL(path, HtmlSanitizerOptionalTest.class.getSimpleName() + version); | ||
} | ||
|
||
@Before | ||
public void init() { | ||
client = (ResteasyClient)ClientBuilder.newClient(); | ||
} | ||
|
||
@After | ||
public void after() throws Exception { | ||
client.close(); | ||
} | ||
|
||
/** | ||
* @tpTestDetails Context parameter "resteasy.disable.html.sanitizer" is set to "true". | ||
* @tpPassCrit Input string should be unchanged. | ||
* @tpSince RESTEasy 4.0.0 | ||
*/ | ||
@Test | ||
public void testHtmlSanitizerDisabled() throws Exception { | ||
Response response = client.target(generateURL("/test", DISABLED)).request().get(); | ||
Assert.assertEquals(input, response.readEntity(String.class)); | ||
} | ||
|
||
/** | ||
* @tpTestDetails Context parameter "resteasy.disable.html.sanitizer" is set to "false" | ||
* @tpPassCrit Input string should be sanitized. | ||
* @tpSince RESTEasy 4.0.0 | ||
*/ | ||
@Test | ||
public void testHtmlSanitizerEnabled() throws Exception { | ||
Response response = client.target(generateURL("/test", ENABLED)).request().get(); | ||
Assert.assertEquals(output, response.readEntity(String.class)); | ||
} | ||
|
||
/** | ||
* @tpTestDetails Context parameter "resteasy.disable.html.sanitizer" is not set. | ||
* @tpPassCrit Input string should be sanitized. | ||
* @tpSince RESTEasy 4.0.0 | ||
*/ | ||
@Test | ||
public void testHtmlSanitizerDefault() throws Exception { | ||
Response response = client.target(generateURL("/test", DEFAULT)).request().get(); | ||
Assert.assertEquals(output, response.readEntity(String.class)); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...t/java/org/jboss/resteasy/test/providers/html/resource/HtmlSanitizerOptionalResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.jboss.resteasy.test.providers.html.resource; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.Response; | ||
|
||
import org.jboss.resteasy.test.providers.html.HtmlSanitizerOptionalTest; | ||
import org.jboss.resteasy.util.HttpResponseCodes; | ||
|
||
@Path("") | ||
public class HtmlSanitizerOptionalResource { | ||
|
||
@Path("test") | ||
@GET | ||
@Produces("text/html") | ||
public Response test() { | ||
return Response.status(HttpResponseCodes.SC_BAD_REQUEST).entity(HtmlSanitizerOptionalTest.input).build(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...st/resources/org/jboss/resteasy/test/providers/html/HtmlSanitizerOptional_Default_web.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> | ||
|
||
<display-name>HtmlSanitizerOptionalTest_Default</display-name> | ||
|
||
<servlet> | ||
<servlet-name>Resteasy</servlet-name> | ||
|
||
<servlet-class> | ||
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher | ||
</servlet-class> | ||
<init-param> | ||
<param-name>javax.ws.rs.Application</param-name> | ||
<param-value>org.jboss.resteasy.utils.TestApplication</param-value> | ||
</init-param> | ||
</servlet> | ||
|
||
<servlet-mapping> | ||
<servlet-name>Resteasy</servlet-name> | ||
<url-pattern>/*</url-pattern> | ||
</servlet-mapping> | ||
|
||
</web-app> |
29 changes: 29 additions & 0 deletions
29
...t/resources/org/jboss/resteasy/test/providers/html/HtmlSanitizerOptional_Disabled_web.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> | ||
|
||
<display-name>HtmlSanitizerOptionalTest_Disabled</display-name> | ||
|
||
<context-param> | ||
<param-name>resteasy.disable.html.sanitizer</param-name> | ||
<param-value>true</param-value> | ||
</context-param> | ||
|
||
<servlet> | ||
<servlet-name>Resteasy</servlet-name> | ||
|
||
<servlet-class> | ||
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher | ||
</servlet-class> | ||
<init-param> | ||
<param-name>javax.ws.rs.Application</param-name> | ||
<param-value>org.jboss.resteasy.utils.TestApplication</param-value> | ||
</init-param> | ||
</servlet> | ||
|
||
<servlet-mapping> | ||
<servlet-name>Resteasy</servlet-name> | ||
<url-pattern>/*</url-pattern> | ||
</servlet-mapping> | ||
|
||
</web-app> |
29 changes: 29 additions & 0 deletions
29
...st/resources/org/jboss/resteasy/test/providers/html/HtmlSanitizerOptional_Enabled_web.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> | ||
|
||
<display-name>HtmlSanitizerOptionalTest_Disabled</display-name> | ||
|
||
<context-param> | ||
<param-name>resteasy.disable.html.sanitizer</param-name> | ||
<param-value>false</param-value> | ||
</context-param> | ||
|
||
<servlet> | ||
<servlet-name>Resteasy</servlet-name> | ||
|
||
<servlet-class> | ||
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher | ||
</servlet-class> | ||
<init-param> | ||
<param-name>javax.ws.rs.Application</param-name> | ||
<param-value>org.jboss.resteasy.utils.TestApplication</param-value> | ||
</init-param> | ||
</servlet> | ||
|
||
<servlet-mapping> | ||
<servlet-name>Resteasy</servlet-name> | ||
<url-pattern>/*</url-pattern> | ||
</servlet-mapping> | ||
|
||
</web-app> |
51 changes: 51 additions & 0 deletions
51
...sts/src/test/java/org/jboss/resteasy/test/interception/MessageSanitizerMediaTypeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.jboss.resteasy.test.interception; | ||
|
||
import javax.ws.rs.container.ContainerResponseContext; | ||
import javax.ws.rs.core.MediaType; | ||
|
||
import org.jboss.resteasy.core.Headers; | ||
import org.jboss.resteasy.core.interception.ContainerResponseContextImpl; | ||
import org.jboss.resteasy.plugins.interceptors.encoding.MessageSanitizerContainerResponseFilter; | ||
import org.jboss.resteasy.specimpl.BuiltResponse; | ||
import org.jboss.resteasy.util.HttpResponseCodes; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
|
||
/** | ||
* @tpSubChapter Providers - MessageSanitizerContainerResponseFilter | ||
* @tpChapter Unit tests | ||
* @tpTestCaseDetails Regression test for RESTEASY-2033 | ||
* @tpSince RESTEasy 4.0.0 | ||
*/ | ||
public class MessageSanitizerMediaTypeTest { | ||
|
||
static private final String input = "<html <\"abc\" 'xyz'>/>"; | ||
static private final String output = "<html &lt;"abc" 'xyz'&gt;/>"; | ||
|
||
public static class TestContainerResponseContext extends ContainerResponseContextImpl { | ||
public TestContainerResponseContext(BuiltResponse builtResponse) { | ||
super(null, null, builtResponse, null, null, null, null); | ||
} | ||
} | ||
|
||
@Test | ||
public void testMessageSanitizerText() throws Exception { | ||
doTestMessageSanitizerMediaType("text/html"); | ||
} | ||
|
||
@Test | ||
public void testMessageSanitizerMediaType() throws Exception { | ||
doTestMessageSanitizerMediaType(MediaType.TEXT_HTML_TYPE); | ||
} | ||
|
||
void doTestMessageSanitizerMediaType(Object mediaType) throws Exception { | ||
Headers<Object> headers = new Headers<Object>(); | ||
headers.add("Content-Type", mediaType); | ||
BuiltResponse response = new BuiltResponse(HttpResponseCodes.SC_BAD_REQUEST, "", headers, input, null); | ||
ContainerResponseContext responseContext = new TestContainerResponseContext(response); | ||
MessageSanitizerContainerResponseFilter filter = new MessageSanitizerContainerResponseFilter(); | ||
filter.filter(null, responseContext); | ||
Assert.assertEquals(output, responseContext.getEntity()); | ||
} | ||
} |