xref: /unit/test/test_java_application.py (revision 977:4f9268f27b57)
1import time
2import unittest
3import unit
4
5class TestUnitJavaApplication(unit.TestUnitApplicationJava):
6
7    def setUpClass():
8        unit.TestUnit().check_modules('java')
9
10    def test_java_application_cookies(self):
11        self.load('cookies')
12
13        headers = self.get(headers={
14            'Cookie': 'var1=val1; var2=val2',
15            'Host': 'localhost',
16            'Connection': 'close'
17        })['headers']
18
19        self.assertEqual(headers['X-Cookie-1'], 'val1', 'cookie 1')
20        self.assertEqual(headers['X-Cookie-2'], 'val2', 'cookie 2')
21
22    def test_java_application_filter(self):
23        self.load('filter')
24
25        headers = self.get()['headers']
26
27        self.assertEqual(headers['X-Filter-Before'], '1', 'filter before')
28        self.assertEqual(headers['X-Filter-After'], '1', 'filter after')
29
30        self.assertEqual(self.get(url='/test')['headers']['X-Filter-After'],
31            '0', 'filter after 2')
32
33    def test_java_application_get_variables(self):
34        self.load('get_params')
35
36        headers = self.get(url='/?var1=val1&var2=&var4=val4&var4=foo')['headers']
37
38        self.assertEqual(headers['X-Var-1'], 'val1', 'GET variables')
39        self.assertEqual(headers['X-Var-2'], 'true', 'GET variables 2')
40        self.assertEqual(headers['X-Var-3'], 'false', 'GET variables 3')
41
42        self.assertEqual(headers['X-Param-Names'], 'var4 var2 var1 ',
43            'getParameterNames')
44        self.assertEqual(headers['X-Param-Values'], 'val4 foo ',
45            'getParameterValues')
46        self.assertEqual(headers['X-Param-Map'],
47            'var2= var1=val1 var4=val4,foo ', 'getParameterMap')
48
49    def test_java_application_post_variables(self):
50        self.load('post_params')
51
52        headers = self.post(headers={
53            'Content-Type': 'application/x-www-form-urlencoded',
54            'Host': 'localhost',
55            'Connection': 'close'
56        }, body='var1=val1&var2=')['headers']
57
58        self.assertEqual(headers['X-Var-1'], 'val1', 'POST variables')
59        self.assertEqual(headers['X-Var-2'], 'true', 'POST variables 2')
60        self.assertEqual(headers['X-Var-3'], 'false', 'POST variables 3')
61
62    def test_java_application_session(self):
63        self.load('session')
64
65        headers = self.get(url='/?var1=val1')['headers']
66        session_id = headers['X-Session-Id']
67
68        self.assertEqual(headers['X-Var-1'], 'null', 'variable empty')
69        self.assertEqual(headers['X-Session-New'], 'true', 'session create')
70
71        headers = self.get(headers={
72            'Host': 'localhost',
73            'Cookie': 'JSESSIONID=' + session_id,
74            'Connection': 'close'
75        }, url='/?var1=val2')['headers']
76
77        self.assertEqual(headers['X-Var-1'], 'val1', 'variable')
78        self.assertEqual(headers['X-Session-New'], 'false', 'session resume')
79        self.assertEqual(session_id, headers['X-Session-Id'], 'session same id')
80
81    def test_java_application_session_active(self):
82        self.load('session_inactive')
83
84        resp = self.get()
85        session_id = resp['headers']['X-Session-Id']
86
87        self.assertEqual(resp['status'], 200, 'session init')
88        self.assertEqual(resp['headers']['X-Session-Interval'], '2',
89            'session interval')
90        self.assertLess(abs(self.date_to_sec_epoch(
91            resp['headers']['X-Session-Last-Access-Time']) - self.sec_epoch()),
92            5, 'session last access time')
93
94        time.sleep(1)
95
96        resp = self.get(headers={
97            'Host': 'localhost',
98            'Cookie': 'JSESSIONID=' + session_id,
99            'Connection': 'close'
100        })
101
102        self.assertEqual(resp['headers']['X-Session-Id'], session_id,
103            'session active')
104
105        session_id = resp['headers']['X-Session-Id']
106
107        time.sleep(1)
108
109        resp = self.get(headers={
110            'Host': 'localhost',
111            'Cookie': 'JSESSIONID=' + session_id,
112            'Connection': 'close'
113        })
114
115        self.assertEqual(resp['headers']['X-Session-Id'], session_id,
116            'session active 2')
117
118        time.sleep(1)
119
120        resp = self.get(headers={
121            'Host': 'localhost',
122            'Cookie': 'JSESSIONID=' + session_id,
123            'Connection': 'close'
124        })
125
126        self.assertEqual(resp['headers']['X-Session-Id'], session_id,
127            'session active 3')
128
129    def test_java_application_session_inactive(self):
130        self.load('session_inactive')
131
132        resp = self.get()
133        session_id = resp['headers']['X-Session-Id']
134
135        time.sleep(3)
136
137        resp = self.get(headers={
138            'Host': 'localhost',
139            'Cookie': 'JSESSIONID=' + session_id,
140            'Connection': 'close'
141        })
142
143        self.assertNotEqual(resp['headers']['X-Session-Id'], session_id,
144            'session inactive')
145
146    def test_java_application_session_invalidate(self):
147        self.load('session_invalidate')
148
149        resp = self.get()
150        session_id = resp['headers']['X-Session-Id']
151
152        resp = self.get(headers={
153            'Host': 'localhost',
154            'Cookie': 'JSESSIONID=' + session_id,
155            'Connection': 'close'
156        })
157
158        self.assertNotEqual(resp['headers']['X-Session-Id'], session_id,
159            'session invalidate')
160
161    def test_java_application_session_listeners(self):
162        self.load('session_listeners')
163
164        headers = self.get(url='/test?var1=val1')['headers']
165        session_id = headers['X-Session-Id']
166
167        self.assertEqual(headers['X-Session-Created'], session_id,
168            'session create')
169        self.assertEqual(headers['X-Attr-Added'], 'var1=val1',
170            'attribute add')
171
172        headers = self.get(headers={
173            'Host': 'localhost',
174            'Cookie': 'JSESSIONID=' + session_id,
175            'Connection': 'close'
176        }, url='/?var1=val2')['headers']
177
178        self.assertEqual(session_id, headers['X-Session-Id'], 'session same id')
179        self.assertEqual(headers['X-Attr-Replaced'], 'var1=val1',
180            'attribute replace')
181
182        headers = self.get(headers={
183            'Host': 'localhost',
184            'Cookie': 'JSESSIONID=' + session_id,
185            'Connection': 'close'
186        }, url='/')['headers']
187
188        self.assertEqual(session_id, headers['X-Session-Id'], 'session same id')
189        self.assertEqual(headers['X-Attr-Removed'], 'var1=val2',
190            'attribute remove')
191
192    def test_java_application_jsp(self):
193        self.load('jsp')
194
195        headers = self.get(url='/index.jsp')['headers']
196
197        self.assertEqual(headers['X-Unit-JSP'], 'ok', 'JSP Ok header')
198
199    def test_java_application_url_pattern(self):
200        self.load('url_pattern')
201
202        headers = self.get(url='/foo/bar/index.html')['headers']
203
204        self.assertEqual(headers['X-Id'], 'servlet1', '#1 Servlet1 request')
205        self.assertEqual(headers['X-Request-URI'], '/foo/bar/index.html', '#1 request URI')
206        self.assertEqual(headers['X-Servlet-Path'], '/foo/bar', '#1 servlet path')
207        self.assertEqual(headers['X-Path-Info'], '/index.html', '#1 path info')
208
209        headers = self.get(url='/foo/bar/index.bop')['headers']
210
211        self.assertEqual(headers['X-Id'], 'servlet1', '#2 Servlet1 request')
212        self.assertEqual(headers['X-Request-URI'], '/foo/bar/index.bop', '#2 request URI')
213        self.assertEqual(headers['X-Servlet-Path'], '/foo/bar', '#2 servlet path')
214        self.assertEqual(headers['X-Path-Info'], '/index.bop', '#2 path info')
215
216        headers = self.get(url='/baz')['headers']
217
218        self.assertEqual(headers['X-Id'], 'servlet2', '#3 Servlet2 request')
219        self.assertEqual(headers['X-Request-URI'], '/baz', '#3 request URI')
220        self.assertEqual(headers['X-Servlet-Path'], '/baz', '#3 servlet path')
221        self.assertEqual(headers['X-Path-Info'], 'null', '#3 path info')
222
223        headers = self.get(url='/baz/index.html')['headers']
224
225        self.assertEqual(headers['X-Id'], 'servlet2', '#4 Servlet2 request')
226        self.assertEqual(headers['X-Request-URI'], '/baz/index.html', '#4 request URI')
227        self.assertEqual(headers['X-Servlet-Path'], '/baz', '#4 servlet path')
228        self.assertEqual(headers['X-Path-Info'], '/index.html', '#4 path info')
229
230        headers = self.get(url='/catalog')['headers']
231
232        self.assertEqual(headers['X-Id'], 'servlet3', '#5 Servlet3 request')
233        self.assertEqual(headers['X-Request-URI'], '/catalog', '#5 request URI')
234        self.assertEqual(headers['X-Servlet-Path'], '/catalog', '#5 servlet path')
235        self.assertEqual(headers['X-Path-Info'], 'null', '#5 path info')
236
237        headers = self.get(url='/catalog/index.html')['headers']
238
239        self.assertEqual(headers['X-Id'], 'default', '#6 default request')
240        self.assertEqual(headers['X-Request-URI'], '/catalog/index.html', '#6 request URI')
241        self.assertEqual(headers['X-Servlet-Path'], '/catalog/index.html', '#6 servlet path')
242        self.assertEqual(headers['X-Path-Info'], 'null', '#6 path info')
243
244        headers = self.get(url='/catalog/racecar.bop')['headers']
245
246        self.assertEqual(headers['X-Id'], 'servlet4', '#7 servlet4 request')
247        self.assertEqual(headers['X-Request-URI'], '/catalog/racecar.bop', '#7 request URI')
248        self.assertEqual(headers['X-Servlet-Path'], '/catalog/racecar.bop', '#7 servlet path')
249        self.assertEqual(headers['X-Path-Info'], 'null', '#7 path info')
250
251        headers = self.get( url='/index.bop')['headers']
252
253        self.assertEqual(headers['X-Id'], 'servlet4', '#8 servlet4 request')
254        self.assertEqual(headers['X-Request-URI'], '/index.bop', '#8 request URI')
255        self.assertEqual(headers['X-Servlet-Path'], '/index.bop', '#8 servlet path')
256        self.assertEqual(headers['X-Path-Info'], 'null', '#8 path info')
257
258        headers = self.get(url='/foo/baz')['headers']
259
260        self.assertEqual(headers['X-Id'], 'servlet0', '#9 servlet0 request')
261        self.assertEqual(headers['X-Request-URI'], '/foo/baz', '#9 request URI')
262        self.assertEqual(headers['X-Servlet-Path'], '/foo', '#9 servlet path')
263        self.assertEqual(headers['X-Path-Info'], '/baz', '#9 path info')
264
265        headers = self.get()['headers']
266
267        self.assertEqual(headers['X-Id'], 'default', '#10 default request')
268        self.assertEqual(headers['X-Request-URI'], '/', '#10 request URI')
269        self.assertEqual(headers['X-Servlet-Path'], '/', '#10 servlet path')
270        self.assertEqual(headers['X-Path-Info'], 'null', '#10 path info')
271
272        headers = self.get(url='/index.bop/')['headers']
273
274        self.assertEqual(headers['X-Id'], 'default', '#11 default request')
275        self.assertEqual(headers['X-Request-URI'], '/index.bop/', '#11 request URI')
276        self.assertEqual(headers['X-Servlet-Path'], '/index.bop/', '#11 servlet path')
277        self.assertEqual(headers['X-Path-Info'], 'null', '#11 path info')
278
279    def test_java_application_header(self):
280        self.load('header')
281
282        headers = self.get()['headers']
283
284        self.assertEqual(headers['X-Set-Utf8-Value'], '????', 'set Utf8 header value')
285        self.assertEqual(headers['X-Set-Utf8-Name-???'], 'x', 'set Utf8 header name')
286        self.assertEqual(headers['X-Add-Utf8-Value'], '????', 'add Utf8 header value')
287        self.assertEqual(headers['X-Add-Utf8-Name-???'], 'y', 'add Utf8 header name')
288        self.assertEqual(headers['X-Add-Test'], 'v1', 'add null header')
289        self.assertEqual('X-Set-Test1' in headers, False, 'set null header')
290        self.assertEqual(headers['X-Set-Test2'], '', 'set empty header')
291
292    def test_java_application_content_type(self):
293        self.load('content_type')
294
295        headers = self.get(url='/1')['headers']
296
297        self.assertEqual(headers['Content-Type'], 'text/plain;charset=utf-8', '#1 Content-Type header')
298        self.assertEqual(headers['X-Content-Type'], 'text/plain;charset=utf-8', '#1 response Content-Type')
299        self.assertEqual(headers['X-Character-Encoding'], 'utf-8', '#1 response charset')
300
301        headers = self.get(url='/2')['headers']
302
303        self.assertEqual(headers['Content-Type'], 'text/plain;charset=iso-8859-1', '#2 Content-Type header')
304        self.assertEqual(headers['X-Content-Type'], 'text/plain;charset=iso-8859-1', '#2 response Content-Type')
305        self.assertEqual(headers['X-Character-Encoding'], 'iso-8859-1', '#2 response charset')
306
307        headers = self.get(url='/3')['headers']
308
309        self.assertEqual(headers['Content-Type'], 'text/plain;charset=windows-1251', '#3 Content-Type header')
310        self.assertEqual(headers['X-Content-Type'], 'text/plain;charset=windows-1251', '#3 response Content-Type')
311        self.assertEqual(headers['X-Character-Encoding'], 'windows-1251', '#3 response charset')
312
313        headers = self.get(url='/4')['headers']
314
315        self.assertEqual(headers['Content-Type'], 'text/plain;charset=windows-1251', '#4 Content-Type header')
316        self.assertEqual(headers['X-Content-Type'], 'text/plain;charset=windows-1251', '#4 response Content-Type')
317        self.assertEqual(headers['X-Character-Encoding'], 'windows-1251', '#4 response charset')
318
319        headers = self.get(url='/5')['headers']
320
321        self.assertEqual(headers['Content-Type'], 'text/plain;charset=iso-8859-1', '#5 Content-Type header')
322        self.assertEqual(headers['X-Content-Type'], 'text/plain;charset=iso-8859-1', '#5 response Content-Type')
323        self.assertEqual(headers['X-Character-Encoding'], 'iso-8859-1', '#5 response charset')
324
325        headers = self.get(url='/6')['headers']
326
327        self.assertEqual('Content-Type' in headers, False, '#6 no Content-Type header')
328        self.assertEqual('X-Content-Type' in headers, False, '#6 no response Content-Type')
329        self.assertEqual(headers['X-Character-Encoding'], 'utf-8', '#6 response charset')
330
331
332        headers = self.get(url='/7')['headers']
333
334        self.assertEqual(headers['Content-Type'], 'text/plain;charset=utf-8', '#7 Content-Type header')
335        self.assertEqual(headers['X-Content-Type'], 'text/plain;charset=utf-8', '#7 response Content-Type')
336        self.assertEqual(headers['X-Character-Encoding'], 'utf-8', '#7 response charset')
337
338        headers = self.get(url='/8')['headers']
339
340        self.assertEqual(headers['Content-Type'], 'text/html;charset=utf-8', '#8 Content-Type header')
341        self.assertEqual(headers['X-Content-Type'], 'text/html;charset=utf-8', '#8 response Content-Type')
342        self.assertEqual(headers['X-Character-Encoding'], 'utf-8', '#8 response charset')
343
344    def test_java_application_welcome_files(self):
345        self.load('welcome_files')
346
347        headers = self.get()['headers']
348
349        resp = self.get(url='/dir1')
350
351        self.assertEqual(resp['status'], 302, 'dir redirect expected')
352
353        resp = self.get(url='/dir1/')
354
355        self.assertEqual('This is index.txt.' in resp['body'], True, 'dir1 index body')
356        self.assertEqual(resp['headers']['X-TXT-Filter'], '1', 'TXT Filter header')
357
358        headers = self.get(url='/dir2/')['headers']
359
360        self.assertEqual(headers['X-Unit-JSP'], 'ok', 'JSP Ok header')
361        self.assertEqual(headers['X-JSP-Filter'], '1', 'JSP Filter header')
362
363        headers = self.get(url='/dir3/')['headers']
364
365        self.assertEqual(headers['X-App-Servlet'], '1', 'URL pattern overrides welcome file')
366
367        headers = self.get(url='/dir4/')['headers']
368
369        self.assertEqual('X-App-Servlet' in headers, False, 'Static welcome file served first')
370
371        headers = self.get(url='/dir5/')['headers']
372
373        self.assertEqual(headers['X-App-Servlet'], '1', 'Servlet for welcome file served when no static file found')
374
375    def test_java_application_request_listeners(self):
376        self.load('request_listeners')
377
378        headers = self.get(url='/test1')['headers']
379
380        self.assertEqual(headers['X-Request-Initialized'], '/test1',
381            'request initialized event')
382        self.assertEqual(headers['X-Request-Destroyed'], '',
383            'request destroyed event')
384        self.assertEqual(headers['X-Attr-Added'], '',
385            'attribute added event')
386        self.assertEqual(headers['X-Attr-Removed'], '',
387            'attribute removed event')
388        self.assertEqual(headers['X-Attr-Replaced'], '',
389            'attribute replaced event')
390
391        headers = self.get(url='/test2?var1=1')['headers']
392
393        self.assertEqual(headers['X-Request-Initialized'], '/test2',
394            'request initialized event')
395        self.assertEqual(headers['X-Request-Destroyed'], '/test1',
396            'request destroyed event')
397        self.assertEqual(headers['X-Attr-Added'], 'var=1;',
398            'attribute added event')
399        self.assertEqual(headers['X-Attr-Removed'], 'var=1;',
400            'attribute removed event')
401        self.assertEqual(headers['X-Attr-Replaced'], '',
402            'attribute replaced event')
403
404        headers = self.get(url='/test3?var1=1&var2=2')['headers']
405
406        self.assertEqual(headers['X-Request-Initialized'], '/test3',
407            'request initialized event')
408        self.assertEqual(headers['X-Request-Destroyed'], '/test2',
409            'request destroyed event')
410        self.assertEqual(headers['X-Attr-Added'], 'var=1;',
411            'attribute added event')
412        self.assertEqual(headers['X-Attr-Removed'], 'var=2;',
413            'attribute removed event')
414        self.assertEqual(headers['X-Attr-Replaced'], 'var=1;',
415            'attribute replaced event')
416
417        headers = self.get(url='/test4?var1=1&var2=2&var3=3')['headers']
418
419        self.assertEqual(headers['X-Request-Initialized'], '/test4',
420            'request initialized event')
421        self.assertEqual(headers['X-Request-Destroyed'], '/test3',
422            'request destroyed event')
423        self.assertEqual(headers['X-Attr-Added'], 'var=1;',
424            'attribute added event')
425        self.assertEqual(headers['X-Attr-Removed'], '',
426            'attribute removed event')
427        self.assertEqual(headers['X-Attr-Replaced'], 'var=1;var=2;',
428            'attribute replaced event')
429
430    def test_java_application_request_uri_forward(self):
431        self.load('forward')
432
433        resp = self.get(url='/fwd?uri=%2Fdata%2Ftest%3Furi%3Dnew_uri%26a%3D2%26b%3D3&a=1&c=4')
434        headers = resp['headers']
435
436        self.assertEqual(headers['X-REQUEST-Id'], 'fwd',
437            'initial request servlet mapping')
438        self.assertEqual(headers['X-Forward-To'], '/data/test?uri=new_uri&a=2&b=3',
439            'forwarding triggered')
440        self.assertEqual(headers['X-REQUEST-Param-uri'], '/data/test?uri=new_uri&a=2&b=3',
441            'original uri parameter')
442        self.assertEqual(headers['X-REQUEST-Param-a'], '1',
443            'original a parameter')
444        self.assertEqual(headers['X-REQUEST-Param-c'], '4',
445            'original c parameter')
446
447        self.assertEqual(headers['X-FORWARD-Id'], 'data',
448            'forward request servlet mapping')
449        self.assertEqual(headers['X-FORWARD-Request-URI'], '/data/test',
450            'forward request uri')
451        self.assertEqual(headers['X-FORWARD-Servlet-Path'], '/data',
452            'forward request servlet path')
453        self.assertEqual(headers['X-FORWARD-Path-Info'], '/test',
454            'forward request path info')
455        self.assertEqual(headers['X-FORWARD-Query-String'], 'uri=new_uri&a=2&b=3',
456            'forward request query string')
457        self.assertEqual(headers['X-FORWARD-Param-uri'], 'new_uri,/data/test?uri=new_uri&a=2&b=3',
458            'forward uri parameter')
459        self.assertEqual(headers['X-FORWARD-Param-a'], '2,1',
460            'forward a parameter')
461        self.assertEqual(headers['X-FORWARD-Param-b'], '3',
462            'forward b parameter')
463        self.assertEqual(headers['X-FORWARD-Param-c'], '4',
464            'forward c parameter')
465
466        self.assertEqual(headers['X-javax.servlet.forward.request_uri'], '/fwd',
467            'original request uri')
468        self.assertEqual(headers['X-javax.servlet.forward.context_path'], '',
469            'original request context path')
470        self.assertEqual(headers['X-javax.servlet.forward.servlet_path'], '/fwd',
471            'original request servlet path')
472        self.assertEqual(headers['X-javax.servlet.forward.path_info'], 'null',
473            'original request path info')
474        self.assertEqual(headers['X-javax.servlet.forward.query_string'], 'uri=%2Fdata%2Ftest%3Furi%3Dnew_uri%26a%3D2%26b%3D3&a=1&c=4',
475            'original request query')
476
477        self.assertEqual('Before forwarding' in resp['body'], False,
478            'discarded data added before forward() call')
479        self.assertEqual('X-After-Forwarding' in headers, False,
480            'cannot add headers after forward() call')
481        self.assertEqual('After forwarding' in resp['body'], False,
482            'cannot add data after forward() call')
483
484    def test_java_application_named_dispatcher_forward(self):
485        self.load('forward')
486
487        resp = self.get(url='/fwd?disp=name&uri=data')
488        headers = resp['headers']
489
490        self.assertEqual(headers['X-REQUEST-Id'], 'fwd',
491            'initial request servlet mapping')
492        self.assertEqual(headers['X-Forward-To'], 'data',
493            'forwarding triggered')
494
495        self.assertEqual(headers['X-FORWARD-Id'], 'data',
496            'forward request servlet mapping')
497        self.assertEqual(headers['X-FORWARD-Request-URI'], '/fwd',
498            'forward request uri')
499        self.assertEqual(headers['X-FORWARD-Servlet-Path'], '/fwd',
500            'forward request servlet path')
501        self.assertEqual(headers['X-FORWARD-Path-Info'], 'null',
502            'forward request path info')
503        self.assertEqual(headers['X-FORWARD-Query-String'], 'disp=name&uri=data',
504            'forward request query string')
505
506        self.assertEqual(headers['X-javax.servlet.forward.request_uri'], 'null',
507            'original request uri')
508        self.assertEqual(headers['X-javax.servlet.forward.context_path'], 'null',
509            'original request context path')
510        self.assertEqual(headers['X-javax.servlet.forward.servlet_path'], 'null',
511            'original request servlet path')
512        self.assertEqual(headers['X-javax.servlet.forward.path_info'], 'null',
513            'original request path info')
514        self.assertEqual(headers['X-javax.servlet.forward.query_string'], 'null',
515            'original request query')
516
517        self.assertEqual('Before forwarding' in resp['body'], False,
518            'discarded data added before forward() call')
519        self.assertEqual('X-After-Forwarding' in headers, False,
520            'cannot add headers after forward() call')
521        self.assertEqual('After forwarding' in resp['body'], False,
522            'cannot add data after forward() call')
523
524    def test_java_application_request_uri_include(self):
525        self.load('include')
526
527        resp = self.get(url='/inc?uri=/data/test')
528        headers = resp['headers']
529        body = resp['body']
530
531        self.assertEqual(headers['X-REQUEST-Id'], 'inc',
532            'initial request servlet mapping')
533        self.assertEqual(headers['X-Include'], '/data/test',
534            'including triggered')
535
536        self.assertEqual('X-INCLUDE-Id' in headers, False,
537            'unable to add headers in include request')
538
539        self.assertEqual('javax.servlet.include.request_uri:  /data/test' in body,
540            True, 'include request uri')
541#        self.assertEqual('javax.servlet.include.context_path: ' in body,
542#            'include request context path')
543        self.assertEqual('javax.servlet.include.servlet_path: /data' in body,
544            True, 'include request servlet path')
545        self.assertEqual('javax.servlet.include.path_info:    /test' in body,
546            True, 'include request path info')
547        self.assertEqual('javax.servlet.include.query_string: null' in body,
548            True, 'include request query')
549
550        self.assertEqual('Before include' in body, True,
551            'preserve data added before include() call')
552        self.assertEqual(headers['X-After-Include'], 'you-should-see-this',
553            'add headers after include() call')
554        self.assertEqual('After include' in body, True,
555            'add data after include() call')
556
557    def test_java_application_named_dispatcher_include(self):
558        self.load('include')
559
560        resp = self.get(url='/inc?disp=name&uri=data')
561        headers = resp['headers']
562        body = resp['body']
563
564        self.assertEqual(headers['X-REQUEST-Id'], 'inc',
565            'initial request servlet mapping')
566        self.assertEqual(headers['X-Include'], 'data',
567            'including triggered')
568
569        self.assertEqual('X-INCLUDE-Id' in headers, False,
570            'unable to add headers in include request')
571
572        self.assertEqual('javax.servlet.include.request_uri:  null' in body,
573            True, 'include request uri')
574#        self.assertEqual('javax.servlet.include.context_path: null' in body,
575#            'include request context path')
576        self.assertEqual('javax.servlet.include.servlet_path: null' in body,
577            True, 'include request servlet path')
578        self.assertEqual('javax.servlet.include.path_info:    null' in body,
579            True, 'include request path info')
580        self.assertEqual('javax.servlet.include.query_string: null' in body,
581            True, 'include request query')
582
583        self.assertEqual('Before include' in body, True,
584            'preserve data added before include() call')
585        self.assertEqual(headers['X-After-Include'], 'you-should-see-this',
586            'add headers after include() call')
587        self.assertEqual('After include' in body, True,
588            'add data after include() call')
589
590    def test_java_application_path_translation(self):
591        self.load('path_translation')
592
593        headers = self.get(url='/pt/test?path=/')['headers']
594
595        self.assertEqual(headers['X-Servlet-Path'], '/pt',
596            'matched servlet path')
597        self.assertEqual(headers['X-Path-Info'], '/test',
598            'the rest of the path')
599        self.assertEqual(headers['X-Path-Translated'],
600            headers['X-Real-Path'] + headers['X-Path-Info'],
601            'translated path is the app root + path info')
602        self.assertEqual(
603            headers['X-Resource-Paths'].endswith('/WEB-INF/, /index.html]'),
604            True, 'app root directory content')
605        self.assertEqual(headers['X-Resource-As-Stream'], 'null',
606            'no resource stream for root path')
607
608        headers = self.get(url='/test?path=/none')['headers']
609
610        self.assertEqual(headers['X-Servlet-Path'], '/test',
611            'matched whole path')
612        self.assertEqual(headers['X-Path-Info'], 'null',
613            'the rest of the path is null, whole path matched')
614        self.assertEqual(headers['X-Path-Translated'], 'null',
615            'translated path is null because path info is null')
616        self.assertEqual(headers['X-Real-Path'].endswith('/none'), True,
617            'read path is not null')
618        self.assertEqual(headers['X-Resource-Paths'], 'null',
619            'no resource found')
620        self.assertEqual(headers['X-Resource-As-Stream'], 'null',
621            'no resource stream')
622
623    def test_java_application_query_string(self):
624        self.load('query_string')
625
626        self.assertEqual(self.get(url='/?a=b')['headers']['X-Query-String'],
627            'a=b', 'query string')
628
629    def test_java_application_query_empty(self):
630        self.load('query_string')
631
632        self.assertEqual(self.get(url='/?')['headers']['X-Query-String'], '',
633            'query string empty')
634
635    def test_java_application_query_absent(self):
636        self.load('query_string')
637
638        self.assertEqual(self.get()['headers']['X-Query-String'], 'null',
639            'query string absent')
640
641    def test_java_application_empty(self):
642        self.load('empty')
643
644        self.assertEqual(self.get()['status'], 200, 'empty')
645
646    def test_java_application_keepalive_body(self):
647        self.load('mirror')
648
649        (resp, sock) = self.post(headers={
650            'Connection': 'keep-alive',
651            'Content-Type': 'text/html',
652            'Host': 'localhost'
653        }, start=True, body='0123456789' * 500)
654
655        self.assertEqual(resp['body'], '0123456789' * 500, 'keep-alive 1')
656
657        resp = self.post(headers={
658            'Connection': 'close',
659            'Content-Type': 'text/html',
660            'Host': 'localhost'
661        }, sock=sock, body='0123456789')
662
663        self.assertEqual(resp['body'], '0123456789', 'keep-alive 2')
664
665    def test_java_application_http_10(self):
666        self.load('empty')
667
668        self.assertEqual(self.get(http_10=True)['status'], 200, 'HTTP 1.0')
669
670    def test_java_application_no_method(self):
671        self.load('empty')
672
673        self.assertEqual(self.post()['status'], 405, 'no method')
674
675    def test_java_application_get_header(self):
676        self.load('get_header')
677
678        self.assertEqual(self.get(headers={
679            'X-Header': 'blah',
680            'Content-Type': 'text/html',
681            'Host': 'localhost'
682        })['headers']['X-Reply'], 'blah', 'get header')
683
684    def test_java_application_get_header_empty(self):
685        self.load('get_header')
686
687        self.assertNotIn('X-Reply', self.get()['headers'], 'get header empty')
688
689    def test_java_application_get_headers(self):
690        self.load('get_headers')
691
692        headers = self.get(headers={
693            'X-Header': ['blah', 'blah'],
694            'Content-Type': 'text/html',
695            'Host': 'localhost'
696        })['headers']
697
698        self.assertEqual(headers['X-Reply-0'], 'blah', 'get headers')
699        self.assertEqual(headers['X-Reply-1'], 'blah', 'get headers 2')
700
701    def test_java_application_get_headers_empty(self):
702        self.load('get_headers')
703
704        self.assertNotIn('X-Reply-0', self.get()['headers'],
705            'get headers empty')
706
707    def test_java_application_get_header_names(self):
708        self.load('get_header_names')
709
710        headers = self.get()['headers']
711
712        self.assertRegex(headers['X-Reply-0'], r'(?:Host|Connection)',
713            'get header names')
714        self.assertRegex(headers['X-Reply-1'], r'(?:Host|Connection)',
715            'get header names 2')
716        self.assertNotEqual(headers['X-Reply-0'], headers['X-Reply-1'],
717            'get header names not equal')
718
719    def test_java_application_get_header_names_empty(self):
720        self.load('get_header_names')
721
722        self.assertNotIn('X-Reply-0', self.get(headers={})['headers'],
723            'get header names empty')
724
725    def test_java_application_header_int(self):
726        self.load('header_int')
727
728        headers = self.get(headers={
729            'X-Header': '2',
730            'Content-Type': 'text/html',
731            'Host': 'localhost'
732        })['headers']
733
734        self.assertEqual(headers['X-Set-Int'], '1', 'set int header')
735        self.assertEqual(headers['X-Get-Int'], '2', 'get int header')
736
737    def test_java_application_header_date(self):
738        self.load('header_date')
739
740        date = 'Fri, 15 Mar 2019 14:45:34 GMT'
741
742        headers = self.get(headers={
743            'X-Header': date,
744            'Content-Type': 'text/html',
745            'Host': 'localhost'
746        })['headers']
747
748        self.assertEqual(headers['X-Set-Date'], 'Thu, 01 Jan 1970 00:00:01 GMT',
749            'set date header')
750        self.assertEqual(headers['X-Get-Date'], date, 'get date header')
751
752if __name__ == '__main__':
753    TestUnitJavaApplication.main()
754