"Fossies" - the Fresh Open Source Software Archive

Member "groovy-4.0.12/subprojects/groovy-servlet/src/test/groovy/groovy/servlet/AbstractHttpServletTest.groovy" (31 Jan 1980, 10021 Bytes) of package /linux/misc/apache-groovy-src-4.0.12.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Java source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2  *  Licensed to the Apache Software Foundation (ASF) under one
    3  *  or more contributor license agreements.  See the NOTICE file
    4  *  distributed with this work for additional information
    5  *  regarding copyright ownership.  The ASF licenses this file
    6  *  to you under the Apache License, Version 2.0 (the
    7  *  "License"); you may not use this file except in compliance
    8  *  with the License.  You may obtain a copy of the License at
    9  *
   10  *    http://www.apache.org/licenses/LICENSE-2.0
   11  *
   12  *  Unless required by applicable law or agreed to in writing,
   13  *  software distributed under the License is distributed on an
   14  *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15  *  KIND, either express or implied.  See the License for the
   16  *  specific language governing permissions and limitations
   17  *  under the License.
   18  */
   19 package groovy.servlet
   20 
   21 import groovy.test.GroovyTestCase
   22 
   23 import javax.servlet.ServletConfig
   24 import javax.servlet.ServletContext
   25 import javax.servlet.http.HttpServletRequest
   26 
   27 /**
   28  * This test case tests the AbstractHttpServlet class. It uses a test
   29  * specific subclass called ConcreteHttpServlet to test the abstract
   30  * class in isolation from any implementations.
   31  */
   32 class AbstractHttpServletTest extends GroovyTestCase {
   33 
   34     def servlet
   35 
   36     void setUp() {
   37         super.setUp()
   38         servlet = new ConcreteHttpServlet()
   39     }
   40 
   41     /**
   42      * getScriptUri() concatenates the servlet path and path info
   43      * attributes if attributes exist on the http request.
   44      */
   45     void testGetScriptUri_AllAttributesExist() {
   46         // just return whatever attributes were requested
   47         def request = { attribute -> attribute }
   48 
   49         assert servlet.getScriptUri(request as HttpServletRequest) ==
   50                 AbstractHttpServlet.INC_SERVLET_PATH + AbstractHttpServlet.INC_PATH_INFO
   51     }
   52 
   53     /**
   54      * getScriptUri() returns the servlet path if the http request
   55      * contains path but no path info attribute.
   56      */
   57     void testGetScriptUri_NoPathInfoAttribute() {
   58         // just return whatever attributes were requested, except for path info attribute
   59         def request = { attribute ->
   60             attribute == AbstractHttpServlet.INC_PATH_INFO ? null : attribute
   61         }
   62 
   63         assert servlet.getScriptUri(request as HttpServletRequest) ==
   64                 AbstractHttpServlet.INC_SERVLET_PATH
   65     }
   66 
   67     /**
   68      * Tests getScriptUri when no attributes exist, but servletPath and
   69      * pathInfo methods return data.
   70      */
   71     void testGetScriptUri_NoAttributesPathInfoExists() {
   72         def request = [
   73                 getAttribute: { null },
   74                 getServletPath: { 'servletPath' },
   75                 getPathInfo: { 'pathInfo' }
   76         ] as HttpServletRequest
   77 
   78         def servlet = new ConcreteHttpServlet()
   79 
   80         assert servlet.getScriptUri(request) == 'servletPathpathInfo'
   81     }
   82 
   83     /**
   84      * Tests getScriptUri when no attributes exist, no path info exists,
   85      * but servletPath returns data.
   86      */
   87     void testGetScriptUri_NoAttributesPathInfoMissing() {
   88         def request = [
   89                 getAttribute: { null },
   90                 getServletPath: { 'servletPath' },
   91                 getPathInfo: { null }
   92         ] as HttpServletRequest
   93 
   94         def servlet = new ConcreteHttpServlet()
   95 
   96         assert servlet.getScriptUri(request) == 'servletPath'
   97     }
   98 
   99     /**
  100      * Tests getting URIs as files.
  101      */
  102     void testGetScriptURIasFile() {
  103         def request = [
  104                 getAttribute: { null },
  105                 getServletPath: { 'servletPath' },
  106                 getPathInfo: { 'pathInfo' }
  107         ] as HttpServletRequest
  108 
  109         def servletContext = [
  110                 getRealPath: { arg -> 'realPath' + arg }
  111         ] as ServletContext
  112 
  113         def servletConfig = [
  114                 getServletContext: { servletContext },
  115                 getInitParameter: { null }
  116         ] as ServletConfig
  117 
  118         servlet.init(servletConfig)
  119         def file = servlet.getScriptUriAsFile(request)
  120 
  121         assert file.getName() == 'realPathservletPathpathInfo'
  122     }
  123 
  124     /**
  125      * Tests getting URIs as files where filename not available.
  126      */
  127     void testGetScriptURIasFileNoMapping() {
  128         def request = [
  129                 getAttribute: { null },
  130                 getServletPath: { 'servletPath' },
  131                 getPathInfo: { 'pathInfo' }
  132         ] as HttpServletRequest
  133 
  134         def servletContext = [
  135                 getRealPath: { arg -> null }
  136         ] as ServletContext
  137 
  138         def servletConfig = [
  139                 getServletContext: { servletContext },
  140                 getInitParameter: { null }
  141         ] as ServletConfig
  142 
  143         servlet.init(servletConfig)
  144         def file = servlet.getScriptUriAsFile(request)
  145 
  146         assert file == null
  147     }
  148 
  149     /**
  150      * Tests that exception is thrown when resource is not found.
  151      */
  152     void testGetResourceConnection_MissingResource() {
  153         def servletContext = [
  154                 getRealPath: { arg -> 'realPath' + arg },
  155                 getResource: { arg -> null }
  156         ] as ServletContext
  157 
  158         def servletConfig = [
  159                 getServletContext: { servletContext },
  160                 getInitParameter: { null }
  161         ] as ServletConfig
  162 
  163         // servlet config is used to find resources
  164         servlet.init(servletConfig)
  165 
  166         shouldFail(groovy.util.ResourceException) {
  167             servlet.getResourceConnection('someresource')
  168         }
  169     }
  170 
  171     /**
  172      * Tests finding resource.
  173      */
  174     void testGetResourceConnection_FoundInCurrentDir() {
  175         def urlStub = new java.net.URL('file:realPath/someresource')
  176         def servletContext = [
  177                 getRealPath: { arg -> 'realPath' + arg },
  178                 getResource: { arg -> arg == '/someresource' ? urlStub : null }
  179         ] as ServletContext
  180 
  181         def servletConfig = [
  182                 getServletContext: { servletContext },
  183                 getInitParameter: { null }
  184         ] as ServletConfig
  185 
  186         // servlet config is used to find resources
  187         servlet.init(servletConfig)
  188 
  189         def connection = servlet.getResourceConnection('someresource')
  190 
  191         assert connection.getURL() == urlStub
  192     }
  193 
  194     /**
  195      * Tests finding resource in web-inf directory.
  196      */
  197     void testGetResourceConnection_FoundInWebInf() {
  198         def urlStub = new java.net.URL('file:realPath/WEB-INF/groovy/someresource')
  199         def servletContext = [
  200                 getRealPath: { arg -> 'realPath' + arg },
  201                 getResource: { arg -> arg == '/WEB-INF/groovy/someresource' ? urlStub : null }
  202         ] as ServletContext
  203 
  204         def servletConfig = [
  205                 getServletContext: { servletContext },
  206                 getInitParameter: { null }
  207         ] as ServletConfig
  208 
  209         // servlet config is used to find resources
  210         servlet.init(servletConfig)
  211 
  212         def connection = servlet.getResourceConnection('someresource')
  213 
  214         assert connection.getURL() == urlStub
  215     }
  216 
  217     /**
  218      * Tests regex style resource replacement for first occurrence.
  219      */
  220     void testGetResourceConnection_Replace1stFooWithBar() {
  221         def servletContext = [
  222                 getRealPath: { arg -> 'realPath' + arg },
  223                 getResource: { arg ->
  224                     if (arg.startsWith('//')) arg = arg.substring(2)
  225                     new URL('http://' + (arg == '/' ? '' : arg))
  226                 }
  227         ] as ServletContext
  228 
  229         def servletConfig = [
  230                 getServletContext: { servletContext },
  231                 getInitParameter: { arg ->
  232                     // replace first occurrence of foo resources with bar resources
  233                     if (arg == 'resource.name.regex') return 'foo'
  234                     else if (arg == 'resource.name.replacement') return 'bar'
  235                     else if (arg == 'resource.name.replace.all') return 'false'
  236                     return null
  237                 }
  238         ] as ServletConfig
  239 
  240         def request = [
  241                 getAttribute: { null },
  242                 getServletPath: { '/somefoo/foo' },
  243                 getPathInfo: { null }
  244         ] as HttpServletRequest
  245 
  246         // servlet config is used to find resources
  247         servlet.init(servletConfig)
  248 
  249         // replace first foo with bar in resources
  250         def connection = servlet.getResourceConnection(servlet.getScriptUri(request))
  251 
  252         assert connection.getURL().toExternalForm() == new URL('http:/somebar/foo').toExternalForm()
  253     }
  254 
  255     /**
  256      * Tests regex style resource replacement for all occurrences.
  257      */
  258     void testGetResourceConnection_ReplaceAllFooWithBar() {
  259         def servletContext = [
  260                 getRealPath: { arg -> 'realPath' + arg },
  261                 getResource: { arg ->
  262                     if (arg.startsWith('//')) arg = arg.substring(2)
  263                     new URL('http://' + (arg == '/' ? '' : arg))
  264                 }
  265         ] as ServletContext
  266 
  267         def servletConfig = [
  268                 getServletContext: { servletContext },
  269                 getInitParameter: { arg ->
  270                     // replace all occurrences of foo resources with bar resources
  271                     if (arg == 'resource.name.regex') return 'foo'
  272                     else if (arg == 'resource.name.replacement') return 'bar'
  273                     else if (arg == 'resource.name.replace.all') return 'true'
  274                     return null
  275                 }
  276         ] as ServletConfig
  277 
  278         def request = [
  279                 getAttribute: { null },
  280                 getServletPath: { '/somefoo/foo' },
  281                 getPathInfo: { null }
  282         ] as HttpServletRequest
  283 
  284         // servlet config is used to find resources
  285         servlet.init(servletConfig)
  286 
  287         // replace all foo(s) with bar in resources
  288         def connection = servlet.getResourceConnection(servlet.getScriptUri(request))
  289 
  290         assert connection.getURL().toExternalForm() == new URL('http:/somebar/bar').toExternalForm()
  291     }
  292 }
  293 
  294 // test specific subclass
  295 class ConcreteHttpServlet extends AbstractHttpServlet {}