"Fossies" - the Fresh Open Source Software Archive

Member "grails-core-5.2.2/grails-plugin-mimetypes/src/test/groovy/org/grails/web/mime/AcceptHeaderParserTests.groovy" (2 Aug 2022, 6011 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.mime
    2 
    3 import grails.core.DefaultGrailsApplication
    4 import grails.util.Holders
    5 import org.grails.config.PropertySourcesConfig
    6 import org.grails.plugins.web.mime.MimeTypesFactoryBean
    7 import org.grails.web.mime.DefaultAcceptHeaderParser
    8 import org.junit.jupiter.api.AfterEach
    9 import org.junit.jupiter.api.BeforeEach
   10 import org.junit.jupiter.api.Test
   11 import org.springframework.core.env.MapPropertySource
   12 import org.springframework.core.env.MutablePropertySources
   13 
   14 import static org.junit.jupiter.api.Assertions.assertEquals
   15 
   16 /**
   17  * @author Graeme Rocher
   18  * @since 1.0
   19  */
   20 class AcceptHeaderParserTests {
   21 
   22     def config
   23 
   24     @BeforeEach
   25     protected void setUp() {
   26         def configObject = new ConfigSlurper().parse("""
   27 grails.mime.types = [ xml: ['text/xml', 'application/xml'],
   28                       text: 'text/plain',
   29                       js: 'text/javascript',
   30                       rss: 'application/rss+xml',
   31                       atom: 'application/atom+xml',
   32                       css: 'text/css',
   33                       cvs: 'text/csv',
   34                       all: '*/*',
   35                       json: 'application/json',
   36                       html: ['text/html','application/xhtml+xml'],
   37                       foov1: 'application/vnd.foo+json;v=1.0',
   38                       foov2: 'application/vnd.foo+json;v=2.0'
   39                     ]
   40         """)
   41         def ps = new MutablePropertySources()
   42         ps.addLast(new MapPropertySource("grails", configObject))
   43         config = new PropertySourcesConfig(ps)
   44 
   45     }
   46 
   47     @AfterEach
   48     protected void tearDown() {
   49         config = null
   50         Holders.setConfig null
   51     }
   52 
   53     @Test
   54     void testXmlContentTypeWithCharset() {
   55 
   56         DefaultAcceptHeaderParser parser = getAcceptHeaderParser()
   57         def mimes = parser.parse("text/xml; charset=UTF-8")
   58 
   59         assertEquals 1, mimes.size()
   60         assertEquals "application/xml", mimes[0].name
   61         assertEquals "xml", mimes[0].extension
   62         assertEquals('UTF-8', mimes[0].parameters.charset)
   63         assertEquals('1.0', mimes[0].parameters.q)
   64         assertEquals('1.0', mimes[0].quality)
   65     }
   66 
   67     protected DefaultAcceptHeaderParser getAcceptHeaderParser() {
   68         final application = new DefaultGrailsApplication(config: config)
   69         final factoryBean = new MimeTypesFactoryBean(grailsApplication: application)
   70         final parser = new DefaultAcceptHeaderParser(factoryBean.getObject())
   71         parser
   72     }
   73 
   74     @Test
   75     void testXmlContentTypeWithCharsetAndVersion() {
   76         def mimes = getAcceptHeaderParser().parse("text/xml; charset=UTF-8; v=1.1")
   77 
   78         assertEquals 1, mimes.size()
   79         assertEquals "application/xml", mimes[0].name
   80         assertEquals "xml", mimes[0].extension
   81         assertEquals('UTF-8', mimes[0].parameters.charset)
   82         assertEquals('1.0', mimes[0].parameters.q)
   83         assertEquals('1.0', mimes[0].quality)
   84         assertEquals('1.1', mimes[0].version)
   85     }
   86 
   87     @Test
   88     void testGRAILS10678() {
   89         def mimes = getAcceptHeaderParser().parse("application/json;")
   90 
   91         assertEquals 1, mimes.size()
   92         assertEquals "application/json", mimes[0].name
   93         assertEquals "json", mimes[0].extension
   94     }
   95 
   96     @Test
   97     void testFirefox2AcceptHeaderOrdering() {
   98 
   99         def mimes = getAcceptHeaderParser().parse("text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5")
  100 
  101         assertEquals 5, mimes.size()
  102 
  103         assertEquals(['application/xhtml+xml','application/xml', 'text/html', 'text/plain', '*/*'], mimes.name)
  104         assertEquals(['html', 'xml', 'html', 'text', 'all'], mimes.extension)
  105     }
  106 
  107     @Test
  108     void testFirefox3AcceptHeaderOrdering() {
  109 
  110         def mimes = getAcceptHeaderParser().parse("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
  111 
  112         assertEquals 4, mimes.size()
  113 
  114         assertEquals(['html','html','xml', 'all'], mimes.extension)
  115     }
  116 
  117     @Test
  118     void testParseAcceptHeaderWithNonNumericQualityValue() {
  119 
  120         def mimes = getAcceptHeaderParser().parse("text/html,application/xhtml+xml,application/xml;q=blah,*/*;q=0.8")
  121 
  122         assertEquals 4, mimes.size()
  123 
  124         assertEquals(['html','html','xml', 'all'], mimes.extension)
  125     }
  126 
  127     @Test
  128     void testAcceptHeaderWithQNumberOrdering() {
  129         def mimes = getAcceptHeaderParser().parse("text/html,application/xhtml+xml,application/xml;q=1.1,*/*;q=0.8")
  130 
  131         assertEquals 4, mimes.size()
  132 
  133         assertEquals(['xml','html','html', 'all'], mimes.extension)
  134     }
  135 
  136     @Test
  137     void testPrototypeHeaderOrdering() {
  138         def mimes = getAcceptHeaderParser().parse("text/javascript, text/html, application/xml, text/xml, */*")
  139 
  140         assertEquals 4, mimes.size()
  141 
  142         assertEquals(["js",'html', 'xml', 'all'], mimes.extension)
  143         assertEquals(["text/javascript",'text/html', 'application/xml', '*/*'], mimes.name)
  144     }
  145 
  146     @Test
  147     void testOldBrowserHeader() {
  148         def mimes = getAcceptHeaderParser().parse("*/*")
  149 
  150         assertEquals 1, mimes.size()
  151         assertEquals(['all'], mimes.extension)
  152     }
  153 
  154     @Test
  155     // test for GRAILS-3389
  156     void testAcceptExtensionWithTokenNoValue() {
  157         def mimes = getAcceptHeaderParser().parse("text/html,application/xhtml+xml,application/xml;token,*/*;q=0.8")
  158 
  159         assertEquals 4, mimes.size()
  160 
  161         assertEquals(['html','html', 'xml','all'], mimes.extension)
  162     }
  163 
  164     @Test
  165     // test for GRAILS-3493
  166     void testAcceptHeaderWithNoQValue() {
  167         def mimes = getAcceptHeaderParser().parse("application/xml; charset=UTF-8")
  168 
  169         assertEquals 1, mimes.size()
  170     }
  171 
  172     @Test
  173     void testAcceptExtensionWithCustomVndTypeAndVersion() {
  174         def mimesV1 = getAcceptHeaderParser().parse("application/vnd.foo+json;v=1.0; charset=UTF-8")
  175         def mimesV2 = getAcceptHeaderParser().parse("application/vnd.foo+json;v=2.0; charset=UTF-8")
  176 
  177         assertEquals(['foov1'], mimesV1.extension)
  178         assertEquals(['foov2'], mimesV2.extension)
  179     }
  180 }