"Fossies" - the Fresh Open Source Software Archive

Member "grails-core-5.2.2/grails-web-databinding/src/test/groovy/org/grails/web/databinding/bindingsource/AbstractRequestBodyDataBindingSourceCreatorSpec.groovy" (2 Aug 2022, 3075 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 package org.grails.web.databinding.bindingsource
    2 
    3 import grails.databinding.CollectionDataBindingSource
    4 import grails.databinding.DataBindingSource
    5 import grails.databinding.SimpleMapDataBindingSource
    6 import grails.web.http.HttpHeaders
    7 import grails.web.mime.MimeType
    8 import org.grails.web.servlet.mvc.GrailsWebRequest
    9 import org.grails.web.util.GrailsApplicationAttributes
   10 import org.springframework.http.HttpMethod
   11 import org.springframework.mock.web.MockHttpServletRequest
   12 import org.springframework.mock.web.MockHttpServletResponse
   13 import org.springframework.mock.web.MockServletContext
   14 import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder
   15 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
   16 import spock.lang.Shared
   17 import spock.lang.Specification
   18 import spock.lang.Unroll
   19 
   20 import javax.servlet.ServletContext
   21 
   22 /**
   23  * Created by Jim on 8/22/2016.
   24  */
   25 class AbstractRequestBodyDataBindingSourceCreatorSpec extends Specification {
   26 
   27     @Shared
   28     AbstractRequestBodyDataBindingSourceCreator bindingSourceCreator
   29 
   30     @Shared
   31     ServletContext servletContext = new MockServletContext()
   32 
   33     void setupSpec() {
   34         bindingSourceCreator = new AbstractRequestBodyDataBindingSourceCreator() {
   35 
   36             @Override
   37             protected DataBindingSource createBindingSource(Reader reader) {
   38                 return new SimpleMapDataBindingSource([id: "request"])
   39             }
   40 
   41             @Override
   42             protected CollectionDataBindingSource createCollectionBindingSource(Reader reader) {
   43                 return null
   44             }
   45         }
   46     }
   47 
   48     MockHttpServletRequest build(String method, String content) {
   49         MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.request(method, new URI("")).param("id", "url")
   50         if (content != null) {
   51             builder.content(content)
   52         }
   53         MockHttpServletRequest request = builder.buildRequest(servletContext)
   54         request.setAttribute(GrailsApplicationAttributes.WEB_REQUEST, new GrailsWebRequest(request, new MockHttpServletResponse(), servletContext))
   55         request
   56     }
   57 
   58     @Unroll
   59     void "test binding request #request.method with content length #request.contentLength"() {
   60         given:
   61         MimeType mimeType = MimeType.ALL
   62         DataBindingSource source
   63 
   64         when:
   65         source = bindingSourceCreator.createDataBindingSource(mimeType, Object, request)
   66 
   67         then:
   68         source.identifierValue == expectedSource
   69 
   70         where:
   71         expectedSource | request
   72         "url"          | build("GET", null)
   73         "url"          | build("GET", "")
   74         "url"          | build("GET", "x")
   75         "url"          | build("DELETE", null)
   76         "url"          | build("DELETE", "")
   77         "url"          | build("DELETE", "x")
   78         "request"      | build("POST", null)
   79         "url"          | build("POST", "")
   80         "request"      | build("POST", "x")
   81         "request"      | build("PUT", null)
   82         "url"          | build("PUT", "")
   83         "request"      | build("PUT", "x")
   84     }
   85 }
   86