package org.codehaus.groovy.grails.web.taglib; import org.codehaus.groovy.grails.commons.ApplicationHolder import javax.servlet.http.Cookie import org.springframework.mock.web.MockHttpServletResponse import javax.servlet.http.HttpServletResponse import org.springframework.mock.web.MockHttpServletRequest import org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib import org.codehaus.groovy.grails.commons.ConfigurationHolder class ApplicationTagLibTests extends AbstractGrailsTagTests { void testResourceTag() { request.contextPath = '/test' def template = '${resource(file:"images/foo.jpg")}' assertOutputEquals '/test/images/foo.jpg', template template = '${resource(dir:"images",file:"foo.jpg")}' assertOutputEquals '/test/images/foo.jpg', template } void testUseJessionIdWithCreateLink() { def response = new JsessionIdMockHttpServletResponse() ApplicationTagLib.metaClass.getResponse = {-> response} def tagLibBean = appCtx.getBean("org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib") ga.config.grails.views.enable.jsessionid=true tagLibBean.afterPropertiesSet() assertTrue( tagLibBean.@useJsessionId ) def template = '' assertOutputEquals "/foo/test;jsessionid=test", template ga.config.grails.views.enable.jsessionid=false tagLibBean.afterPropertiesSet() assertFalse( tagLibBean.@useJsessionId ) assertOutputEquals "/foo/test", template } void testObtainCookieValue() { def cookie = new Cookie("foo", "bar") request.cookies = [cookie] as Cookie[] def template = '' assertOutputEquals "bar", template template = '${cookie(name:"foo")}' assertOutputEquals "bar", template } void testObtainHeaderValue() { request.addHeader "FOO", "BAR" def template = '' assertOutputEquals "BAR", template template = '${header(name:"FOO")}' assertOutputEquals "BAR", template } void testClonedUrlFromVariable() { def template = '''${urlMap.controller},${urlMap.action}test${urlMap.controller},${urlMap.action}''' assertOutputEquals('test,justdoittesttest,justdoit', template) } void testLinkWithMultipleParameters() { def template = 'test' assertOutputEquals('test', template) } void testLinkWithFragment() { def template = 'link' profile("link rendering") { assertOutputEquals('link', template) } } void testCreateLinkWithFlowExecutionKeyAndEvent() { request.flowExecutionKey = '12345' def template = '' assertOutputEquals('/foo/bar?execution=12345&_eventId=boo', template) } void testLinkWithFlowExecutionKeyAndEvent() { request.flowExecutionKey = '12345' def template = 'link' assertOutputEquals('link', template) } void testSetTag() { def template = 'one: ${one}' assertOutputEquals('one: two', template) } void testSetTagWithBody() { def template = 'twoone: ${one}' assertOutputEquals('one: two', template) } void testSetTagWithMap() { def template = '${e?.b}' assertOutputEquals('null', template, [c:[:]]) assertOutputEquals('foo', template, [c:[a:[b:'foo']]]) } void testIteration() { def template = ''' ${counter}''' printCompiledSource template assertOutputEquals('112234', template, [:], { it.toString().trim() }) } void testMetaTag() { def template = '' assertOutputEquals('0.9.9.1', template) } void testCreateLinkToWithDirAndLeadingSlash() { def template = '' assertOutputEquals "/images/foo.jpg", template } void testCreateLinkToWithDirAndLeadingNoLeadingSlash() { def template = '' assertOutputEquals "/images/foo.jpg", template } void testCreateLinkToWithFileAndLeadingSlash() { def template = '' assertOutputEquals "/images/foo.jpg", template } void testCreateLinkTo() { StringWriter sw = new StringWriter(); withTag("createLinkTo", sw) { tag -> def attrs = [dir:'test'] tag.call( attrs ) assertEquals '/test', sw.toString() sw.getBuffer().delete(0,sw.getBuffer().length()); attrs = [dir:'test',file:'file'] tag.call( attrs ) assertEquals '/test/file', sw.toString() sw.getBuffer().delete(0,sw.getBuffer().length()); attrs = [dir:''] tag.call( attrs ) println sw.toString() assertEquals '', sw.toString() } } void testCreateLinkToFilesInRoot() { StringWriter sw = new StringWriter(); withTag("createLinkTo", sw) { tag -> def attrs = [dir:'/', file:'test.gsp'] tag.call( attrs ) assertEquals '/test.gsp', sw.toString() } } void testCreateLinkToFilesInRootWithContext() { StringWriter sw = new StringWriter(); request.contextPath = "/foo" withTag("createLinkTo", sw) { tag -> def attrs = [dir:'/', file:'test.gsp'] tag.call( attrs ) assertEquals '/foo/test.gsp', sw.toString() } } void testCreateLinkWithZeroId() { // test case for GRAILS-1123 StringWriter sw = new StringWriter(); withTag("createLink", sw) { tag -> def attrs = [action:'testAction', controller: 'testController', id:0] tag.call( attrs ) assertEquals '/testController/testAction/0', sw.toString() } } void testCreateLinkURLEncoding() { StringWriter sw = new StringWriter(); withTag("createLink", sw) { tag -> // test URL encoding. Params unordered to have to try one test at a time def attrs = [action:'testAction', controller: 'testController', params:['name':'Marc Palmer']] tag.call( attrs ) assertEquals '/testController/testAction?name=Marc+Palmer', sw.toString() } } void testCreateLinkURLEncodingWithHTMLChars() { StringWriter sw = new StringWriter(); withTag("createLink", sw) { tag -> // test URL encoding is done but HTML encoding isn't, only want the one here. def attrs = [action:'testAction', controller: 'testController', params:['email':'']] tag.call( attrs ) assertEquals '/testController/testAction?email=%3Cmarc%40anyware.co.uk%3E', sw.toString() } } void testCreateLinkWithBase() { StringWriter sw = new StringWriter(); withTag("createLink", sw) { tag -> // test URL encoding. Params unordered to have to try one test at a time def attrs = [base:"http://www128.myhost.com:3495", action:'testAction', controller: 'testController'] tag.call( attrs ) assertEquals 'http://www128.myhost.com:3495/testController/testAction', sw.toString() } } void testAbsoluteWithContextPath() { request.contextPath = "/foo" def template = '' assertOutputEquals 'http://localhost:8080/testController/testAction', template ConfigurationHolder.config.grails.serverURL="http://www.mysite.com" assertOutputEquals 'http://www.mysite.com/testController/testAction', template ConfigurationHolder.config.grails.serverURL=null } /** * Tests regression of GRAILS-3368. * The context path should not be included in the generated link * if "base" is set to an empty string. */ void testCreateLinkWithNoContextPath() { StringWriter sw = new StringWriter(); withTag("createLink", sw) { tag -> def attrs = [base: "", action:'testAction', controller: 'testController'] tag.call( attrs ) assertEquals '/testController/testAction', sw.toString() } } void testCreateLinkWithAbsolute() { StringWriter sw = new StringWriter(); withTag("createLink", sw) { tag -> // test URL encoding. Params unordered to have to try one test at a time def attrs = [absolute:"true", action:'testAction', controller: 'testController'] tag.call( attrs ) assertEquals 'http://localhost:8080/testController/testAction', sw.toString() } } } class JsessionIdMockHttpServletResponse extends MockHttpServletResponse { public String encodeURL(String url) { return super.encodeURL("$url;jsessionid=test"); } }