From 32bc34d797007009a6412533e9fbf9ecf6224edc Mon Sep 17 00:00:00 2001 From: Yasser Zamani Date: Fri, 11 Sep 2020 03:13:49 +0430 Subject: [PATCH] increase nonce test coverage --- .../main/resources/template/simple/nonce.ftl | 4 +-- .../FreemarkerResultMockedTest.java | 35 +++++++++++++++++++ .../struts2/views/freemarker/nonceTest.ftl | 25 +++++++++++++ core/src/test/resources/struts.xml | 6 ++++ 4 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 core/src/test/resources/org/apache/struts2/views/freemarker/nonceTest.ftl diff --git a/core/src/main/resources/template/simple/nonce.ftl b/core/src/main/resources/template/simple/nonce.ftl index a124cf6db..795d82a2e 100644 --- a/core/src/main/resources/template/simple/nonce.ftl +++ b/core/src/main/resources/template/simple/nonce.ftl @@ -18,6 +18,4 @@ * under the License. */ --> -<#if parameters.nonce?has_content> - nonce="${parameters.nonce}"<#rt/> - \ No newline at end of file +<#if parameters.nonce?has_content>nonce="${parameters.nonce}"<#rt/> \ No newline at end of file diff --git a/core/src/test/java/org/apache/struts2/views/freemarker/FreemarkerResultMockedTest.java b/core/src/test/java/org/apache/struts2/views/freemarker/FreemarkerResultMockedTest.java index cd3ce51dc..a78bcf1a9 100644 --- a/core/src/test/java/org/apache/struts2/views/freemarker/FreemarkerResultMockedTest.java +++ b/core/src/test/java/org/apache/struts2/views/freemarker/FreemarkerResultMockedTest.java @@ -238,6 +238,41 @@ public class FreemarkerResultMockedTest extends StrutsInternalTestCase { assertTrue(result.contains("")); } + public void testNonce() throws Exception { + File file = new File(ClassLoaderUtil.getResource("template/simple/common-attributes.ftl", getClass()).toURI()); + EasyMock.expect(servletContext.getRealPath("/template/xhtml/common-attributes.ftl")).andReturn(file.getAbsolutePath()); + EasyMock.expect(servletContext.getRealPath("/template/~~~xhtml/common-attributes.ftl")).andReturn(file.getAbsolutePath()); + + file = new File(ClassLoaderUtil.getResource("template/simple/dynamic-attributes.ftl", getClass()).toURI()); + EasyMock.expect(servletContext.getRealPath("/template/xhtml/dynamic-attributes.ftl")).andReturn(file.getAbsolutePath()); + EasyMock.expect(servletContext.getRealPath("/template/~~~xhtml/dynamic-attributes.ftl")).andReturn(file.getAbsolutePath()); + + file = new File(ClassLoaderUtil.getResource("template/simple/nonce.ftl", getClass()).toURI()); + EasyMock.expect(servletContext.getRealPath("/template/simple/nonce.ftl")).andReturn(file.getAbsolutePath()); + + file = new File(ClassLoaderUtil.getResource("template/simple/script.ftl", getClass()).toURI()); + EasyMock.expect(servletContext.getRealPath("/template/simple/script.ftl")).andReturn(file.getAbsolutePath()); + + file = new File(ClassLoaderUtil.getResource("template/simple/script-close.ftl", getClass()).toURI()); + EasyMock.expect(servletContext.getRealPath("/template/simple/script-close.ftl")).andReturn(file.getAbsolutePath()); + + file = new File(ClassLoaderUtil.getResource("template/simple/link.ftl", getClass()).toURI()); + EasyMock.expect(servletContext.getRealPath("/template/simple/link.ftl")).andReturn(file.getAbsolutePath()); + + file = new File(FreeMarkerResultTest.class.getResource("nonceTest.ftl").toURI()); + EasyMock.expect(servletContext.getRealPath("/tutorial/org/apache/struts2/views/freemarker/nonceTest.ftl")).andReturn(file.getAbsolutePath()); + EasyMock.replay(servletContext); + + init(); + + request.setRequestURI("/tutorial/test10.action"); + ActionMapping mapping = container.getInstance(ActionMapper.class).getMapping(request, configurationManager); + dispatcher.serviceAction(request, response, mapping); + + assertTrue(stringWriter.toString().contains(" + +<@s.link/> +<@s.script/> diff --git a/core/src/test/resources/struts.xml b/core/src/test/resources/struts.xml index 0b370aa3e..cae2eb88b 100644 --- a/core/src/test/resources/struts.xml +++ b/core/src/test/resources/struts.xml @@ -86,6 +86,12 @@ + + + org/apache/struts2/views/freemarker/nonceTest.ftl + + +