groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jwagenleit...@apache.org
Subject [1/2] groovy git commit: GROOVY-8294: Return 404 for non-existing paths in TemplateServlet when running from .war (closes #588)
Date Fri, 25 Aug 2017 01:30:17 GMT
Repository: groovy
Updated Branches:
  refs/heads/master 4d75ac66d -> 47a47a4ac


GROOVY-8294: Return 404 for non-existing paths in TemplateServlet when running from .war (closes
#588)

- When TemplateServlet was used in application servers that don't unpack
  a war (i.e. when the real path of resources could not be retrieved),
  an internal server error was returned upon trying to retrieve a
  non-existing template file. Now, a 404 is returned.
- When running from a unpacked war, this had already worked before.


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/82922e32
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/82922e32
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/82922e32

Branch: refs/heads/master
Commit: 82922e32f20d4b1a73e4ad0ec4ba031a29b19f1b
Parents: 4d75ac6
Author: Thomas Reifenberger <tom-mi@users.noreply.github.com>
Authored: Thu Aug 17 23:11:30 2017 +0200
Committer: John Wagenleitner <jwagenleitner@apache.org>
Committed: Thu Aug 24 17:51:47 2017 -0700

----------------------------------------------------------------------
 .../java/groovy/servlet/TemplateServlet.java    |  4 +
 .../groovy/servlet/TemplateServletTest.groovy   | 97 ++++++++++++++++++++
 2 files changed, 101 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/82922e32/subprojects/groovy-servlet/src/main/java/groovy/servlet/TemplateServlet.java
----------------------------------------------------------------------
diff --git a/subprojects/groovy-servlet/src/main/java/groovy/servlet/TemplateServlet.java
b/subprojects/groovy-servlet/src/main/java/groovy/servlet/TemplateServlet.java
index 4c3e8be..553ed3c 100644
--- a/subprojects/groovy-servlet/src/main/java/groovy/servlet/TemplateServlet.java
+++ b/subprojects/groovy-servlet/src/main/java/groovy/servlet/TemplateServlet.java
@@ -441,6 +441,10 @@ public class TemplateServlet extends AbstractHttpServlet {
         } else {
             name = getScriptUri(request);
             URL url = servletContext.getResource(name);
+            if (url == null) {
+                response.sendError(HttpServletResponse.SC_NOT_FOUND);
+                return;
+            }
             getMillis = System.currentTimeMillis();
             template = getTemplate(url);
             getMillis = System.currentTimeMillis() - getMillis;

http://git-wip-us.apache.org/repos/asf/groovy/blob/82922e32/subprojects/groovy-servlet/src/test/groovy/groovy/servlet/TemplateServletTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-servlet/src/test/groovy/groovy/servlet/TemplateServletTest.groovy
b/subprojects/groovy-servlet/src/test/groovy/groovy/servlet/TemplateServletTest.groovy
new file mode 100644
index 0000000..ae05e6d
--- /dev/null
+++ b/subprojects/groovy-servlet/src/test/groovy/groovy/servlet/TemplateServletTest.groovy
@@ -0,0 +1,97 @@
+package groovy.servlet
+
+import org.junit.Before
+import org.junit.Rule
+import org.junit.Test
+import org.junit.rules.TemporaryFolder
+
+import javax.servlet.ServletConfig
+import javax.servlet.ServletContext
+import javax.servlet.http.HttpServletRequest
+import javax.servlet.http.HttpServletResponse
+
+class TemplateServletTest {
+
+    TemplateServlet servlet
+
+    @Rule
+    public TemporaryFolder temporaryFolder = new TemporaryFolder()
+
+    @Before
+    void setUp() {
+        servlet = new TemplateServlet()
+    }
+
+    @Test
+    void test_service_for_existing_resource() {
+        def templateFile = temporaryFolder.newFile('template.gsp')
+        def url = templateFile.toURI().toURL()
+        def servletConfig = mockServletConfigForUrlResource(url)
+        HttpServletRequest request = mockRequest()
+        def (HttpServletResponse response, responseData) = mockResponse()
+        servlet.init(servletConfig)
+
+        servlet.service(request, response)
+
+        assert responseData.error == null
+        assert responseData.writer.toString() != ''
+        assert responseData.status == HttpServletResponse.SC_OK
+    }
+
+    @Test
+    void test_service_for_missing_resource() {
+        def url = null
+        def servletConfig = mockServletConfigForUrlResource(url)
+        HttpServletRequest request = mockRequest()
+        def (HttpServletResponse response, responseData) = mockResponse()
+        servlet.init(servletConfig)
+
+        servlet.service(request, response)
+
+        assert responseData.error == HttpServletResponse.SC_NOT_FOUND
+        assert responseData.writer.toString() == ''
+        assert responseData.status == null
+    }
+
+    private mockRequest() {
+        return [
+                getAttribute     : { null },
+                getPathInfo      : { 'pathInfo' },
+                getScriptUri     : 'scriptUri',
+                getServletPath   : { 'servletPath' },
+                getSession       : { null },
+                getParameterNames: { new Vector().elements() },
+                getHeaderNames   : { new Vector().elements() },
+        ] as HttpServletRequest
+    }
+
+    private mockResponse() {
+        def data = [
+                writer: new StringWriter(),
+                status: null,
+                error : null,
+        ]
+        def mock = [
+                getWriter     : { new PrintWriter(data.writer) },
+                sendError     : { error -> data.error = error },
+                setContentType: { contentType -> },
+                setStatus     : { status -> data.status = status },
+                flushBuffer   : { -> },
+        ] as HttpServletResponse
+        return [mock, data]
+    }
+
+    private mockServletConfigForUrlResource(URL mockedResourceUrl) {
+        def servletContext = [
+                getRealPath: { arg -> null },
+                getResource: { arg -> mockedResourceUrl },
+                log        : { msg -> },
+        ] as ServletContext
+        [
+                getServletName   : { 'name' },
+                getServletContext: { servletContext },
+                getInitParameter : { null },
+        ] as ServletConfig
+    }
+
+}


Mime
View raw message