xref: /unit/test/test_java_application.py (revision 2073:bc6ad31ce286)
11355St.nateldemoura@f5.comimport io
21293St.nateldemoura@f5.comimport os
31596Szelenkov@nginx.comimport re
4977Smax.romanov@gmail.comimport time
51477Szelenkov@nginx.com
61019Szelenkov@nginx.comfrom unit.applications.lang.java import TestApplicationJava
71730Szelenkov@nginx.comfrom unit.option import option
81735Szelenkov@nginx.comfrom unit.utils import public_dir
91017Szelenkov@nginx.com
101848Szelenkov@nginx.com
111019Szelenkov@nginx.comclass TestJavaApplication(TestApplicationJava):
121467Szelenkov@nginx.com    prerequisites = {'modules': {'java': 'all'}}
13977Smax.romanov@gmail.com
141736Szelenkov@nginx.com    def test_java_conf_error(self, temp_dir, skip_alert):
151596Szelenkov@nginx.com        skip_alert(
161596Szelenkov@nginx.com            r'realpath.*failed',
171596Szelenkov@nginx.com            r'failed to apply new conf',
181596Szelenkov@nginx.com            r'application setup failed',
191106Szelenkov@nginx.com        )
201596Szelenkov@nginx.com        assert 'error' in self.conf(
211596Szelenkov@nginx.com            {
221596Szelenkov@nginx.com                "listeners": {"*:7080": {"pass": "applications/app"}},
231596Szelenkov@nginx.com                "applications": {
241596Szelenkov@nginx.com                    "app": {
252055Szelenkov@nginx.com                        "type": self.get_application_type(),
261596Szelenkov@nginx.com                        "processes": 1,
271596Szelenkov@nginx.com                        "working_directory": option.test_dir + "/java/empty",
281654Szelenkov@nginx.com                        "webapp": temp_dir + "/java",
291654Szelenkov@nginx.com                        "unit_jars": temp_dir + "/no_such_dir",
301596Szelenkov@nginx.com                    }
311596Szelenkov@nginx.com                },
321596Szelenkov@nginx.com            }
331596Szelenkov@nginx.com        ), 'conf error'
341106Szelenkov@nginx.com
351654Szelenkov@nginx.com    def test_java_war(self, temp_dir):
361106Szelenkov@nginx.com        self.load('empty_war')
371106Szelenkov@nginx.com
381596Szelenkov@nginx.com        assert 'success' in self.conf(
391654Szelenkov@nginx.com            '"' + temp_dir + '/java/empty.war"',
401596Szelenkov@nginx.com            '/config/applications/empty_war/webapp',
411596Szelenkov@nginx.com        ), 'configure war'
421106Szelenkov@nginx.com
431596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'war'
441106Szelenkov@nginx.com
45977Smax.romanov@gmail.com    def test_java_application_cookies(self):
46977Smax.romanov@gmail.com        self.load('cookies')
47977Smax.romanov@gmail.com
481017Szelenkov@nginx.com        headers = self.get(
491017Szelenkov@nginx.com            headers={
501017Szelenkov@nginx.com                'Cookie': 'var1=val1; var2=val2',
511017Szelenkov@nginx.com                'Host': 'localhost',
521017Szelenkov@nginx.com                'Connection': 'close',
531017Szelenkov@nginx.com            }
541017Szelenkov@nginx.com        )['headers']
55977Smax.romanov@gmail.com
561596Szelenkov@nginx.com        assert headers['X-Cookie-1'] == 'val1', 'cookie 1'
571596Szelenkov@nginx.com        assert headers['X-Cookie-2'] == 'val2', 'cookie 2'
58977Smax.romanov@gmail.com
59977Smax.romanov@gmail.com    def test_java_application_filter(self):
60977Smax.romanov@gmail.com        self.load('filter')
61977Smax.romanov@gmail.com
62977Smax.romanov@gmail.com        headers = self.get()['headers']
63977Smax.romanov@gmail.com
641596Szelenkov@nginx.com        assert headers['X-Filter-Before'] == '1', 'filter before'
651596Szelenkov@nginx.com        assert headers['X-Filter-After'] == '1', 'filter after'
66977Smax.romanov@gmail.com
671596Szelenkov@nginx.com        assert (
681596Szelenkov@nginx.com            self.get(url='/test')['headers']['X-Filter-After'] == '0'
691596Szelenkov@nginx.com        ), 'filter after 2'
70977Smax.romanov@gmail.com
71977Smax.romanov@gmail.com    def test_java_application_get_variables(self):
72977Smax.romanov@gmail.com        self.load('get_params')
73977Smax.romanov@gmail.com
741017Szelenkov@nginx.com        headers = self.get(url='/?var1=val1&var2=&var4=val4&var4=foo')[
751017Szelenkov@nginx.com            'headers'
761017Szelenkov@nginx.com        ]
77977Smax.romanov@gmail.com
781596Szelenkov@nginx.com        assert headers['X-Var-1'] == 'val1', 'GET variables'
791596Szelenkov@nginx.com        assert headers['X-Var-2'] == 'true', 'GET variables 2'
801596Szelenkov@nginx.com        assert headers['X-Var-3'] == 'false', 'GET variables 3'
81977Smax.romanov@gmail.com
821596Szelenkov@nginx.com        assert (
831596Szelenkov@nginx.com            headers['X-Param-Names'] == 'var4 var2 var1 '
841596Szelenkov@nginx.com        ), 'getParameterNames'
851596Szelenkov@nginx.com        assert headers['X-Param-Values'] == 'val4 foo ', 'getParameterValues'
861596Szelenkov@nginx.com        assert (
871596Szelenkov@nginx.com            headers['X-Param-Map'] == 'var2= var1=val1 var4=val4,foo '
881596Szelenkov@nginx.com        ), 'getParameterMap'
89977Smax.romanov@gmail.com
90977Smax.romanov@gmail.com    def test_java_application_post_variables(self):
91977Smax.romanov@gmail.com        self.load('post_params')
92977Smax.romanov@gmail.com
931017Szelenkov@nginx.com        headers = self.post(
941017Szelenkov@nginx.com            headers={
951017Szelenkov@nginx.com                'Content-Type': 'application/x-www-form-urlencoded',
961017Szelenkov@nginx.com                'Host': 'localhost',
971017Szelenkov@nginx.com                'Connection': 'close',
981017Szelenkov@nginx.com            },
991017Szelenkov@nginx.com            body='var1=val1&var2=',
1001017Szelenkov@nginx.com        )['headers']
101977Smax.romanov@gmail.com
1021596Szelenkov@nginx.com        assert headers['X-Var-1'] == 'val1', 'POST variables'
1031596Szelenkov@nginx.com        assert headers['X-Var-2'] == 'true', 'POST variables 2'
1041596Szelenkov@nginx.com        assert headers['X-Var-3'] == 'false', 'POST variables 3'
105977Smax.romanov@gmail.com
106977Smax.romanov@gmail.com    def test_java_application_session(self):
107977Smax.romanov@gmail.com        self.load('session')
108977Smax.romanov@gmail.com
109977Smax.romanov@gmail.com        headers = self.get(url='/?var1=val1')['headers']
110977Smax.romanov@gmail.com        session_id = headers['X-Session-Id']
111977Smax.romanov@gmail.com
1121596Szelenkov@nginx.com        assert headers['X-Var-1'] == 'null', 'variable empty'
1131596Szelenkov@nginx.com        assert headers['X-Session-New'] == 'true', 'session create'
114977Smax.romanov@gmail.com
1151017Szelenkov@nginx.com        headers = self.get(
1161017Szelenkov@nginx.com            headers={
1171017Szelenkov@nginx.com                'Host': 'localhost',
1181017Szelenkov@nginx.com                'Cookie': 'JSESSIONID=' + session_id,
1191017Szelenkov@nginx.com                'Connection': 'close',
1201017Szelenkov@nginx.com            },
1211017Szelenkov@nginx.com            url='/?var1=val2',
1221017Szelenkov@nginx.com        )['headers']
123977Smax.romanov@gmail.com
1241596Szelenkov@nginx.com        assert headers['X-Var-1'] == 'val1', 'variable'
1251596Szelenkov@nginx.com        assert headers['X-Session-New'] == 'false', 'session resume'
1261596Szelenkov@nginx.com        assert session_id == headers['X-Session-Id'], 'session same id'
127977Smax.romanov@gmail.com
128977Smax.romanov@gmail.com    def test_java_application_session_active(self):
129977Smax.romanov@gmail.com        self.load('session_inactive')
130977Smax.romanov@gmail.com
1311596Szelenkov@nginx.com        resp = self.get(
1321596Szelenkov@nginx.com            headers={
1331596Szelenkov@nginx.com                'X-Interval': '4',
1341596Szelenkov@nginx.com                'Host': 'localhost',
1351596Szelenkov@nginx.com                'Connection': 'close',
1361596Szelenkov@nginx.com            }
1371596Szelenkov@nginx.com        )
138977Smax.romanov@gmail.com        session_id = resp['headers']['X-Session-Id']
139977Smax.romanov@gmail.com
1401596Szelenkov@nginx.com        assert resp['status'] == 200, 'session init'
1411596Szelenkov@nginx.com        assert resp['headers']['X-Session-Interval'] == '4', 'session interval'
1421596Szelenkov@nginx.com        assert (
1431017Szelenkov@nginx.com            abs(
1441017Szelenkov@nginx.com                self.date_to_sec_epoch(
1451017Szelenkov@nginx.com                    resp['headers']['X-Session-Last-Access-Time']
1461017Szelenkov@nginx.com                )
1471017Szelenkov@nginx.com                - self.sec_epoch()
1481596Szelenkov@nginx.com            )
1491596Szelenkov@nginx.com            < 5
1501596Szelenkov@nginx.com        ), 'session last access time'
151977Smax.romanov@gmail.com
152977Smax.romanov@gmail.com        time.sleep(1)
153977Smax.romanov@gmail.com
1541017Szelenkov@nginx.com        resp = self.get(
1551017Szelenkov@nginx.com            headers={
1561017Szelenkov@nginx.com                'Host': 'localhost',
1571017Szelenkov@nginx.com                'Cookie': 'JSESSIONID=' + session_id,
1581017Szelenkov@nginx.com                'Connection': 'close',
1591017Szelenkov@nginx.com            }
1601017Szelenkov@nginx.com        )
161977Smax.romanov@gmail.com
1621596Szelenkov@nginx.com        assert resp['headers']['X-Session-Id'] == session_id, 'session active'
163977Smax.romanov@gmail.com
164977Smax.romanov@gmail.com        session_id = resp['headers']['X-Session-Id']
165977Smax.romanov@gmail.com
166977Smax.romanov@gmail.com        time.sleep(1)
167977Smax.romanov@gmail.com
1681017Szelenkov@nginx.com        resp = self.get(
1691017Szelenkov@nginx.com            headers={
1701017Szelenkov@nginx.com                'Host': 'localhost',
1711017Szelenkov@nginx.com                'Cookie': 'JSESSIONID=' + session_id,
1721017Szelenkov@nginx.com                'Connection': 'close',
1731017Szelenkov@nginx.com            }
1741017Szelenkov@nginx.com        )
175977Smax.romanov@gmail.com
176*2073Szelenkov@nginx.com        assert resp['headers']['X-Session-Id'] == session_id, 'session active 2'
177977Smax.romanov@gmail.com
1781088Szelenkov@nginx.com        time.sleep(2)
179977Smax.romanov@gmail.com
1801017Szelenkov@nginx.com        resp = self.get(
1811017Szelenkov@nginx.com            headers={
1821017Szelenkov@nginx.com                'Host': 'localhost',
1831017Szelenkov@nginx.com                'Cookie': 'JSESSIONID=' + session_id,
1841017Szelenkov@nginx.com                'Connection': 'close',
1851017Szelenkov@nginx.com            }
1861017Szelenkov@nginx.com        )
187977Smax.romanov@gmail.com
188*2073Szelenkov@nginx.com        assert resp['headers']['X-Session-Id'] == session_id, 'session active 3'
189977Smax.romanov@gmail.com
190977Smax.romanov@gmail.com    def test_java_application_session_inactive(self):
191977Smax.romanov@gmail.com        self.load('session_inactive')
192977Smax.romanov@gmail.com
1931596Szelenkov@nginx.com        resp = self.get(
1941596Szelenkov@nginx.com            headers={
1951596Szelenkov@nginx.com                'X-Interval': '1',
1961596Szelenkov@nginx.com                'Host': 'localhost',
1971596Szelenkov@nginx.com                'Connection': 'close',
1981596Szelenkov@nginx.com            }
1991596Szelenkov@nginx.com        )
200977Smax.romanov@gmail.com        session_id = resp['headers']['X-Session-Id']
201977Smax.romanov@gmail.com
202977Smax.romanov@gmail.com        time.sleep(3)
203977Smax.romanov@gmail.com
2041017Szelenkov@nginx.com        resp = self.get(
2051017Szelenkov@nginx.com            headers={
2061017Szelenkov@nginx.com                'Host': 'localhost',
2071017Szelenkov@nginx.com                'Cookie': 'JSESSIONID=' + session_id,
2081017Szelenkov@nginx.com                'Connection': 'close',
2091017Szelenkov@nginx.com            }
2101017Szelenkov@nginx.com        )
211977Smax.romanov@gmail.com
212*2073Szelenkov@nginx.com        assert resp['headers']['X-Session-Id'] != session_id, 'session inactive'
213977Smax.romanov@gmail.com
214977Smax.romanov@gmail.com    def test_java_application_session_invalidate(self):
215977Smax.romanov@gmail.com        self.load('session_invalidate')
216977Smax.romanov@gmail.com
217977Smax.romanov@gmail.com        resp = self.get()
218977Smax.romanov@gmail.com        session_id = resp['headers']['X-Session-Id']
219977Smax.romanov@gmail.com
2201017Szelenkov@nginx.com        resp = self.get(
2211017Szelenkov@nginx.com            headers={
2221017Szelenkov@nginx.com                'Host': 'localhost',
2231017Szelenkov@nginx.com                'Cookie': 'JSESSIONID=' + session_id,
2241017Szelenkov@nginx.com                'Connection': 'close',
2251017Szelenkov@nginx.com            }
2261017Szelenkov@nginx.com        )
227977Smax.romanov@gmail.com
2281596Szelenkov@nginx.com        assert (
2291596Szelenkov@nginx.com            resp['headers']['X-Session-Id'] != session_id
2301596Szelenkov@nginx.com        ), 'session invalidate'
231977Smax.romanov@gmail.com
232977Smax.romanov@gmail.com    def test_java_application_session_listeners(self):
233977Smax.romanov@gmail.com        self.load('session_listeners')
234977Smax.romanov@gmail.com
235977Smax.romanov@gmail.com        headers = self.get(url='/test?var1=val1')['headers']
236977Smax.romanov@gmail.com        session_id = headers['X-Session-Id']
237977Smax.romanov@gmail.com
2381596Szelenkov@nginx.com        assert headers['X-Session-Created'] == session_id, 'session create'
2391596Szelenkov@nginx.com        assert headers['X-Attr-Added'] == 'var1=val1', 'attribute add'
240977Smax.romanov@gmail.com
2411017Szelenkov@nginx.com        headers = self.get(
2421017Szelenkov@nginx.com            headers={
2431017Szelenkov@nginx.com                'Host': 'localhost',
2441017Szelenkov@nginx.com                'Cookie': 'JSESSIONID=' + session_id,
2451017Szelenkov@nginx.com                'Connection': 'close',
2461017Szelenkov@nginx.com            },
2471017Szelenkov@nginx.com            url='/?var1=val2',
2481017Szelenkov@nginx.com        )['headers']
249977Smax.romanov@gmail.com
2501596Szelenkov@nginx.com        assert session_id == headers['X-Session-Id'], 'session same id'
2511596Szelenkov@nginx.com        assert headers['X-Attr-Replaced'] == 'var1=val1', 'attribute replace'
252977Smax.romanov@gmail.com
2531017Szelenkov@nginx.com        headers = self.get(
2541017Szelenkov@nginx.com            headers={
2551017Szelenkov@nginx.com                'Host': 'localhost',
2561017Szelenkov@nginx.com                'Cookie': 'JSESSIONID=' + session_id,
2571017Szelenkov@nginx.com                'Connection': 'close',
2581017Szelenkov@nginx.com            },
2591017Szelenkov@nginx.com            url='/',
2601017Szelenkov@nginx.com        )['headers']
261977Smax.romanov@gmail.com
2621596Szelenkov@nginx.com        assert session_id == headers['X-Session-Id'], 'session same id'
2631596Szelenkov@nginx.com        assert headers['X-Attr-Removed'] == 'var1=val2', 'attribute remove'
264977Smax.romanov@gmail.com
265977Smax.romanov@gmail.com    def test_java_application_jsp(self):
266977Smax.romanov@gmail.com        self.load('jsp')
267977Smax.romanov@gmail.com
268977Smax.romanov@gmail.com        headers = self.get(url='/index.jsp')['headers']
269977Smax.romanov@gmail.com
2701596Szelenkov@nginx.com        assert headers['X-Unit-JSP'] == 'ok', 'JSP Ok header'
271977Smax.romanov@gmail.com
272977Smax.romanov@gmail.com    def test_java_application_url_pattern(self):
273977Smax.romanov@gmail.com        self.load('url_pattern')
274977Smax.romanov@gmail.com
275977Smax.romanov@gmail.com        headers = self.get(url='/foo/bar/index.html')['headers']
276977Smax.romanov@gmail.com
2771596Szelenkov@nginx.com        assert headers['X-Id'] == 'servlet1', '#1 Servlet1 request'
2781596Szelenkov@nginx.com        assert (
2791596Szelenkov@nginx.com            headers['X-Request-URI'] == '/foo/bar/index.html'
2801596Szelenkov@nginx.com        ), '#1 request URI'
2811596Szelenkov@nginx.com        assert headers['X-Servlet-Path'] == '/foo/bar', '#1 servlet path'
2821596Szelenkov@nginx.com        assert headers['X-Path-Info'] == '/index.html', '#1 path info'
283977Smax.romanov@gmail.com
284977Smax.romanov@gmail.com        headers = self.get(url='/foo/bar/index.bop')['headers']
285977Smax.romanov@gmail.com
2861596Szelenkov@nginx.com        assert headers['X-Id'] == 'servlet1', '#2 Servlet1 request'
2871596Szelenkov@nginx.com        assert (
2881596Szelenkov@nginx.com            headers['X-Request-URI'] == '/foo/bar/index.bop'
2891596Szelenkov@nginx.com        ), '#2 request URI'
2901596Szelenkov@nginx.com        assert headers['X-Servlet-Path'] == '/foo/bar', '#2 servlet path'
2911596Szelenkov@nginx.com        assert headers['X-Path-Info'] == '/index.bop', '#2 path info'
292977Smax.romanov@gmail.com
293977Smax.romanov@gmail.com        headers = self.get(url='/baz')['headers']
294977Smax.romanov@gmail.com
2951596Szelenkov@nginx.com        assert headers['X-Id'] == 'servlet2', '#3 Servlet2 request'
2961596Szelenkov@nginx.com        assert headers['X-Request-URI'] == '/baz', '#3 request URI'
2971596Szelenkov@nginx.com        assert headers['X-Servlet-Path'] == '/baz', '#3 servlet path'
2981596Szelenkov@nginx.com        assert headers['X-Path-Info'] == 'null', '#3 path info'
299977Smax.romanov@gmail.com
300977Smax.romanov@gmail.com        headers = self.get(url='/baz/index.html')['headers']
301977Smax.romanov@gmail.com
3021596Szelenkov@nginx.com        assert headers['X-Id'] == 'servlet2', '#4 Servlet2 request'
3031596Szelenkov@nginx.com        assert headers['X-Request-URI'] == '/baz/index.html', '#4 request URI'
3041596Szelenkov@nginx.com        assert headers['X-Servlet-Path'] == '/baz', '#4 servlet path'
3051596Szelenkov@nginx.com        assert headers['X-Path-Info'] == '/index.html', '#4 path info'
306977Smax.romanov@gmail.com
307977Smax.romanov@gmail.com        headers = self.get(url='/catalog')['headers']
308977Smax.romanov@gmail.com
3091596Szelenkov@nginx.com        assert headers['X-Id'] == 'servlet3', '#5 Servlet3 request'
3101596Szelenkov@nginx.com        assert headers['X-Request-URI'] == '/catalog', '#5 request URI'
3111596Szelenkov@nginx.com        assert headers['X-Servlet-Path'] == '/catalog', '#5 servlet path'
3121596Szelenkov@nginx.com        assert headers['X-Path-Info'] == 'null', '#5 path info'
313977Smax.romanov@gmail.com
314977Smax.romanov@gmail.com        headers = self.get(url='/catalog/index.html')['headers']
315977Smax.romanov@gmail.com
3161596Szelenkov@nginx.com        assert headers['X-Id'] == 'default', '#6 default request'
3171596Szelenkov@nginx.com        assert (
3181596Szelenkov@nginx.com            headers['X-Request-URI'] == '/catalog/index.html'
3191596Szelenkov@nginx.com        ), '#6 request URI'
3201596Szelenkov@nginx.com        assert (
3211596Szelenkov@nginx.com            headers['X-Servlet-Path'] == '/catalog/index.html'
3221596Szelenkov@nginx.com        ), '#6 servlet path'
3231596Szelenkov@nginx.com        assert headers['X-Path-Info'] == 'null', '#6 path info'
324977Smax.romanov@gmail.com
325977Smax.romanov@gmail.com        headers = self.get(url='/catalog/racecar.bop')['headers']
326977Smax.romanov@gmail.com
3271596Szelenkov@nginx.com        assert headers['X-Id'] == 'servlet4', '#7 servlet4 request'
3281596Szelenkov@nginx.com        assert (
3291596Szelenkov@nginx.com            headers['X-Request-URI'] == '/catalog/racecar.bop'
3301596Szelenkov@nginx.com        ), '#7 request URI'
3311596Szelenkov@nginx.com        assert (
3321596Szelenkov@nginx.com            headers['X-Servlet-Path'] == '/catalog/racecar.bop'
3331596Szelenkov@nginx.com        ), '#7 servlet path'
3341596Szelenkov@nginx.com        assert headers['X-Path-Info'] == 'null', '#7 path info'
335977Smax.romanov@gmail.com
3361017Szelenkov@nginx.com        headers = self.get(url='/index.bop')['headers']
337977Smax.romanov@gmail.com
3381596Szelenkov@nginx.com        assert headers['X-Id'] == 'servlet4', '#8 servlet4 request'
3391596Szelenkov@nginx.com        assert headers['X-Request-URI'] == '/index.bop', '#8 request URI'
3401596Szelenkov@nginx.com        assert headers['X-Servlet-Path'] == '/index.bop', '#8 servlet path'
3411596Szelenkov@nginx.com        assert headers['X-Path-Info'] == 'null', '#8 path info'
342977Smax.romanov@gmail.com
343977Smax.romanov@gmail.com        headers = self.get(url='/foo/baz')['headers']
344977Smax.romanov@gmail.com
3451596Szelenkov@nginx.com        assert headers['X-Id'] == 'servlet0', '#9 servlet0 request'
3461596Szelenkov@nginx.com        assert headers['X-Request-URI'] == '/foo/baz', '#9 request URI'
3471596Szelenkov@nginx.com        assert headers['X-Servlet-Path'] == '/foo', '#9 servlet path'
3481596Szelenkov@nginx.com        assert headers['X-Path-Info'] == '/baz', '#9 path info'
349977Smax.romanov@gmail.com
350977Smax.romanov@gmail.com        headers = self.get()['headers']
351977Smax.romanov@gmail.com
3521596Szelenkov@nginx.com        assert headers['X-Id'] == 'default', '#10 default request'
3531596Szelenkov@nginx.com        assert headers['X-Request-URI'] == '/', '#10 request URI'
3541596Szelenkov@nginx.com        assert headers['X-Servlet-Path'] == '/', '#10 servlet path'
3551596Szelenkov@nginx.com        assert headers['X-Path-Info'] == 'null', '#10 path info'
356977Smax.romanov@gmail.com
357977Smax.romanov@gmail.com        headers = self.get(url='/index.bop/')['headers']
358977Smax.romanov@gmail.com
3591596Szelenkov@nginx.com        assert headers['X-Id'] == 'default', '#11 default request'
3601596Szelenkov@nginx.com        assert headers['X-Request-URI'] == '/index.bop/', '#11 request URI'
3611596Szelenkov@nginx.com        assert headers['X-Servlet-Path'] == '/index.bop/', '#11 servlet path'
3621596Szelenkov@nginx.com        assert headers['X-Path-Info'] == 'null', '#11 path info'
363977Smax.romanov@gmail.com
364977Smax.romanov@gmail.com    def test_java_application_header(self):
365977Smax.romanov@gmail.com        self.load('header')
366977Smax.romanov@gmail.com
367977Smax.romanov@gmail.com        headers = self.get()['headers']
368977Smax.romanov@gmail.com
3691596Szelenkov@nginx.com        assert headers['X-Set-Utf8-Value'] == '????', 'set Utf8 header value'
3701596Szelenkov@nginx.com        assert headers['X-Set-Utf8-Name-???'] == 'x', 'set Utf8 header name'
3711596Szelenkov@nginx.com        assert headers['X-Add-Utf8-Value'] == '????', 'add Utf8 header value'
3721596Szelenkov@nginx.com        assert headers['X-Add-Utf8-Name-???'] == 'y', 'add Utf8 header name'
3731596Szelenkov@nginx.com        assert headers['X-Add-Test'] == 'v1', 'add null header'
3741596Szelenkov@nginx.com        assert ('X-Set-Test1' in headers) == False, 'set null header'
3751596Szelenkov@nginx.com        assert headers['X-Set-Test2'] == '', 'set empty header'
376977Smax.romanov@gmail.com
377977Smax.romanov@gmail.com    def test_java_application_content_type(self):
378977Smax.romanov@gmail.com        self.load('content_type')
379977Smax.romanov@gmail.com
380977Smax.romanov@gmail.com        headers = self.get(url='/1')['headers']
381977Smax.romanov@gmail.com
3821596Szelenkov@nginx.com        assert (
3831596Szelenkov@nginx.com            headers['Content-Type'] == 'text/plain;charset=utf-8'
3841596Szelenkov@nginx.com        ), '#1 Content-Type header'
3851596Szelenkov@nginx.com        assert (
3861596Szelenkov@nginx.com            headers['X-Content-Type'] == 'text/plain;charset=utf-8'
3871596Szelenkov@nginx.com        ), '#1 response Content-Type'
388*2073Szelenkov@nginx.com        assert headers['X-Character-Encoding'] == 'utf-8', '#1 response charset'
389977Smax.romanov@gmail.com
390977Smax.romanov@gmail.com        headers = self.get(url='/2')['headers']
391977Smax.romanov@gmail.com
3921596Szelenkov@nginx.com        assert (
3931596Szelenkov@nginx.com            headers['Content-Type'] == 'text/plain;charset=iso-8859-1'
3941596Szelenkov@nginx.com        ), '#2 Content-Type header'
3951596Szelenkov@nginx.com        assert (
3961596Szelenkov@nginx.com            headers['X-Content-Type'] == 'text/plain;charset=iso-8859-1'
3971596Szelenkov@nginx.com        ), '#2 response Content-Type'
3981596Szelenkov@nginx.com        assert (
3991596Szelenkov@nginx.com            headers['X-Character-Encoding'] == 'iso-8859-1'
4001596Szelenkov@nginx.com        ), '#2 response charset'
401977Smax.romanov@gmail.com
402977Smax.romanov@gmail.com        headers = self.get(url='/3')['headers']
403977Smax.romanov@gmail.com
4041596Szelenkov@nginx.com        assert (
4051596Szelenkov@nginx.com            headers['Content-Type'] == 'text/plain;charset=windows-1251'
4061596Szelenkov@nginx.com        ), '#3 Content-Type header'
4071596Szelenkov@nginx.com        assert (
4081596Szelenkov@nginx.com            headers['X-Content-Type'] == 'text/plain;charset=windows-1251'
4091596Szelenkov@nginx.com        ), '#3 response Content-Type'
4101596Szelenkov@nginx.com        assert (
4111596Szelenkov@nginx.com            headers['X-Character-Encoding'] == 'windows-1251'
4121596Szelenkov@nginx.com        ), '#3 response charset'
413977Smax.romanov@gmail.com
414977Smax.romanov@gmail.com        headers = self.get(url='/4')['headers']
415977Smax.romanov@gmail.com
4161596Szelenkov@nginx.com        assert (
4171596Szelenkov@nginx.com            headers['Content-Type'] == 'text/plain;charset=windows-1251'
4181596Szelenkov@nginx.com        ), '#4 Content-Type header'
4191596Szelenkov@nginx.com        assert (
4201596Szelenkov@nginx.com            headers['X-Content-Type'] == 'text/plain;charset=windows-1251'
4211596Szelenkov@nginx.com        ), '#4 response Content-Type'
4221596Szelenkov@nginx.com        assert (
4231596Szelenkov@nginx.com            headers['X-Character-Encoding'] == 'windows-1251'
4241596Szelenkov@nginx.com        ), '#4 response charset'
425977Smax.romanov@gmail.com
426977Smax.romanov@gmail.com        headers = self.get(url='/5')['headers']
427977Smax.romanov@gmail.com
4281596Szelenkov@nginx.com        assert (
4291596Szelenkov@nginx.com            headers['Content-Type'] == 'text/plain;charset=iso-8859-1'
4301596Szelenkov@nginx.com        ), '#5 Content-Type header'
4311596Szelenkov@nginx.com        assert (
4321596Szelenkov@nginx.com            headers['X-Content-Type'] == 'text/plain;charset=iso-8859-1'
4331596Szelenkov@nginx.com        ), '#5 response Content-Type'
4341596Szelenkov@nginx.com        assert (
4351596Szelenkov@nginx.com            headers['X-Character-Encoding'] == 'iso-8859-1'
4361596Szelenkov@nginx.com        ), '#5 response charset'
437977Smax.romanov@gmail.com
438977Smax.romanov@gmail.com        headers = self.get(url='/6')['headers']
439977Smax.romanov@gmail.com
440*2073Szelenkov@nginx.com        assert ('Content-Type' in headers) == False, '#6 no Content-Type header'
4411596Szelenkov@nginx.com        assert (
4421596Szelenkov@nginx.com            'X-Content-Type' in headers
4431596Szelenkov@nginx.com        ) == False, '#6 no response Content-Type'
444*2073Szelenkov@nginx.com        assert headers['X-Character-Encoding'] == 'utf-8', '#6 response charset'
445977Smax.romanov@gmail.com
446977Smax.romanov@gmail.com        headers = self.get(url='/7')['headers']
447977Smax.romanov@gmail.com
4481596Szelenkov@nginx.com        assert (
4491596Szelenkov@nginx.com            headers['Content-Type'] == 'text/plain;charset=utf-8'
4501596Szelenkov@nginx.com        ), '#7 Content-Type header'
4511596Szelenkov@nginx.com        assert (
4521596Szelenkov@nginx.com            headers['X-Content-Type'] == 'text/plain;charset=utf-8'
4531596Szelenkov@nginx.com        ), '#7 response Content-Type'
454*2073Szelenkov@nginx.com        assert headers['X-Character-Encoding'] == 'utf-8', '#7 response charset'
455977Smax.romanov@gmail.com
456977Smax.romanov@gmail.com        headers = self.get(url='/8')['headers']
457977Smax.romanov@gmail.com
4581596Szelenkov@nginx.com        assert (
4591596Szelenkov@nginx.com            headers['Content-Type'] == 'text/html;charset=utf-8'
4601596Szelenkov@nginx.com        ), '#8 Content-Type header'
4611596Szelenkov@nginx.com        assert (
4621596Szelenkov@nginx.com            headers['X-Content-Type'] == 'text/html;charset=utf-8'
4631596Szelenkov@nginx.com        ), '#8 response Content-Type'
464*2073Szelenkov@nginx.com        assert headers['X-Character-Encoding'] == 'utf-8', '#8 response charset'
465977Smax.romanov@gmail.com
466977Smax.romanov@gmail.com    def test_java_application_welcome_files(self):
467977Smax.romanov@gmail.com        self.load('welcome_files')
468977Smax.romanov@gmail.com
469977Smax.romanov@gmail.com        headers = self.get()['headers']
470977Smax.romanov@gmail.com
471977Smax.romanov@gmail.com        resp = self.get(url='/dir1')
472977Smax.romanov@gmail.com
4731596Szelenkov@nginx.com        assert resp['status'] == 302, 'dir redirect expected'
474977Smax.romanov@gmail.com
475977Smax.romanov@gmail.com        resp = self.get(url='/dir1/')
476977Smax.romanov@gmail.com
477*2073Szelenkov@nginx.com        assert ('This is index.txt.' in resp['body']) == True, 'dir1 index body'
4781596Szelenkov@nginx.com        assert resp['headers']['X-TXT-Filter'] == '1', 'TXT Filter header'
479977Smax.romanov@gmail.com
480977Smax.romanov@gmail.com        headers = self.get(url='/dir2/')['headers']
481977Smax.romanov@gmail.com
4821596Szelenkov@nginx.com        assert headers['X-Unit-JSP'] == 'ok', 'JSP Ok header'
4831596Szelenkov@nginx.com        assert headers['X-JSP-Filter'] == '1', 'JSP Filter header'
484977Smax.romanov@gmail.com
485977Smax.romanov@gmail.com        headers = self.get(url='/dir3/')['headers']
486977Smax.romanov@gmail.com
4871596Szelenkov@nginx.com        assert (
4881596Szelenkov@nginx.com            headers['X-App-Servlet'] == '1'
4891596Szelenkov@nginx.com        ), 'URL pattern overrides welcome file'
490977Smax.romanov@gmail.com
491977Smax.romanov@gmail.com        headers = self.get(url='/dir4/')['headers']
492977Smax.romanov@gmail.com
4931596Szelenkov@nginx.com        assert (
4941596Szelenkov@nginx.com            'X-App-Servlet' in headers
4951596Szelenkov@nginx.com        ) == False, 'Static welcome file served first'
496977Smax.romanov@gmail.com
497977Smax.romanov@gmail.com        headers = self.get(url='/dir5/')['headers']
498977Smax.romanov@gmail.com
4991596Szelenkov@nginx.com        assert (
5001596Szelenkov@nginx.com            headers['X-App-Servlet'] == '1'
5011596Szelenkov@nginx.com        ), 'Servlet for welcome file served when no static file found'
502977Smax.romanov@gmail.com
503977Smax.romanov@gmail.com    def test_java_application_request_listeners(self):
504977Smax.romanov@gmail.com        self.load('request_listeners')
505977Smax.romanov@gmail.com
506977Smax.romanov@gmail.com        headers = self.get(url='/test1')['headers']
507977Smax.romanov@gmail.com
5081596Szelenkov@nginx.com        assert (
5091596Szelenkov@nginx.com            headers['X-Request-Initialized'] == '/test1'
5101596Szelenkov@nginx.com        ), 'request initialized event'
5111596Szelenkov@nginx.com        assert headers['X-Request-Destroyed'] == '', 'request destroyed event'
5121596Szelenkov@nginx.com        assert headers['X-Attr-Added'] == '', 'attribute added event'
5131596Szelenkov@nginx.com        assert headers['X-Attr-Removed'] == '', 'attribute removed event'
5141596Szelenkov@nginx.com        assert headers['X-Attr-Replaced'] == '', 'attribute replaced event'
515977Smax.romanov@gmail.com
516977Smax.romanov@gmail.com        headers = self.get(url='/test2?var1=1')['headers']
517977Smax.romanov@gmail.com
5181596Szelenkov@nginx.com        assert (
5191596Szelenkov@nginx.com            headers['X-Request-Initialized'] == '/test2'
5201596Szelenkov@nginx.com        ), 'request initialized event'
5211596Szelenkov@nginx.com        assert (
5221596Szelenkov@nginx.com            headers['X-Request-Destroyed'] == '/test1'
5231596Szelenkov@nginx.com        ), 'request destroyed event'
5241596Szelenkov@nginx.com        assert headers['X-Attr-Added'] == 'var=1;', 'attribute added event'
5251596Szelenkov@nginx.com        assert headers['X-Attr-Removed'] == 'var=1;', 'attribute removed event'
5261596Szelenkov@nginx.com        assert headers['X-Attr-Replaced'] == '', 'attribute replaced event'
527977Smax.romanov@gmail.com
528977Smax.romanov@gmail.com        headers = self.get(url='/test3?var1=1&var2=2')['headers']
529977Smax.romanov@gmail.com
5301596Szelenkov@nginx.com        assert (
5311596Szelenkov@nginx.com            headers['X-Request-Initialized'] == '/test3'
5321596Szelenkov@nginx.com        ), 'request initialized event'
5331596Szelenkov@nginx.com        assert (
5341596Szelenkov@nginx.com            headers['X-Request-Destroyed'] == '/test2'
5351596Szelenkov@nginx.com        ), 'request destroyed event'
5361596Szelenkov@nginx.com        assert headers['X-Attr-Added'] == 'var=1;', 'attribute added event'
5371596Szelenkov@nginx.com        assert headers['X-Attr-Removed'] == 'var=2;', 'attribute removed event'
5381596Szelenkov@nginx.com        assert (
5391596Szelenkov@nginx.com            headers['X-Attr-Replaced'] == 'var=1;'
5401596Szelenkov@nginx.com        ), 'attribute replaced event'
541977Smax.romanov@gmail.com
542977Smax.romanov@gmail.com        headers = self.get(url='/test4?var1=1&var2=2&var3=3')['headers']
543977Smax.romanov@gmail.com
5441596Szelenkov@nginx.com        assert (
5451596Szelenkov@nginx.com            headers['X-Request-Initialized'] == '/test4'
5461596Szelenkov@nginx.com        ), 'request initialized event'
5471596Szelenkov@nginx.com        assert (
5481596Szelenkov@nginx.com            headers['X-Request-Destroyed'] == '/test3'
5491596Szelenkov@nginx.com        ), 'request destroyed event'
5501596Szelenkov@nginx.com        assert headers['X-Attr-Added'] == 'var=1;', 'attribute added event'
5511596Szelenkov@nginx.com        assert headers['X-Attr-Removed'] == '', 'attribute removed event'
5521596Szelenkov@nginx.com        assert (
5531596Szelenkov@nginx.com            headers['X-Attr-Replaced'] == 'var=1;var=2;'
5541596Szelenkov@nginx.com        ), 'attribute replaced event'
555977Smax.romanov@gmail.com
556977Smax.romanov@gmail.com    def test_java_application_request_uri_forward(self):
557977Smax.romanov@gmail.com        self.load('forward')
558977Smax.romanov@gmail.com
5591017Szelenkov@nginx.com        resp = self.get(
5601017Szelenkov@nginx.com            url='/fwd?uri=%2Fdata%2Ftest%3Furi%3Dnew_uri%26a%3D2%26b%3D3&a=1&c=4'
5611017Szelenkov@nginx.com        )
562977Smax.romanov@gmail.com        headers = resp['headers']
563977Smax.romanov@gmail.com
5641596Szelenkov@nginx.com        assert (
5651596Szelenkov@nginx.com            headers['X-REQUEST-Id'] == 'fwd'
5661596Szelenkov@nginx.com        ), 'initial request servlet mapping'
5671596Szelenkov@nginx.com        assert (
5681596Szelenkov@nginx.com            headers['X-Forward-To'] == '/data/test?uri=new_uri&a=2&b=3'
5691596Szelenkov@nginx.com        ), 'forwarding triggered'
5701596Szelenkov@nginx.com        assert (
5711596Szelenkov@nginx.com            headers['X-REQUEST-Param-uri'] == '/data/test?uri=new_uri&a=2&b=3'
5721596Szelenkov@nginx.com        ), 'original uri parameter'
5731596Szelenkov@nginx.com        assert headers['X-REQUEST-Param-a'] == '1', 'original a parameter'
5741596Szelenkov@nginx.com        assert headers['X-REQUEST-Param-c'] == '4', 'original c parameter'
575977Smax.romanov@gmail.com
5761596Szelenkov@nginx.com        assert (
5771596Szelenkov@nginx.com            headers['X-FORWARD-Id'] == 'data'
5781596Szelenkov@nginx.com        ), 'forward request servlet mapping'
5791596Szelenkov@nginx.com        assert (
5801596Szelenkov@nginx.com            headers['X-FORWARD-Request-URI'] == '/data/test'
5811596Szelenkov@nginx.com        ), 'forward request uri'
5821596Szelenkov@nginx.com        assert (
5831596Szelenkov@nginx.com            headers['X-FORWARD-Servlet-Path'] == '/data'
5841596Szelenkov@nginx.com        ), 'forward request servlet path'
5851596Szelenkov@nginx.com        assert (
5861596Szelenkov@nginx.com            headers['X-FORWARD-Path-Info'] == '/test'
5871596Szelenkov@nginx.com        ), 'forward request path info'
5881596Szelenkov@nginx.com        assert (
5891596Szelenkov@nginx.com            headers['X-FORWARD-Query-String'] == 'uri=new_uri&a=2&b=3'
5901596Szelenkov@nginx.com        ), 'forward request query string'
5911596Szelenkov@nginx.com        assert (
5921596Szelenkov@nginx.com            headers['X-FORWARD-Param-uri']
5931596Szelenkov@nginx.com            == 'new_uri,/data/test?uri=new_uri&a=2&b=3'
5941596Szelenkov@nginx.com        ), 'forward uri parameter'
5951596Szelenkov@nginx.com        assert headers['X-FORWARD-Param-a'] == '2,1', 'forward a parameter'
5961596Szelenkov@nginx.com        assert headers['X-FORWARD-Param-b'] == '3', 'forward b parameter'
5971596Szelenkov@nginx.com        assert headers['X-FORWARD-Param-c'] == '4', 'forward c parameter'
598977Smax.romanov@gmail.com
5991596Szelenkov@nginx.com        assert (
6001596Szelenkov@nginx.com            headers['X-javax.servlet.forward.request_uri'] == '/fwd'
6011596Szelenkov@nginx.com        ), 'original request uri'
6021596Szelenkov@nginx.com        assert (
6031596Szelenkov@nginx.com            headers['X-javax.servlet.forward.context_path'] == ''
6041596Szelenkov@nginx.com        ), 'original request context path'
6051596Szelenkov@nginx.com        assert (
6061596Szelenkov@nginx.com            headers['X-javax.servlet.forward.servlet_path'] == '/fwd'
6071596Szelenkov@nginx.com        ), 'original request servlet path'
6081596Szelenkov@nginx.com        assert (
6091596Szelenkov@nginx.com            headers['X-javax.servlet.forward.path_info'] == 'null'
6101596Szelenkov@nginx.com        ), 'original request path info'
6111596Szelenkov@nginx.com        assert (
6121596Szelenkov@nginx.com            headers['X-javax.servlet.forward.query_string']
6131596Szelenkov@nginx.com            == 'uri=%2Fdata%2Ftest%3Furi%3Dnew_uri%26a%3D2%26b%3D3&a=1&c=4'
6141596Szelenkov@nginx.com        ), 'original request query'
615977Smax.romanov@gmail.com
6161596Szelenkov@nginx.com        assert (
6171596Szelenkov@nginx.com            'Before forwarding' in resp['body']
6181596Szelenkov@nginx.com        ) == False, 'discarded data added before forward() call'
6191596Szelenkov@nginx.com        assert (
6201596Szelenkov@nginx.com            'X-After-Forwarding' in headers
6211596Szelenkov@nginx.com        ) == False, 'cannot add headers after forward() call'
6221596Szelenkov@nginx.com        assert (
6231596Szelenkov@nginx.com            'After forwarding' in resp['body']
6241596Szelenkov@nginx.com        ) == False, 'cannot add data after forward() call'
625977Smax.romanov@gmail.com
626977Smax.romanov@gmail.com    def test_java_application_named_dispatcher_forward(self):
627977Smax.romanov@gmail.com        self.load('forward')
628977Smax.romanov@gmail.com
629977Smax.romanov@gmail.com        resp = self.get(url='/fwd?disp=name&uri=data')
630977Smax.romanov@gmail.com        headers = resp['headers']
631977Smax.romanov@gmail.com
6321596Szelenkov@nginx.com        assert (
6331596Szelenkov@nginx.com            headers['X-REQUEST-Id'] == 'fwd'
6341596Szelenkov@nginx.com        ), 'initial request servlet mapping'
6351596Szelenkov@nginx.com        assert headers['X-Forward-To'] == 'data', 'forwarding triggered'
636977Smax.romanov@gmail.com
6371596Szelenkov@nginx.com        assert (
6381596Szelenkov@nginx.com            headers['X-FORWARD-Id'] == 'data'
6391596Szelenkov@nginx.com        ), 'forward request servlet mapping'
640*2073Szelenkov@nginx.com        assert headers['X-FORWARD-Request-URI'] == '/fwd', 'forward request uri'
6411596Szelenkov@nginx.com        assert (
6421596Szelenkov@nginx.com            headers['X-FORWARD-Servlet-Path'] == '/fwd'
6431596Szelenkov@nginx.com        ), 'forward request servlet path'
6441596Szelenkov@nginx.com        assert (
6451596Szelenkov@nginx.com            headers['X-FORWARD-Path-Info'] == 'null'
6461596Szelenkov@nginx.com        ), 'forward request path info'
6471596Szelenkov@nginx.com        assert (
6481596Szelenkov@nginx.com            headers['X-FORWARD-Query-String'] == 'disp=name&uri=data'
6491596Szelenkov@nginx.com        ), 'forward request query string'
650977Smax.romanov@gmail.com
6511596Szelenkov@nginx.com        assert (
6521596Szelenkov@nginx.com            headers['X-javax.servlet.forward.request_uri'] == 'null'
6531596Szelenkov@nginx.com        ), 'original request uri'
6541596Szelenkov@nginx.com        assert (
6551596Szelenkov@nginx.com            headers['X-javax.servlet.forward.context_path'] == 'null'
6561596Szelenkov@nginx.com        ), 'original request context path'
6571596Szelenkov@nginx.com        assert (
6581596Szelenkov@nginx.com            headers['X-javax.servlet.forward.servlet_path'] == 'null'
6591596Szelenkov@nginx.com        ), 'original request servlet path'
6601596Szelenkov@nginx.com        assert (
6611596Szelenkov@nginx.com            headers['X-javax.servlet.forward.path_info'] == 'null'
6621596Szelenkov@nginx.com        ), 'original request path info'
6631596Szelenkov@nginx.com        assert (
6641596Szelenkov@nginx.com            headers['X-javax.servlet.forward.query_string'] == 'null'
6651596Szelenkov@nginx.com        ), 'original request query'
666977Smax.romanov@gmail.com
6671596Szelenkov@nginx.com        assert (
6681596Szelenkov@nginx.com            'Before forwarding' in resp['body']
6691596Szelenkov@nginx.com        ) == False, 'discarded data added before forward() call'
6701596Szelenkov@nginx.com        assert (
6711596Szelenkov@nginx.com            'X-After-Forwarding' in headers
6721596Szelenkov@nginx.com        ) == False, 'cannot add headers after forward() call'
6731596Szelenkov@nginx.com        assert (
6741596Szelenkov@nginx.com            'After forwarding' in resp['body']
6751596Szelenkov@nginx.com        ) == False, 'cannot add data after forward() call'
676977Smax.romanov@gmail.com
677977Smax.romanov@gmail.com    def test_java_application_request_uri_include(self):
678977Smax.romanov@gmail.com        self.load('include')
679977Smax.romanov@gmail.com
680977Smax.romanov@gmail.com        resp = self.get(url='/inc?uri=/data/test')
681977Smax.romanov@gmail.com        headers = resp['headers']
682977Smax.romanov@gmail.com        body = resp['body']
683977Smax.romanov@gmail.com
6841596Szelenkov@nginx.com        assert (
6851596Szelenkov@nginx.com            headers['X-REQUEST-Id'] == 'inc'
6861596Szelenkov@nginx.com        ), 'initial request servlet mapping'
6871596Szelenkov@nginx.com        assert headers['X-Include'] == '/data/test', 'including triggered'
688977Smax.romanov@gmail.com
6891596Szelenkov@nginx.com        assert (
6901596Szelenkov@nginx.com            'X-INCLUDE-Id' in headers
6911596Szelenkov@nginx.com        ) == False, 'unable to add headers in include request'
692977Smax.romanov@gmail.com
6931596Szelenkov@nginx.com        assert (
6941596Szelenkov@nginx.com            'javax.servlet.include.request_uri:  /data/test' in body
6951596Szelenkov@nginx.com        ) == True, 'include request uri'
6961848Szelenkov@nginx.com        # assert (
6971596Szelenkov@nginx.com        #    'javax.servlet.include.context_path: ' in body
6981848Szelenkov@nginx.com        # ) == True, 'include request context path'
6991596Szelenkov@nginx.com        assert (
7001596Szelenkov@nginx.com            'javax.servlet.include.servlet_path: /data' in body
7011596Szelenkov@nginx.com        ) == True, 'include request servlet path'
7021596Szelenkov@nginx.com        assert (
7031596Szelenkov@nginx.com            'javax.servlet.include.path_info:    /test' in body
7041596Szelenkov@nginx.com        ) == True, 'include request path info'
7051596Szelenkov@nginx.com        assert (
7061596Szelenkov@nginx.com            'javax.servlet.include.query_string: null' in body
7071596Szelenkov@nginx.com        ) == True, 'include request query'
708977Smax.romanov@gmail.com
7091596Szelenkov@nginx.com        assert (
7101596Szelenkov@nginx.com            'Before include' in body
7111596Szelenkov@nginx.com        ) == True, 'preserve data added before include() call'
7121596Szelenkov@nginx.com        assert (
7131596Szelenkov@nginx.com            headers['X-After-Include'] == 'you-should-see-this'
7141596Szelenkov@nginx.com        ), 'add headers after include() call'
7151596Szelenkov@nginx.com        assert (
7161596Szelenkov@nginx.com            'After include' in body
7171596Szelenkov@nginx.com        ) == True, 'add data after include() call'
718977Smax.romanov@gmail.com
719977Smax.romanov@gmail.com    def test_java_application_named_dispatcher_include(self):
720977Smax.romanov@gmail.com        self.load('include')
721977Smax.romanov@gmail.com
722977Smax.romanov@gmail.com        resp = self.get(url='/inc?disp=name&uri=data')
723977Smax.romanov@gmail.com        headers = resp['headers']
724977Smax.romanov@gmail.com        body = resp['body']
725977Smax.romanov@gmail.com
7261596Szelenkov@nginx.com        assert (
7271596Szelenkov@nginx.com            headers['X-REQUEST-Id'] == 'inc'
7281596Szelenkov@nginx.com        ), 'initial request servlet mapping'
7291596Szelenkov@nginx.com        assert headers['X-Include'] == 'data', 'including triggered'
730977Smax.romanov@gmail.com
7311596Szelenkov@nginx.com        assert (
7321596Szelenkov@nginx.com            'X-INCLUDE-Id' in headers
7331596Szelenkov@nginx.com        ) == False, 'unable to add headers in include request'
734977Smax.romanov@gmail.com
7351596Szelenkov@nginx.com        assert (
7361596Szelenkov@nginx.com            'javax.servlet.include.request_uri:  null' in body
7371596Szelenkov@nginx.com        ) == True, 'include request uri'
7381848Szelenkov@nginx.com        # assert (
7391596Szelenkov@nginx.com        #    'javax.servlet.include.context_path: null' in body
7401848Szelenkov@nginx.com        # ) == True, 'include request context path'
7411596Szelenkov@nginx.com        assert (
7421596Szelenkov@nginx.com            'javax.servlet.include.servlet_path: null' in body
7431596Szelenkov@nginx.com        ) == True, 'include request servlet path'
7441596Szelenkov@nginx.com        assert (
7451596Szelenkov@nginx.com            'javax.servlet.include.path_info:    null' in body
7461596Szelenkov@nginx.com        ) == True, 'include request path info'
7471596Szelenkov@nginx.com        assert (
7481596Szelenkov@nginx.com            'javax.servlet.include.query_string: null' in body
7491596Szelenkov@nginx.com        ) == True, 'include request query'
750977Smax.romanov@gmail.com
7511596Szelenkov@nginx.com        assert (
7521596Szelenkov@nginx.com            'Before include' in body
7531596Szelenkov@nginx.com        ) == True, 'preserve data added before include() call'
7541596Szelenkov@nginx.com        assert (
7551596Szelenkov@nginx.com            headers['X-After-Include'] == 'you-should-see-this'
7561596Szelenkov@nginx.com        ), 'add headers after include() call'
7571596Szelenkov@nginx.com        assert (
7581596Szelenkov@nginx.com            'After include' in body
7591596Szelenkov@nginx.com        ) == True, 'add data after include() call'
760977Smax.romanov@gmail.com
761977Smax.romanov@gmail.com    def test_java_application_path_translation(self):
762977Smax.romanov@gmail.com        self.load('path_translation')
763977Smax.romanov@gmail.com
764977Smax.romanov@gmail.com        headers = self.get(url='/pt/test?path=/')['headers']
765977Smax.romanov@gmail.com
7661596Szelenkov@nginx.com        assert headers['X-Servlet-Path'] == '/pt', 'matched servlet path'
7671596Szelenkov@nginx.com        assert headers['X-Path-Info'] == '/test', 'the rest of the path'
7681596Szelenkov@nginx.com        assert (
7691596Szelenkov@nginx.com            headers['X-Path-Translated']
7701596Szelenkov@nginx.com            == headers['X-Real-Path'] + headers['X-Path-Info']
7711596Szelenkov@nginx.com        ), 'translated path is the app root + path info'
7721596Szelenkov@nginx.com        assert (
7731596Szelenkov@nginx.com            headers['X-Resource-Paths'].endswith('/WEB-INF/, /index.html]')
7741596Szelenkov@nginx.com            == True
7751596Szelenkov@nginx.com        ), 'app root directory content'
7761596Szelenkov@nginx.com        assert (
7771596Szelenkov@nginx.com            headers['X-Resource-As-Stream'] == 'null'
7781596Szelenkov@nginx.com        ), 'no resource stream for root path'
779977Smax.romanov@gmail.com
780977Smax.romanov@gmail.com        headers = self.get(url='/test?path=/none')['headers']
781977Smax.romanov@gmail.com
7821596Szelenkov@nginx.com        assert headers['X-Servlet-Path'] == '/test', 'matched whole path'
7831596Szelenkov@nginx.com        assert (
7841596Szelenkov@nginx.com            headers['X-Path-Info'] == 'null'
7851596Szelenkov@nginx.com        ), 'the rest of the path is null, whole path matched'
7861596Szelenkov@nginx.com        assert (
7871596Szelenkov@nginx.com            headers['X-Path-Translated'] == 'null'
7881596Szelenkov@nginx.com        ), 'translated path is null because path info is null'
7891596Szelenkov@nginx.com        assert (
7901596Szelenkov@nginx.com            headers['X-Real-Path'].endswith('/none') == True
7911596Szelenkov@nginx.com        ), 'read path is not null'
7921596Szelenkov@nginx.com        assert headers['X-Resource-Paths'] == 'null', 'no resource found'
7931596Szelenkov@nginx.com        assert headers['X-Resource-As-Stream'] == 'null', 'no resource stream'
794977Smax.romanov@gmail.com
795977Smax.romanov@gmail.com    def test_java_application_query_string(self):
796977Smax.romanov@gmail.com        self.load('query_string')
797977Smax.romanov@gmail.com
7981596Szelenkov@nginx.com        assert (
7991596Szelenkov@nginx.com            self.get(url='/?a=b')['headers']['X-Query-String'] == 'a=b'
8001596Szelenkov@nginx.com        ), 'query string'
801977Smax.romanov@gmail.com
802977Smax.romanov@gmail.com    def test_java_application_query_empty(self):
803977Smax.romanov@gmail.com        self.load('query_string')
804977Smax.romanov@gmail.com
8051596Szelenkov@nginx.com        assert (
8061596Szelenkov@nginx.com            self.get(url='/?')['headers']['X-Query-String'] == ''
8071596Szelenkov@nginx.com        ), 'query string empty'
808977Smax.romanov@gmail.com
809977Smax.romanov@gmail.com    def test_java_application_query_absent(self):
810977Smax.romanov@gmail.com        self.load('query_string')
811977Smax.romanov@gmail.com
8121596Szelenkov@nginx.com        assert (
8131596Szelenkov@nginx.com            self.get()['headers']['X-Query-String'] == 'null'
8141596Szelenkov@nginx.com        ), 'query string absent'
815977Smax.romanov@gmail.com
816977Smax.romanov@gmail.com    def test_java_application_empty(self):
817977Smax.romanov@gmail.com        self.load('empty')
818977Smax.romanov@gmail.com
8191596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'empty'
820977Smax.romanov@gmail.com
821977Smax.romanov@gmail.com    def test_java_application_keepalive_body(self):
822977Smax.romanov@gmail.com        self.load('mirror')
823977Smax.romanov@gmail.com
8241596Szelenkov@nginx.com        assert self.post()['status'] == 200, 'init'
8251029Szelenkov@nginx.com
8261453Szelenkov@nginx.com        body = '0123456789' * 500
8271017Szelenkov@nginx.com        (resp, sock) = self.post(
8281017Szelenkov@nginx.com            headers={
8291017Szelenkov@nginx.com                'Connection': 'keep-alive',
8301017Szelenkov@nginx.com                'Content-Type': 'text/html',
8311017Szelenkov@nginx.com                'Host': 'localhost',
8321017Szelenkov@nginx.com            },
8331017Szelenkov@nginx.com            start=True,
8341453Szelenkov@nginx.com            body=body,
8351029Szelenkov@nginx.com            read_timeout=1,
8361017Szelenkov@nginx.com        )
837977Smax.romanov@gmail.com
8381596Szelenkov@nginx.com        assert resp['body'] == body, 'keep-alive 1'
839977Smax.romanov@gmail.com
8401453Szelenkov@nginx.com        body = '0123456789'
8411017Szelenkov@nginx.com        resp = self.post(
8421017Szelenkov@nginx.com            headers={
8431017Szelenkov@nginx.com                'Connection': 'close',
8441017Szelenkov@nginx.com                'Content-Type': 'text/html',
8451017Szelenkov@nginx.com                'Host': 'localhost',
8461017Szelenkov@nginx.com            },
8471017Szelenkov@nginx.com            sock=sock,
8481453Szelenkov@nginx.com            body=body,
8491017Szelenkov@nginx.com        )
850977Smax.romanov@gmail.com
8511596Szelenkov@nginx.com        assert resp['body'] == body, 'keep-alive 2'
852977Smax.romanov@gmail.com
853977Smax.romanov@gmail.com    def test_java_application_http_10(self):
854977Smax.romanov@gmail.com        self.load('empty')
855977Smax.romanov@gmail.com
8561596Szelenkov@nginx.com        assert self.get(http_10=True)['status'] == 200, 'HTTP 1.0'
857977Smax.romanov@gmail.com
858977Smax.romanov@gmail.com    def test_java_application_no_method(self):
859977Smax.romanov@gmail.com        self.load('empty')
860977Smax.romanov@gmail.com
8611596Szelenkov@nginx.com        assert self.post()['status'] == 405, 'no method'
862977Smax.romanov@gmail.com
863977Smax.romanov@gmail.com    def test_java_application_get_header(self):
864977Smax.romanov@gmail.com        self.load('get_header')
865977Smax.romanov@gmail.com
8661596Szelenkov@nginx.com        assert (
8671017Szelenkov@nginx.com            self.get(
8681017Szelenkov@nginx.com                headers={
8691017Szelenkov@nginx.com                    'X-Header': 'blah',
8701017Szelenkov@nginx.com                    'Content-Type': 'text/html',
8711017Szelenkov@nginx.com                    'Host': 'localhost',
8721018Szelenkov@nginx.com                    'Connection': 'close',
8731017Szelenkov@nginx.com                }
8741596Szelenkov@nginx.com            )['headers']['X-Reply']
8751596Szelenkov@nginx.com            == 'blah'
8761596Szelenkov@nginx.com        ), 'get header'
877977Smax.romanov@gmail.com
878977Smax.romanov@gmail.com    def test_java_application_get_header_empty(self):
879977Smax.romanov@gmail.com        self.load('get_header')
880977Smax.romanov@gmail.com
8811596Szelenkov@nginx.com        assert 'X-Reply' not in self.get()['headers'], 'get header empty'
882977Smax.romanov@gmail.com
883977Smax.romanov@gmail.com    def test_java_application_get_headers(self):
884977Smax.romanov@gmail.com        self.load('get_headers')
885977Smax.romanov@gmail.com
8861017Szelenkov@nginx.com        headers = self.get(
8871017Szelenkov@nginx.com            headers={
8881017Szelenkov@nginx.com                'X-Header': ['blah', 'blah'],
8891017Szelenkov@nginx.com                'Content-Type': 'text/html',
8901017Szelenkov@nginx.com                'Host': 'localhost',
8911018Szelenkov@nginx.com                'Connection': 'close',
8921017Szelenkov@nginx.com            }
8931017Szelenkov@nginx.com        )['headers']
894977Smax.romanov@gmail.com
8951596Szelenkov@nginx.com        assert headers['X-Reply-0'] == 'blah', 'get headers'
8961596Szelenkov@nginx.com        assert headers['X-Reply-1'] == 'blah', 'get headers 2'
897977Smax.romanov@gmail.com
898977Smax.romanov@gmail.com    def test_java_application_get_headers_empty(self):
899977Smax.romanov@gmail.com        self.load('get_headers')
900977Smax.romanov@gmail.com
9011596Szelenkov@nginx.com        assert 'X-Reply-0' not in self.get()['headers'], 'get headers empty'
902977Smax.romanov@gmail.com
903977Smax.romanov@gmail.com    def test_java_application_get_header_names(self):
904977Smax.romanov@gmail.com        self.load('get_header_names')
905977Smax.romanov@gmail.com
906977Smax.romanov@gmail.com        headers = self.get()['headers']
907977Smax.romanov@gmail.com
9081596Szelenkov@nginx.com        assert re.search(
9091596Szelenkov@nginx.com            r'(?:Host|Connection)', headers['X-Reply-0']
9101596Szelenkov@nginx.com        ), 'get header names'
9111596Szelenkov@nginx.com        assert re.search(
9121596Szelenkov@nginx.com            r'(?:Host|Connection)', headers['X-Reply-1']
9131596Szelenkov@nginx.com        ), 'get header names 2'
9141596Szelenkov@nginx.com        assert (
9151596Szelenkov@nginx.com            headers['X-Reply-0'] != headers['X-Reply-1']
9161596Szelenkov@nginx.com        ), 'get header names not equal'
917977Smax.romanov@gmail.com
918977Smax.romanov@gmail.com    def test_java_application_header_int(self):
919977Smax.romanov@gmail.com        self.load('header_int')
920977Smax.romanov@gmail.com
9211017Szelenkov@nginx.com        headers = self.get(
9221017Szelenkov@nginx.com            headers={
9231017Szelenkov@nginx.com                'X-Header': '2',
9241017Szelenkov@nginx.com                'Content-Type': 'text/html',
9251017Szelenkov@nginx.com                'Host': 'localhost',
9261018Szelenkov@nginx.com                'Connection': 'close',
9271017Szelenkov@nginx.com            }
9281017Szelenkov@nginx.com        )['headers']
929977Smax.romanov@gmail.com
9301596Szelenkov@nginx.com        assert headers['X-Set-Int'] == '1', 'set int header'
9311596Szelenkov@nginx.com        assert headers['X-Get-Int'] == '2', 'get int header'
932977Smax.romanov@gmail.com
933977Smax.romanov@gmail.com    def test_java_application_header_date(self):
934977Smax.romanov@gmail.com        self.load('header_date')
935977Smax.romanov@gmail.com
936977Smax.romanov@gmail.com        date = 'Fri, 15 Mar 2019 14:45:34 GMT'
937977Smax.romanov@gmail.com
9381017Szelenkov@nginx.com        headers = self.get(
9391017Szelenkov@nginx.com            headers={
9401017Szelenkov@nginx.com                'X-Header': date,
9411017Szelenkov@nginx.com                'Content-Type': 'text/html',
9421017Szelenkov@nginx.com                'Host': 'localhost',
9431018Szelenkov@nginx.com                'Connection': 'close',
9441017Szelenkov@nginx.com            }
9451017Szelenkov@nginx.com        )['headers']
946977Smax.romanov@gmail.com
9471596Szelenkov@nginx.com        assert (
9481596Szelenkov@nginx.com            headers['X-Set-Date'] == 'Thu, 01 Jan 1970 00:00:01 GMT'
9491596Szelenkov@nginx.com        ), 'set date header'
9501596Szelenkov@nginx.com        assert headers['X-Get-Date'] == date, 'get date header'
951977Smax.romanov@gmail.com
9521654Szelenkov@nginx.com    def test_java_application_multipart(self, temp_dir):
9531117Szelenkov@nginx.com        self.load('multipart')
9541117Szelenkov@nginx.com
9551293St.nateldemoura@f5.com        reldst = '/uploads'
9561654Szelenkov@nginx.com        fulldst = temp_dir + reldst
9571293St.nateldemoura@f5.com        os.mkdir(fulldst)
9581596Szelenkov@nginx.com        public_dir(fulldst)
9591293St.nateldemoura@f5.com
9601355St.nateldemoura@f5.com        fields = {
9611355St.nateldemoura@f5.com            'file': {
9621355St.nateldemoura@f5.com                'filename': 'sample.txt',
9631355St.nateldemoura@f5.com                'type': 'text/plain',
9641355St.nateldemoura@f5.com                'data': io.StringIO('Data from sample file'),
9651355St.nateldemoura@f5.com            },
9661355St.nateldemoura@f5.com            'destination': fulldst,
9671355St.nateldemoura@f5.com            'upload': 'Upload',
9681355St.nateldemoura@f5.com        }
9691355St.nateldemoura@f5.com
9701355St.nateldemoura@f5.com        encoded, content_type = self.multipart_encode(fields)
9711355St.nateldemoura@f5.com
9721355St.nateldemoura@f5.com        preamble = 'Preamble. Should be ignored.'
9731355St.nateldemoura@f5.com        epilogue = 'Epilogue. Should be ignored.'
9741355St.nateldemoura@f5.com        body = "%s\r\n%s\r\n%s" % (preamble, encoded.decode(), epilogue)
9751117Szelenkov@nginx.com
9761117Szelenkov@nginx.com        resp = self.post(
9771117Szelenkov@nginx.com            headers={
9781355St.nateldemoura@f5.com                'Content-Type': content_type,
9791117Szelenkov@nginx.com                'Host': 'localhost',
9801117Szelenkov@nginx.com                'Connection': 'close',
9811117Szelenkov@nginx.com            },
9821117Szelenkov@nginx.com            body=body,
9831117Szelenkov@nginx.com        )
9841117Szelenkov@nginx.com
9851596Szelenkov@nginx.com        assert resp['status'] == 200, 'multipart status'
986*2073Szelenkov@nginx.com        assert re.search(r'sample\.txt created', resp['body']), 'multipart body'
9871596Szelenkov@nginx.com        assert (
9881293St.nateldemoura@f5.com            self.search_in_log(
9891293St.nateldemoura@f5.com                r'^Data from sample file$', name=reldst + '/sample.txt'
9901596Szelenkov@nginx.com            )
9911596Szelenkov@nginx.com            is not None
9921596Szelenkov@nginx.com        ), 'file created'
9931685Smax.romanov@nginx.com
9941685Smax.romanov@nginx.com    def test_java_application_threads(self):
9951685Smax.romanov@nginx.com        self.load('threads')
9961685Smax.romanov@nginx.com
9971685Smax.romanov@nginx.com        assert 'success' in self.conf(
9981685Smax.romanov@nginx.com            '4', 'applications/threads/threads'
9991685Smax.romanov@nginx.com        ), 'configure 4 threads'
10001685Smax.romanov@nginx.com
10011685Smax.romanov@nginx.com        socks = []
10021685Smax.romanov@nginx.com
10031685Smax.romanov@nginx.com        for i in range(4):
10041685Smax.romanov@nginx.com            (_, sock) = self.get(
10051685Smax.romanov@nginx.com                headers={
10061685Smax.romanov@nginx.com                    'Host': 'localhost',
10071685Smax.romanov@nginx.com                    'X-Delay': '2',
10081685Smax.romanov@nginx.com                    'Connection': 'close',
10091685Smax.romanov@nginx.com                },
10101685Smax.romanov@nginx.com                no_recv=True,
10111685Smax.romanov@nginx.com                start=True,
10121685Smax.romanov@nginx.com            )
10131685Smax.romanov@nginx.com
10141685Smax.romanov@nginx.com            socks.append(sock)
10151685Smax.romanov@nginx.com
10161848Szelenkov@nginx.com            time.sleep(0.25)  # required to avoid greedy request reading
10171685Smax.romanov@nginx.com
10181685Smax.romanov@nginx.com        threads = set()
10191685Smax.romanov@nginx.com
10201685Smax.romanov@nginx.com        for sock in socks:
10211685Smax.romanov@nginx.com            resp = self.recvall(sock).decode('utf-8')
10221685Smax.romanov@nginx.com
10231685Smax.romanov@nginx.com            self.log_in(resp)
10241685Smax.romanov@nginx.com
10251685Smax.romanov@nginx.com            resp = self._resp_to_dict(resp)
10261685Smax.romanov@nginx.com
10271685Smax.romanov@nginx.com            assert resp['status'] == 200, 'status'
10281685Smax.romanov@nginx.com
10291685Smax.romanov@nginx.com            threads.add(resp['headers']['X-Thread'])
10301685Smax.romanov@nginx.com
10311685Smax.romanov@nginx.com            sock.close()
10321685Smax.romanov@nginx.com
10331685Smax.romanov@nginx.com        assert len(socks) == len(threads), 'threads differs'
1034