/
ApplicationTagLibTests.groovy
770 lines (616 loc) · 37.2 KB
/
ApplicationTagLibTests.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
package org.codehaus.groovy.grails.web.taglib
import grails.util.GrailsUtil
import grails.util.MockRequestDataValueProcessor
import javax.servlet.http.Cookie
import org.codehaus.groovy.grails.commons.ConfigurationHolder
import org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib
import org.codehaus.groovy.grails.support.MockApplicationContext
import org.codehaus.groovy.grails.web.pages.GroovyPageBinding
import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes
import org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException
import org.springframework.mock.web.MockHttpServletResponse
class ApplicationTagLibTests extends AbstractGrailsTagTests {
@Override
protected void setUp() {
super.setUp()
appCtx.getBean(ApplicationTagLib.name).requestDataValueProcessor = new MockRequestDataValueProcessor()
}
void testResourceTagWithPluginAttribute() {
unRegisterRequestDataValueProcessor()
request.contextPath = '/test'
def template = '${resource(file:"images/foo.jpg", plugin:"controllers")}'
assertOutputEquals "/test/plugins/controllers-${GrailsUtil.getGrailsVersion()}/images/foo.jpg", template
}
void testResourceTagWithPluginAttributeAndRequestDataValueProcessor() {
request.contextPath = '/test'
def template = '${resource(file:"images/foo.jpg", plugin:"controllers")}'
assertOutputEquals "/test/plugins/controllers-${GrailsUtil.getGrailsVersion()}/images/foo.jpg?requestDataValueProcessorParamName=paramValue", template
}
void testResourceTagWithImplicitPlugin() {
unRegisterRequestDataValueProcessor()
def template = '${resource(file:"images/foo.jpg")}'
request.setAttribute(GrailsApplicationAttributes.PAGE_SCOPE, new GroovyPageBinding("/plugin/one"))
assertOutputEquals "/plugin/one/images/foo.jpg", template
}
void testResourceTagWithImplicitPluginAndRequestDataValueProcessor() {
def template = '${resource(file:"images/foo.jpg")}'
request.setAttribute(GrailsApplicationAttributes.PAGE_SCOPE, new GroovyPageBinding("/plugin/one"))
assertOutputEquals "/plugin/one/images/foo.jpg?requestDataValueProcessorParamName=paramValue", template
}
void testResourceTagWithContextPathAttribute() {
unRegisterRequestDataValueProcessor()
request.contextPath = '/test'
def template = '${resource(file:"images/foo.jpg", contextPath:"/foo")}'
assertOutputEquals "/foo/images/foo.jpg", template
request.setAttribute(GrailsApplicationAttributes.PAGE_SCOPE, new GroovyPageBinding("/plugin/one"))
assertOutputEquals "/foo/images/foo.jpg", template
}
void testResourceTagWithContextPathAttributeAndRequestDataValueProcessor() {
request.contextPath = '/test'
def template = '${resource(file:"images/foo.jpg", contextPath:"/foo")}'
assertOutputEquals "/foo/images/foo.jpg?requestDataValueProcessorParamName=paramValue", template
request.setAttribute(GrailsApplicationAttributes.PAGE_SCOPE, new GroovyPageBinding("/plugin/one"))
assertOutputEquals "/foo/images/foo.jpg?requestDataValueProcessorParamName=paramValue", template
}
void testResourceTagWithPluginAttributeAndNone() {
unRegisterRequestDataValueProcessor()
request.contextPath = '/test'
def template = '${resource(file:"images/foo.jpg", plugin:"none")}'
assertOutputEquals "/test/images/foo.jpg", template
}
void testResourceTagWithPluginAttributeAndNoneAndRequestDataValueProcessor() {
request.contextPath = '/test'
def template = '${resource(file:"images/foo.jpg", plugin:"none")}'
assertOutputEquals "/test/images/foo.jpg?requestDataValueProcessorParamName=paramValue", template
}
void testResourceTag() {
unRegisterRequestDataValueProcessor()
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 testResourceTagAndRequestDataValueProcessor() {
request.contextPath = '/test'
def template = '${resource(file:"images/foo.jpg")}'
assertOutputEquals '/test/images/foo.jpg?requestDataValueProcessorParamName=paramValue', template
template = '${resource(dir:"images",file:"foo.jpg")}'
assertOutputEquals '/test/images/foo.jpg?requestDataValueProcessorParamName=paramValue', template
}
void testResourceTagDirOnly() {
unRegisterRequestDataValueProcessor()
request.contextPath = '/test'
def template = '${resource(dir:"jquery")}'
assertOutputEquals '/test/jquery', template
}
void testResourceTagDirOnlyAndRequestDataValueProcessor() {
request.contextPath = '/test'
def template = '${resource(dir:"jquery")}'
assertOutputEquals '/test/jquery?requestDataValueProcessorParamName=paramValue', template
}
void testUseJessionIdWithCreateLink() {
unRegisterRequestDataValueProcessor()
def response = new JsessionIdMockHttpServletResponse()
ApplicationTagLib.metaClass.getResponse = {-> response}
def tagLibBean = appCtx.getBean(ApplicationTagLib.name)
ga.config.grails.views.enable.jsessionid=true
tagLibBean.afterPropertiesSet()
assertTrue(tagLibBean.@useJsessionId)
def template = '<g:createLink controller="foo" action="test" />'
assertOutputEquals "/foo/test;jsessionid=test", template
ga.config.grails.views.enable.jsessionid=false
tagLibBean.afterPropertiesSet()
assertFalse(tagLibBean.@useJsessionId)
assertOutputEquals "/foo/test", template
}
void testUseJessionIdWithCreateLinkAndRequestDataValueProcessor() {
def response = new JsessionIdMockHttpServletResponse()
ApplicationTagLib.metaClass.getResponse = {-> response}
def tagLibBean = appCtx.getBean(ApplicationTagLib.name)
ga.config.grails.views.enable.jsessionid=true
tagLibBean.afterPropertiesSet()
assertTrue(tagLibBean.@useJsessionId)
def template = '<g:createLink controller="foo" action="test" />'
assertOutputEquals "/foo/test?requestDataValueProcessorParamName=paramValue;jsessionid=test", template
ga.config.grails.views.enable.jsessionid=false
tagLibBean.afterPropertiesSet()
assertFalse(tagLibBean.@useJsessionId)
assertOutputEquals "/foo/test?requestDataValueProcessorParamName=paramValue", template
}
void testObtainCookieValue() {
def cookie = new Cookie("foo", "bar")
request.cookies = [cookie] as Cookie[]
def template = '<g:cookie name="foo" />'
assertOutputEquals "bar", template
template = '${cookie(name:"foo")}'
assertOutputEquals "bar", template
}
void testObtainHeaderValue() {
request.addHeader "FOO", "BAR"
def template = '<g:header name="FOO" />'
assertOutputEquals "BAR", template
template = '${header(name:"FOO")}'
assertOutputEquals "BAR", template
}
void testClonedUrlFromVariable() {
unRegisterRequestDataValueProcessor()
def template = '''<g:set var="urlMap" value="${[controller: 'test', action: 'justdoit']}"/>${urlMap.controller},${urlMap.action}<g:link url="${urlMap}">test</g:link>${urlMap.controller},${urlMap.action}'''
assertOutputEquals('test,justdoit<a href="/test/justdoit">test</a>test,justdoit', template)
}
void testLinkWithMultipleParameters() {
unRegisterRequestDataValueProcessor()
def template = '<g:link controller="foo" action="action" params="[test: \'test\', test2: \'test2\']">test</g:link>'
assertOutputEquals('<a href="/foo/action?test=test&test2=test2">test</a>', template)
// test caching too
assertOutputEquals('<a href="/foo/action?test=test&test2=test2">test</a>', template)
}
void testLinkWithMultipleParametersAndRequestDataValueProcessor() {
def template = '<g:link controller="foo" action="action" params="[test: \'test\', test2: \'test2\']">test</g:link>'
assertOutputEquals('<a href="/foo/action?test=test&test2=test2&requestDataValueProcessorParamName=paramValue">test</a>', template)
// test caching too
assertOutputEquals('<a href="/foo/action?test=test&test2=test2&requestDataValueProcessorParamName=paramValue">test</a>', template)
}
void testLinkWithMultipleCollectionParameters() {
unRegisterRequestDataValueProcessor()
def template = '<g:link controller="foo" action="action" params="[test: [\'test-a\',\'test-b\'], test2: [\'test2-a\',\'test2-b\'] as String[]]">test</g:link>'
assertOutputEquals('<a href="/foo/action?test=test-a&test=test-b&test2=test2-a&test2=test2-b">test</a>', template)
// test caching too
assertOutputEquals('<a href="/foo/action?test=test-a&test=test-b&test2=test2-a&test2=test2-b">test</a>', template)
}
void testLinkWithMultipleCollectionParametersAndRequestDataValueProcessor() {
def template = '<g:link controller="foo" action="action" params="[test: [\'test-a\',\'test-b\'], test2: [\'test2-a\',\'test2-b\'] as String[]]">test</g:link>'
assertOutputEquals('<a href="/foo/action?test=test-a&test=test-b&test2=test2-a&test2=test2-b&requestDataValueProcessorParamName=paramValue">test</a>', template)
// test caching too
assertOutputEquals('<a href="/foo/action?test=test-a&test=test-b&test2=test2-a&test2=test2-b&requestDataValueProcessorParamName=paramValue">test</a>', template)
}
void testLinkWithCollectionParameter() {
unRegisterRequestDataValueProcessor()
def template = '<g:link controller="foo" action="action" params="[test: [\'test-a\']]">test</g:link>'
assertOutputEquals('<a href="/foo/action?test=test-a">test</a>', template)
// test caching too
assertOutputEquals('<a href="/foo/action?test=test-a">test</a>', template)
}
void testLinkWithCollectionParameterAndRequestDataValueProcessor() {
def template = '<g:link controller="foo" action="action" params="[test: [\'test-a\']]">test</g:link>'
assertOutputEquals('<a href="/foo/action?test=test-a&requestDataValueProcessorParamName=paramValue">test</a>', template)
// test caching too
assertOutputEquals('<a href="/foo/action?test=test-a&requestDataValueProcessorParamName=paramValue">test</a>', template)
}
void testLinkWithCharCollectionParameter() {
unRegisterRequestDataValueProcessor()
def template = '<g:link controller="foo" action="action" params="[letter: [\'a\' as char]]">test</g:link>'
assertOutputEquals('<a href="/foo/action?letter=a">test</a>', template)
// test caching too
assertOutputEquals('<a href="/foo/action?letter=a">test</a>', template)
}
void testLinkWithCharCollectionParameterAndRequestDataValueProcessor() {
def template = '<g:link controller="foo" action="action" params="[letter: [\'a\' as char]]">test</g:link>'
assertOutputEquals('<a href="/foo/action?letter=a&requestDataValueProcessorParamName=paramValue">test</a>', template)
// test caching too
assertOutputEquals('<a href="/foo/action?letter=a&requestDataValueProcessorParamName=paramValue">test</a>', template)
}
void testCreateLinkWithCollectionParameter() {
unRegisterRequestDataValueProcessor()
def template = '<% l=\'a\' %>${g.createLink(controller:"foo", action:"action", params:[letter:[l]])} ${g.createLink(controller:"foo", action:"action", params:[letter:[l]])}'
assertOutputEquals('/foo/action?letter=a /foo/action?letter=a', template)
// test caching too
assertOutputEquals('/foo/action?letter=a /foo/action?letter=a', template)
}
void testCreateLinkWithCollectionParameterAndRequestDataValueProcessor() {
def template = '<% l=\'a\' %>${g.createLink(controller:"foo", action:"action", params:[letter:[l]])} ${g.createLink(controller:"foo", action:"action", params:[letter:[l]])}'
assertOutputEquals('/foo/action?letter=a&requestDataValueProcessorParamName=paramValue /foo/action?letter=a&requestDataValueProcessorParamName=paramValue', template)
// test caching too
assertOutputEquals('/foo/action?letter=a&requestDataValueProcessorParamName=paramValue /foo/action?letter=a&requestDataValueProcessorParamName=paramValue', template)
}
void testLikeWithElementId() {
unRegisterRequestDataValueProcessor()
def template = '<g:link elementId="myId" controller="foo" action="list">bar</g:link>'
assertOutputEquals('<a href="/foo/list" id="myId">bar</a>', template)
}
void testLikeWithElementIdAndRequestDataValueProcessor() {
def template = '<g:link elementId="myId" controller="foo" action="list">bar</g:link>'
assertOutputEquals('<a href="/foo/list?requestDataValueProcessorParamName=paramValue" id="myId">bar</a>', template)
}
void testLinkWithMultipleParametersAndElementId() {
unRegisterRequestDataValueProcessor()
def template = '<g:link elementId="myid" controller="foo" action="action" params="[test: \'test\', test2: \'test2\']">test</g:link>'
assertOutputEquals('<a href="/foo/action?test=test&test2=test2" id="myid">test</a>', template)
}
void testLinkWithMultipleParametersAndElementIdAndRequestDataValueProcessor() {
def template = '<g:link elementId="myid" controller="foo" action="action" params="[test: \'test\', test2: \'test2\']">test</g:link>'
assertOutputEquals('<a href="/foo/action?test=test&test2=test2&requestDataValueProcessorParamName=paramValue" id="myid">test</a>', template)
}
void testLinkWithFragment() {
unRegisterRequestDataValueProcessor()
def template = '<g:link controller="foo" action="bar" fragment="test">link</g:link>'
profile("link rendering") {
assertOutputEquals('<a href="/foo/bar#test">link</a>', template)
}
}
void testLinkWithFragmentAndRequestDataValueProcessor() {
def template = '<g:link controller="foo" action="bar" fragment="test">link</g:link>'
profile("link rendering") {
assertOutputEquals('<a href="/foo/bar?requestDataValueProcessorParamName=paramValue#test">link</a>', template)
}
}
void testCreateLinkWithFlowExecutionKeyAndEvent() {
unRegisterRequestDataValueProcessor()
request.flowExecutionKey = '12345'
def template = '<g:createLink controller="foo" action="bar" event="boo" />'
assertOutputEquals('/foo/bar?execution=12345&_eventId=boo', template)
}
void testCreateLinkWithFlowExecutionKeyAndEventAndRequestDataValueProcessor() {
request.flowExecutionKey = '12345'
def template = '<g:createLink controller="foo" action="bar" event="boo" />'
assertOutputEquals('/foo/bar?execution=12345&_eventId=boo&requestDataValueProcessorParamName=paramValue', template)
}
void testLinkWithFlowExecutionKeyAndEvent() {
unRegisterRequestDataValueProcessor()
request.flowExecutionKey = '12345'
def template = '<g:link controller="foo" action="bar" event="boo" >link</g:link>'
assertOutputEquals('<a href="/foo/bar?execution=12345&_eventId=boo">link</a>', template)
}
void testLinkWithFlowExecutionKeyAndEventAndRequestDataValueProcessor() {
request.flowExecutionKey = '12345'
def template = '<g:link controller="foo" action="bar" event="boo" >link</g:link>'
assertOutputEquals('<a href="/foo/bar?execution=12345&_eventId=boo&requestDataValueProcessorParamName=paramValue">link</a>', template)
}
void testSetTag() {
def template = '<g:set var="one" value="two" />one: ${one}'
assertOutputEquals('one: two', template)
}
void testSetTagWithBody() {
def template = '<g:set var="one">two</g:set>one: ${one}'
assertOutputEquals('one: two', template)
}
void testSetTagWithMap() {
def template = '<g:set var="e" value="${c.a}"/>${e?.b}'
assertOutputEquals('', template, [c:[:]])
assertOutputEquals('foo', template, [c:[a:[b:'foo']]])
}
void testSetTagWithScope() {
def template = '<g:set var="var1" value="1"/>${var1}<g:set var="var1" value="2"/> ${var1}<g:set var="var2" value="3" scope="request"/> ${var2}<g:set var="var2" value="4" scope="request"/> ${var2}'
assertOutputEquals('1 2 3 4', template)
}
void testSetTagWithBeanName() {
def template = '<g:set bean="grailsApplication" var="myVar"/>${myVar.initialised}'
assertOutputEquals('true', template)
template = '<g:set bean="grailsApplication" var="myRequestVar" scope="request"/>${request.myRequestVar.initialised}'
assertOutputEquals('true', template)
}
void testSetTagWithBeanType() {
def template = '<%@ page import="org.codehaus.groovy.grails.commons.*" %><g:set bean="${GrailsApplication}" var="myRequestVar" scope="request"/>${request.myRequestVar.initialised}'
assertOutputEquals('true', template)
}
void testIteration() {
def template = '''<g:set var="counter" value="${1}" />
<g:each in="${[10,11,12]}" var="myVal"><g:set var="counter" value="${myVal+counter}" />${counter}</g:each>'''
printCompiledSource template
assertOutputEquals('112234', template, [:], { it.toString().trim() })
}
void testMetaTag() {
def template = '<g:meta name="app.version"/>'
assertOutputEquals('0.9.9.1', template)
}
void testCreateLinkToWithDirAndLeadingSlash() {
unRegisterRequestDataValueProcessor()
request.contextPath = "/"
def template = '<g:createLinkTo dir="/images" file="foo.jpg" />'
assertOutputEquals "/images/foo.jpg", template
}
void testCreateLinkToWithDirAndLeadingSlashAndRequestDataValueProcessor() {
request.contextPath = "/"
def template = '<g:createLinkTo dir="/images" file="foo.jpg" />'
assertOutputEquals "/images/foo.jpg?requestDataValueProcessorParamName=paramValue", template
}
void testCreateLinkToWithDirAndLeadingNoLeadingSlash() {
unRegisterRequestDataValueProcessor()
def template = '<g:createLinkTo dir="images" file="foo.jpg" />'
assertOutputEquals "/images/foo.jpg", template
}
void testCreateLinkToWithDirAndLeadingNoLeadingSlashAndRequestDataValueProcessor() {
def template = '<g:createLinkTo dir="images" file="foo.jpg" />'
assertOutputEquals "/images/foo.jpg?requestDataValueProcessorParamName=paramValue", template
}
void testCreateLinkToWithFileAndLeadingSlash() {
unRegisterRequestDataValueProcessor()
def template = '<g:createLinkTo dir="/images" file="/foo.jpg" />'
assertOutputEquals "/images/foo.jpg", template
}
void testCreateLinkToWithFileAndLeadingSlashAndRequestDataValueProcessor() {
def template = '<g:createLinkTo dir="/images" file="/foo.jpg" />'
assertOutputEquals "/images/foo.jpg?requestDataValueProcessorParamName=paramValue", template
}
void testCreateLinkTo() {
unRegisterRequestDataValueProcessor()
def template = '<g:resource dir="test" />'
assertOutputEquals '/test', template
template = '<g:resource dir="test" file="file" />'
assertOutputEquals '/test/file', template
template = '<g:resource dir="" />'
assertOutputEquals '', template
}
void testCreateLinkToWithRequestDataValueProcessor() {
def template = '<g:resource dir="test" />'
assertOutputEquals '/test?requestDataValueProcessorParamName=paramValue', template
template = '<g:resource dir="test" file="file" />'
assertOutputEquals '/test/file?requestDataValueProcessorParamName=paramValue', template
template = '<g:resource dir="" />'
assertOutputEquals '', template
}
void testCreateLinkToFilesInRoot() {
unRegisterRequestDataValueProcessor()
def template = '<g:resource dir="/" file="test.gsp" />'
assertOutputEquals '/test.gsp', template
}
void testCreateLinkToFilesInRootWithRequestDataValueProcessor() {
def template = '<g:resource dir="/" file="test.gsp" />'
assertOutputEquals '/test.gsp?requestDataValueProcessorParamName=paramValue', template
}
void testResourceFilesInRootWithContext() {
unRegisterRequestDataValueProcessor()
def template = '<g:resource dir="/" file="test.gsp" />'
request.contextPath = "/foo"
assertOutputEquals '/foo/test.gsp', template
}
void testResourceFilesInRootWithContextAndRequestDataValueProcessor() {
def template = '<g:resource dir="/" file="test.gsp" />'
request.contextPath = "/foo"
assertOutputEquals '/foo/test.gsp?requestDataValueProcessorParamName=paramValue', template
}
void testCreateLinkWithZeroId() {
unRegisterRequestDataValueProcessor()
def template = '<g:createLink action="testAction" controller="testController" id="${id}" />'
assertOutputEquals '/testController/testAction/0', template, [id:0]
}
void testCreateLinkWithZeroIdAndRequestDataValueProcessor() {
def template = '<g:createLink action="testAction" controller="testController" id="${id}" />'
assertOutputEquals '/testController/testAction/0?requestDataValueProcessorParamName=paramValue', template, [id:0]
}
void testCreateLinkURLEncoding() {
unRegisterRequestDataValueProcessor()
def template = '<g:createLink action="testAction" controller="testController" params="[name:\'Marc Palmer\']" />'
assertOutputEquals '/testController/testAction?name=Marc+Palmer', template
}
void testCreateLinkURLEncodingWithRequestDataValueProcessor() {
def template = '<g:createLink action="testAction" controller="testController" params="[name:\'Marc Palmer\']" />'
assertOutputEquals '/testController/testAction?name=Marc+Palmer&requestDataValueProcessorParamName=paramValue', template
}
void testCreateLinkURLEncodingWithHTMLChars() {
unRegisterRequestDataValueProcessor()
def template = '<g:createLink action="testAction" controller="testController" params="[email:email]" />'
assertOutputEquals '/testController/testAction?email=%3Cmarc%40anyware.co.uk%3E', template, [email:'<marc@anyware.co.uk>']
}
void testCreateLinkURLEncodingWithHTMLCharsAndRequestDataValueProcessor() {
def template = '<g:createLink action="testAction" controller="testController" params="[email:email]" />'
assertOutputEquals '/testController/testAction?email=%3Cmarc%40anyware.co.uk%3E&requestDataValueProcessorParamName=paramValue', template, [email:'<marc@anyware.co.uk>']
}
void testCreateLinkWithBase() {
unRegisterRequestDataValueProcessor()
def template = '<g:createLink base="http://www128.myhost.com:3495" action="testAction" controller="testController" />'
assertOutputEquals 'http://www128.myhost.com:3495/testController/testAction', template
}
void testCreateLinkWithBaseAndRequestDataValueProcessor() {
def template = '<g:createLink base="http://www128.myhost.com:3495" action="testAction" controller="testController" />'
assertOutputEquals 'http://www128.myhost.com:3495/testController/testAction?requestDataValueProcessorParamName=paramValue', template
}
void testCreateLinkWithUseJSessionIdAndContextPapth() {
unRegisterRequestDataValueProcessor()
request.contextPath = "/foo"
def taglib = appCtx.getBean(ApplicationTagLib.name)
taglib.useJsessionId = true
def template = '<g:createLink action="testAction" controller="testController" />'
assertOutputEquals '/foo/testController/testAction', template
}
void testCreateLinkWithUseJSessionIdAndContextPapthAndRequestDataValueProcessor() {
request.contextPath = "/foo"
def taglib = appCtx.getBean(ApplicationTagLib.name)
taglib.useJsessionId = true
def template = '<g:createLink action="testAction" controller="testController" />'
assertOutputEquals '/foo/testController/testAction?requestDataValueProcessorParamName=paramValue', template
}
void testCreateLinkWithContextPath() {
unRegisterRequestDataValueProcessor()
request.contextPath = "/foo"
def template = '<g:createLink action="testAction" controller="testController" />'
assertOutputEquals '/foo/testController/testAction', template
}
void testCreateLinkWithContextPathAndRequestDataValueProcessor() {
request.contextPath = "/foo"
def template = '<g:createLink action="testAction" controller="testController" />'
assertOutputEquals '/foo/testController/testAction?requestDataValueProcessorParamName=paramValue', template
}
void testAbsoluteWithContextPath() {
unRegisterRequestDataValueProcessor()
request.contextPath = "/foo"
def template = '<g:createLink action="testAction" controller="testController" absolute="true" />'
def linkGenerator = appCtx.getBean("grailsLinkGenerator")
linkGenerator.configuredServerBaseURL="http://www.mysite.com"
assertOutputEquals 'http://www.mysite.com/testController/testAction', template
template = '<g:createLink action="testAction" controller="testController" absolute="${true}" />'
linkGenerator = appCtx.getBean("grailsLinkGenerator")
linkGenerator.configuredServerBaseURL="http://www.mysite.com"
assertOutputEquals 'http://www.mysite.com/testController/testAction', template
}
void testAbsoluteWithContextPathAndRequestDataValueProcessor() {
request.contextPath = "/foo"
def template = '<g:createLink action="testAction" controller="testController" absolute="true" />'
def linkGenerator = appCtx.getBean("grailsLinkGenerator")
linkGenerator.configuredServerBaseURL="http://www.mysite.com"
assertOutputEquals 'http://www.mysite.com/testController/testAction?requestDataValueProcessorParamName=paramValue', template
template = '<g:createLink action="testAction" controller="testController" absolute="${true}" />'
linkGenerator = appCtx.getBean("grailsLinkGenerator")
linkGenerator.configuredServerBaseURL="http://www.mysite.com"
assertOutputEquals 'http://www.mysite.com/testController/testAction?requestDataValueProcessorParamName=paramValue', template
}
void testAbsoluteFalseWithContextPath() {
unRegisterRequestDataValueProcessor()
request.contextPath = "/foo"
def template = '<g:createLink action="testAction" controller="testController" absolute="false" />'
def linkGenerator = appCtx.getBean("grailsLinkGenerator")
linkGenerator.configuredServerBaseURL="http://www.mysite.com"
assertOutputEquals '/foo/testController/testAction', template
template = '<g:createLink action="testAction" controller="testController" absolute="${false}" />'
linkGenerator = appCtx.getBean("grailsLinkGenerator")
linkGenerator.configuredServerBaseURL="http://www.mysite.com"
assertOutputEquals '/foo/testController/testAction', template
}
void testAbsoluteFalseWithContextPathAndRequestDataValueProcessor() {
request.contextPath = "/foo"
def template = '<g:createLink action="testAction" controller="testController" absolute="false" />'
def linkGenerator = appCtx.getBean("grailsLinkGenerator")
linkGenerator.configuredServerBaseURL="http://www.mysite.com"
assertOutputEquals '/foo/testController/testAction?requestDataValueProcessorParamName=paramValue', template
template = '<g:createLink action="testAction" controller="testController" absolute="${false}" />'
linkGenerator = appCtx.getBean("grailsLinkGenerator")
linkGenerator.configuredServerBaseURL="http://www.mysite.com"
assertOutputEquals '/foo/testController/testAction?requestDataValueProcessorParamName=paramValue', template
}
void testAbsoluteWithContextPathAndNullConfig() {
unRegisterRequestDataValueProcessor()
ConfigurationHolder.config = null
request.contextPath = "/foo"
request.serverPort = 8080
def template = '<g:createLink action="testAction" controller="testController" absolute="true" />'
assertOutputEquals 'http://localhost:8080/foo/testController/testAction', template
template = '<g:createLink action="testAction" controller="testController" absolute="${true}" />'
assertOutputEquals 'http://localhost:8080/foo/testController/testAction', template
}
void testAbsoluteWithContextPathAndNullConfigAndRequestDataValueProcessor() {
ConfigurationHolder.config = null
request.contextPath = "/foo"
request.serverPort = 8080
def template = '<g:createLink action="testAction" controller="testController" absolute="true" />'
assertOutputEquals 'http://localhost:8080/foo/testController/testAction?requestDataValueProcessorParamName=paramValue', template
template = '<g:createLink action="testAction" controller="testController" absolute="${true}" />'
assertOutputEquals 'http://localhost:8080/foo/testController/testAction?requestDataValueProcessorParamName=paramValue', template
}
void testAbsoluteFalseWithContextPathAndNullConfig() {
unRegisterRequestDataValueProcessor()
ConfigurationHolder.config = null
request.contextPath = "/foo"
request.serverPort = 8080
def template = '<g:createLink action="testAction" controller="testController" absolute="false" />'
assertOutputEquals '/foo/testController/testAction', template
template = '<g:createLink action="testAction" controller="testController" absolute="${false}" />'
assertOutputEquals '/foo/testController/testAction', template
}
void testAbsoluteFalseWithContextPathAndNullConfigAndRequestDataValueProcessor() {
ConfigurationHolder.config = null
request.contextPath = "/foo"
request.serverPort = 8080
def template = '<g:createLink action="testAction" controller="testController" absolute="false" />'
assertOutputEquals '/foo/testController/testAction?requestDataValueProcessorParamName=paramValue', template
template = '<g:createLink action="testAction" controller="testController" absolute="${false}" />'
assertOutputEquals '/foo/testController/testAction?requestDataValueProcessorParamName=paramValue', template
}
/**
* Tests regression of <a href="http://jira.codehaus.org/browse/GRAILS-3368">GRAILS-3368</a>.
* The context path should not be included in the generated link
* if "base" is set to an empty string.
*/
void testCreateLinkWithNoContextPath() {
unRegisterRequestDataValueProcessor()
def template = '<g:createLink base="" action="testAction" controller="testController" />'
assertOutputEquals '/testController/testAction', template
}
void testCreateLinkWithNoContextPathAndRequestDataValueProcessor() {
def template = '<g:createLink base="" action="testAction" controller="testController" />'
assertOutputEquals '/testController/testAction?requestDataValueProcessorParamName=paramValue', template
}
void testCreateLinkWithAbsolute() {
unRegisterRequestDataValueProcessor()
def template = '<g:createLink absolute="true" action="testAction" controller="testController" />'
request.serverPort = 8080
assertOutputEquals 'http://localhost:8080/testController/testAction', template
template = '<g:createLink absolute="${true}" action="testAction" controller="testController" />'
request.serverPort = 8080
assertOutputEquals 'http://localhost:8080/testController/testAction', template
}
void testCreateLinkWithAbsoluteAndRequestDataValueProcessor() {
def template = '<g:createLink absolute="true" action="testAction" controller="testController" />'
request.serverPort = 8080
assertOutputEquals 'http://localhost:8080/testController/testAction?requestDataValueProcessorParamName=paramValue', template
template = '<g:createLink absolute="${true}" action="testAction" controller="testController" />'
request.serverPort = 8080
assertOutputEquals 'http://localhost:8080/testController/testAction?requestDataValueProcessorParamName=paramValue', template
}
void testCreateLinkWithAbsoluteFalse() {
unRegisterRequestDataValueProcessor()
def template = '<g:createLink absolute="false" action="testAction" controller="testController" />'
request.serverPort = 8080
assertOutputEquals '/testController/testAction', template
template = '<g:createLink absolute="${false}" action="testAction" controller="testController" />'
request.serverPort = 8080
assertOutputEquals '/testController/testAction', template
}
void testCreateLinkWithAbsoluteFalseAndRequestDataValueProcessor() {
def template = '<g:createLink absolute="false" action="testAction" controller="testController" />'
request.serverPort = 8080
assertOutputEquals '/testController/testAction?requestDataValueProcessorParamName=paramValue', template
template = '<g:createLink absolute="${false}" action="testAction" controller="testController" />'
request.serverPort = 8080
assertOutputEquals '/testController/testAction?requestDataValueProcessorParamName=paramValue', template
}
void testJoinStrings() {
def template = /<g:join in="['Bruce', 'Adrian', 'Dave', 'Nicko', 'Steve']" delimiter="_"\/>/
assertOutputEquals 'Bruce_Adrian_Dave_Nicko_Steve', template
}
void testJoinStringsWithEmptyDelimiter() {
def template = /<g:join in="['Bruce', 'Adrian', 'Dave', 'Nicko', 'Steve']" delimiter=""\/>/
assertOutputEquals 'BruceAdrianDaveNickoSteve', template
}
void testJoinWithEmptyCollection() {
def template = /<g:join in="[]" delimiter="_"\/>/
assertOutputEquals '', template
}
void testJoinWithoutSpecifyingIn() {
def template = '<g:join delimiter="_"/>'
def msg = shouldFail(GrailsTagException) {
applyTemplate template
}
assertTrue msg?.startsWith('Tag ["join"] missing required attribute ["in"]')
}
void testJoinWithoutSpecifyingDelimiter() {
def template = /<g:join in="['Bruce', 'Adrian', 'Dave', 'Nicko', 'Steve']"\/>/
assertOutputEquals 'Bruce, Adrian, Dave, Nicko, Steve', template
}
void testImg() {
unRegisterRequestDataValueProcessor()
def template = '<g:img dir="images" file="logo.png" width="100" height="200"/>'
assertOutputEquals '<img src="/images/logo.png" width="100" height="200" />', template
template = '<g:img file="logo.png" width="100" height="200"/>'
assertOutputEquals '<img src="/images/logo.png" width="100" height="200" />', template
}
void testImgWithRequestDataValueProcessor() {
def template = '<g:img dir="images" file="logo.png" width="100" height="200"/>'
assertOutputEquals '<img src="/images/logo.png?requestDataValueProcessorParamName=paramValue" width="100" height="200" />', template
template = '<g:img file="logo.png" width="100" height="200"/>'
assertOutputEquals '<img src="/images/logo.png?requestDataValueProcessorParamName=paramValue" width="100" height="200" />', template
}
void testExternal() {
unRegisterRequestDataValueProcessor()
def template = '<g:external uri="/js/main.js"/>'
assertOutputEquals '<script src="/js/main.js" type="text/javascript"></script>\r\n', template
template = '<g:external uri="/css/style.css"/>'
assertOutputEquals '<link href="/css/style.css" type="text/css" rel="stylesheet" media="screen, projector"/>\r\n', template
template = '<g:external uri="/css/print.css" media="print"/>'
assertOutputEquals '<link href="/css/print.css" type="text/css" rel="stylesheet" media="print"/>\r\n', template
template = '<g:external uri="/images/icons/iphone-icon.png" type="appleicon"/>'
assertOutputEquals '<link href="/images/icons/iphone-icon.png" rel="apple-touch-icon"/>\r\n', template
}
void testExternalWithRequestDataValueProcessor() {
def template = '<g:external uri="/js/main.js"/>'
assertOutputEquals '<script src="/js/main.js?requestDataValueProcessorParamName=paramValue" type="text/javascript"></script>\r\n', template
template = '<g:external uri="/css/style.css"/>'
assertOutputEquals '<link href="/css/style.css?requestDataValueProcessorParamName=paramValue" type="text/css" rel="stylesheet" media="screen, projector"/>\r\n', template
template = '<g:external uri="/css/print.css" media="print"/>'
assertOutputEquals '<link href="/css/print.css?requestDataValueProcessorParamName=paramValue" type="text/css" rel="stylesheet" media="print"/>\r\n', template
template = '<g:external uri="/images/icons/iphone-icon.png" type="appleicon"/>'
assertOutputEquals '<link href="/images/icons/iphone-icon.png?requestDataValueProcessorParamName=paramValue" rel="apple-touch-icon"/>\r\n', template
}
private void unRegisterRequestDataValueProcessor() {
appCtx.getBean(ApplicationTagLib.name).requestDataValueProcessor = null
}
}