"Fossies" - the Fresh Open Source Software Archive

Member "grails-core-5.2.2/grails-test/src/main/groovy/org/grails/plugins/testing/AbstractGrailsMockHttpServletResponse.groovy" (2 Aug 2022, 3929 Bytes) of package /linux/www/grails-core-5.2.2.tar.gz:


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  * Copyright 2008 the original author or authors.
    3  *
    4  * Licensed under the Apache License, Version 2.0 (the "License");
    5  * you may not use this file except in compliance with the License.
    6  * You may obtain a copy of the License at
    7  *
    8  *      http://www.apache.org/licenses/LICENSE-2.0
    9  *
   10  * Unless required by applicable law or agreed to in writing, software
   11  * distributed under the License is distributed on an "AS IS" BASIS,
   12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13  * See the License for the specific language governing permissions and
   14  * limitations under the License.
   15  */
   16 package org.grails.plugins.testing
   17 
   18 import grails.converters.JSON
   19 import groovy.util.slurpersupport.GPathResult
   20 
   21 import javax.servlet.http.HttpServletRequest
   22 
   23 import org.grails.web.json.JSONElement
   24 import org.grails.web.util.GrailsApplicationAttributes
   25 import org.grails.web.servlet.mvc.GrailsWebRequest
   26 import org.springframework.mock.web.MockHttpServletResponse
   27 import org.springframework.util.ReflectionUtils
   28 import org.grails.io.support.SpringIOUtils
   29 
   30 /**
   31  * Simple sub-class of Spring's MockHttpServletResponse that adds the
   32  * left-shift operator, "<<".
   33  */
   34 abstract class AbstractGrailsMockHttpServletResponse extends MockHttpServletResponse {
   35 
   36 
   37     /**
   38      * Sets the response format
   39      *
   40      * @param format The format of the response
   41      */
   42     void setFormat(String format) {
   43         HttpServletRequest request = GrailsWebRequest.lookup().getCurrentRequest()
   44 
   45 
   46         request.setAttribute(GrailsApplicationAttributes.RESPONSE_FORMAT, format)
   47         // remove so that is can be repopulated
   48         request.setAttribute(GrailsApplicationAttributes.RESPONSE_MIME_TYPE, null)
   49     }
   50 
   51     /**
   52      * Appends the given content string to the response's output stream.
   53      */
   54     void leftShift(String content) {
   55         writer << content
   56     }
   57 
   58     /**
   59      * Return the primary value for the given header as a String, if any.
   60      * Will return the first value in case of multiple values.
   61      *
   62      * @param name the name of the header
   63      * @return the associated header value, or <code>null<code> if none
   64      */
   65     String header(String name) {
   66         super.getHeader(name)
   67     }
   68 
   69     /**
   70      * Return all values for the given header as a List of Strings.
   71      * @param name the name of the header
   72      * @return the associated header values, or an empty List if none
   73      */
   74     List<String> headers(String name) {
   75         super.getHeaders(name)
   76     }
   77 
   78     /**
   79      * Get the response XML
   80      *
   81      * @return The response XML
   82      */
   83     GPathResult getXml() {
   84         SpringIOUtils.createXmlSlurper().parseText(contentAsString)
   85     }
   86 
   87     /**
   88      * Get the response JSON
   89      *
   90      * @return  The JSON response
   91      */
   92     JSONElement getJson() {
   93         JSON.parse(contentAsString)
   94     }
   95 
   96     /**
   97      * The response body as text
   98      *
   99      * @return The text within the response body
  100      */
  101     String getText() {
  102         contentAsString
  103     }
  104 
  105     @Override
  106     void reset() {
  107         final webRequest = GrailsWebRequest.lookup()
  108         webRequest?.currentRequest?.removeAttribute(GrailsApplicationAttributes.REDIRECT_ISSUED)
  109         setCommitted(false)
  110         def field = ReflectionUtils.findField(MockHttpServletResponse, "writer")
  111         ReflectionUtils.makeAccessible(field)
  112         field.set(this, null)
  113         webRequest.setOut(getWriter())
  114         super.reset()
  115     }
  116 
  117     String getRedirectUrl() {
  118         getRedirectedUrl()
  119     }
  120 
  121     @Override
  122     String getRedirectedUrl() {
  123         final webRequest = GrailsWebRequest.lookup()
  124         final redirectURI = webRequest?.currentRequest?.getAttribute(GrailsApplicationAttributes.REDIRECT_ISSUED)
  125 
  126         if (redirectURI != null) {
  127             return redirectURI
  128         }
  129 
  130         if (getStatus() in [301, 302]) {
  131             return super.getHeader("Location")
  132         }
  133 
  134         return super.getRedirectedUrl()
  135     }
  136 }