4545 server_name localhost 127.0.0.1;
4646root /srv/http; # And also this one
4747 mykey "myvalue; #notme myothervalue";
48+ "quoted_key" "quoted_value";
4849 # This one too
4950 index index.php;
5051if (!-e $request_filename)
101102"""
102103
103104
105+ TESTBLOCK_CASE_5 = """
106+ upstream test0 {
107+ server 1.1.1.1:8080;
108+ send "some request";
109+ }
110+
111+ upstream test1 {
112+ server 1.1.1.1:8080;
113+ send 'some request';
114+ }
115+
116+ server {
117+ server_name "www.example.com";
118+
119+ location / {
120+ root html;
121+ }
122+ }
123+ """
124+
125+
126+ TESTBLOCK_CASE_6 = """
127+ upstream test0 {
128+ server 1.1.1.1:8080;
129+ check interval=3000 rise=2 fall=3 timeout=3000 type=http;
130+ check_http_send "GET /alive.html HTTP/1.0\r \n \r \n ";
131+ check_http_expect_alive http_2xx http_3xx;
132+ }
133+
134+ upstream test1 {
135+ ip_hash;
136+ server 2.2.2.2:9000;
137+ check_http_send 'GET /alive.html HTTP/1.0\r \n \r \n ';
138+ }
139+ """
140+
141+ TESTBLOCK_CASE_7 = """
142+ upstream xx.com_backend {
143+ server 10.193.2.2:9061 weight=1 max_fails=2 fail_timeout=30s;
144+ server 10.193.2.1:9061 weight=1 max_fails=2 fail_timeout=30s;
145+ session_sticky;
146+ }
147+
148+ server {
149+ listen 80;
150+
151+ location / {
152+ set $xlocation 'test';
153+ proxy_pass http://xx.com_backend;
154+ }
155+ }
156+ """
157+
158+
159+
104160class TestPythonNginx (unittest .TestCase ):
105161 def test_basic_load (self ):
106162 self .assertTrue (nginx .loads (TESTBLOCK_CASE_1 ) is not None )
@@ -124,21 +180,24 @@ def test_key_parse(self):
124180 self .assertEqual (firstKey .name , 'listen' )
125181 self .assertEqual (firstKey .value , '80' )
126182 self .assertEqual (thirdKey .name , 'mykey' )
127- self .assertEqual (thirdKey .value , 'myvalue; #notme myothervalue' )
183+ self .assertEqual (thirdKey .value , '" myvalue; #notme myothervalue" ' )
128184
129185 def test_key_parse_complex (self ):
130186 data = nginx .loads (TESTBLOCK_CASE_2 )
131- self .assertEqual (len (data .server .keys ), 5 )
187+ self .assertEqual (len (data .server .keys ), 6 )
132188 firstKey = data .server .keys [0 ]
133189 thirdKey = data .server .keys [3 ]
190+ fourthKey = data .server .keys [4 ]
134191 self .assertEqual (firstKey .name , 'listen' )
135192 self .assertEqual (firstKey .value , '80' )
136193 self .assertEqual (thirdKey .name , 'mykey' )
137- self .assertEqual (thirdKey .value , 'myvalue; #notme myothervalue' )
194+ self .assertEqual (thirdKey .value , '" myvalue; #notme myothervalue" ' )
138195 self .assertEqual (
139196 data .server .locations [- 1 ].keys [0 ].value ,
140197 "301 $scheme://$host:$server_port${request_uri}bitbucket/"
141198 )
199+ self .assertEqual (fourthKey .name , '"quoted_key"' )
200+ self .assertEqual (fourthKey .value , '"quoted_value"' )
142201
143202 def test_location_parse (self ):
144203 data = nginx .loads (TESTBLOCK_CASE_1 )
@@ -162,6 +221,21 @@ def test_reflection(self):
162221 out_data = '\n ' + nginx .dumps (inp_data )
163222 self .assertEqual (TESTBLOCK_CASE_1 , out_data )
164223
224+ def test_quoted_key_value (self ):
225+ data = nginx .loads (TESTBLOCK_CASE_5 )
226+ out_data = '\n ' + nginx .dumps (data )
227+ self .assertEqual (out_data , TESTBLOCK_CASE_5 )
228+
229+ def test_complex_upstream (self ):
230+ inp_data = nginx .loads (TESTBLOCK_CASE_6 )
231+ out_data = '\n ' + nginx .dumps (inp_data )
232+ self .assertEqual (TESTBLOCK_CASE_6 , out_data )
233+
234+ def test_session_sticky (self ):
235+ inp_data = nginx .loads (TESTBLOCK_CASE_7 )
236+ out_data = '\n ' + nginx .dumps (inp_data )
237+ self .assertEqual (TESTBLOCK_CASE_7 , out_data )
238+
165239 def test_filtering (self ):
166240 data = nginx .loads (TESTBLOCK_CASE_1 )
167241 self .assertEqual (len (data .server .filter ('Key' , 'mykey' )), 1 )
0 commit comments