Deleted
Added
test_proxy.py (1477:b93d1acf81bd) | test_proxy.py (1596:b7e2d4d92624) |
---|---|
1import pytest |
|
1import re 2import socket 3import time | 2import re 3import socket 4import time |
4import unittest | |
5 6from unit.applications.lang.python import TestApplicationPython | 5 6from unit.applications.lang.python import TestApplicationPython |
7from conftest import option, skip_alert |
|
7 8 9class TestProxy(TestApplicationPython): 10 prerequisites = {'modules': {'python': 'any'}} 11 12 SERVER_PORT = 7999 13 14 @staticmethod --- 22 unchanged lines hidden (view full) --- 37 38 while True: 39 connection, client_address = sock.accept() 40 41 data = recvall(connection).decode() 42 43 to_send = req 44 | 8 9 10class TestProxy(TestApplicationPython): 11 prerequisites = {'modules': {'python': 'any'}} 12 13 SERVER_PORT = 7999 14 15 @staticmethod --- 22 unchanged lines hidden (view full) --- 38 39 while True: 40 connection, client_address = sock.accept() 41 42 data = recvall(connection).decode() 43 44 to_send = req 45 |
45 m = re.search('X-Len: (\d+)', data) | 46 m = re.search(r'X-Len: (\d+)', data) |
46 if m: 47 to_send += b'X' * int(m.group(1)) 48 49 connection.sendall(to_send) 50 51 connection.close() 52 53 def get_http10(self, *args, **kwargs): 54 return self.get(*args, http_10=True, **kwargs) 55 56 def post_http10(self, *args, **kwargs): 57 return self.post(*args, http_10=True, **kwargs) 58 | 47 if m: 48 to_send += b'X' * int(m.group(1)) 49 50 connection.sendall(to_send) 51 52 connection.close() 53 54 def get_http10(self, *args, **kwargs): 55 return self.get(*args, http_10=True, **kwargs) 56 57 def post_http10(self, *args, **kwargs): 58 return self.post(*args, http_10=True, **kwargs) 59 |
59 def setUp(self): 60 super().setUp() | 60 def setup_method(self): 61 super().setup_method() |
61 62 self.run_process(self.run_server, self.SERVER_PORT) 63 self.waitforsocket(self.SERVER_PORT) 64 | 62 63 self.run_process(self.run_server, self.SERVER_PORT) 64 self.waitforsocket(self.SERVER_PORT) 65 |
65 self.assertIn( 66 'success', 67 self.conf( 68 { 69 "listeners": { 70 "*:7080": {"pass": "routes"}, 71 "*:7081": {"pass": "applications/mirror"}, | 66 assert 'success' in self.conf( 67 { 68 "listeners": { 69 "*:7080": {"pass": "routes"}, 70 "*:7081": {"pass": "applications/mirror"}, 71 }, 72 "routes": [{"action": {"proxy": "http://127.0.0.1:7081"}}], 73 "applications": { 74 "mirror": { 75 "type": "python", 76 "processes": {"spare": 0}, 77 "path": option.test_dir + "/python/mirror", 78 "working_directory": option.test_dir 79 + "/python/mirror", 80 "module": "wsgi", |
72 }, | 81 }, |
73 "routes": [{"action": {"proxy": "http://127.0.0.1:7081"}}], 74 "applications": { 75 "mirror": { 76 "type": "python", 77 "processes": {"spare": 0}, 78 "path": self.current_dir + "/python/mirror", 79 "working_directory": self.current_dir 80 + "/python/mirror", 81 "module": "wsgi", 82 }, 83 "custom_header": { 84 "type": "python", 85 "processes": {"spare": 0}, 86 "path": self.current_dir + "/python/custom_header", 87 "working_directory": self.current_dir 88 + "/python/custom_header", 89 "module": "wsgi", 90 }, 91 "delayed": { 92 "type": "python", 93 "processes": {"spare": 0}, 94 "path": self.current_dir + "/python/delayed", 95 "working_directory": self.current_dir 96 + "/python/delayed", 97 "module": "wsgi", 98 }, | 82 "custom_header": { 83 "type": "python", 84 "processes": {"spare": 0}, 85 "path": option.test_dir + "/python/custom_header", 86 "working_directory": option.test_dir 87 + "/python/custom_header", 88 "module": "wsgi", |
99 }, | 89 }, |
100 } 101 ), 102 'proxy initial configuration', 103 ) | 90 "delayed": { 91 "type": "python", 92 "processes": {"spare": 0}, 93 "path": option.test_dir + "/python/delayed", 94 "working_directory": option.test_dir 95 + "/python/delayed", 96 "module": "wsgi", 97 }, 98 }, 99 } 100 ), 'proxy initial configuration' |
104 105 def test_proxy_http10(self): 106 for _ in range(10): | 101 102 def test_proxy_http10(self): 103 for _ in range(10): |
107 self.assertEqual(self.get_http10()['status'], 200, 'status') | 104 assert self.get_http10()['status'] == 200, 'status' |
108 109 def test_proxy_chain(self): | 105 106 def test_proxy_chain(self): |
110 self.assertIn( 111 'success', 112 self.conf( 113 { 114 "listeners": { 115 "*:7080": {"pass": "routes/first"}, 116 "*:7081": {"pass": "routes/second"}, 117 "*:7082": {"pass": "routes/third"}, 118 "*:7083": {"pass": "routes/fourth"}, 119 "*:7084": {"pass": "routes/fifth"}, 120 "*:7085": {"pass": "applications/mirror"}, 121 }, 122 "routes": { 123 "first": [ 124 {"action": {"proxy": "http://127.0.0.1:7081"}} 125 ], 126 "second": [ 127 {"action": {"proxy": "http://127.0.0.1:7082"}} 128 ], 129 "third": [ 130 {"action": {"proxy": "http://127.0.0.1:7083"}} 131 ], 132 "fourth": [ 133 {"action": {"proxy": "http://127.0.0.1:7084"}} 134 ], 135 "fifth": [ 136 {"action": {"proxy": "http://127.0.0.1:7085"}} 137 ], 138 }, 139 "applications": { 140 "mirror": { 141 "type": "python", 142 "processes": {"spare": 0}, 143 "path": self.current_dir + "/python/mirror", 144 "working_directory": self.current_dir 145 + "/python/mirror", 146 "module": "wsgi", 147 } 148 }, 149 } 150 ), 151 'proxy chain configuration', 152 ) | 107 assert 'success' in self.conf( 108 { 109 "listeners": { 110 "*:7080": {"pass": "routes/first"}, 111 "*:7081": {"pass": "routes/second"}, 112 "*:7082": {"pass": "routes/third"}, 113 "*:7083": {"pass": "routes/fourth"}, 114 "*:7084": {"pass": "routes/fifth"}, 115 "*:7085": {"pass": "applications/mirror"}, 116 }, 117 "routes": { 118 "first": [{"action": {"proxy": "http://127.0.0.1:7081"}}], 119 "second": [{"action": {"proxy": "http://127.0.0.1:7082"}}], 120 "third": [{"action": {"proxy": "http://127.0.0.1:7083"}}], 121 "fourth": [{"action": {"proxy": "http://127.0.0.1:7084"}}], 122 "fifth": [{"action": {"proxy": "http://127.0.0.1:7085"}}], 123 }, 124 "applications": { 125 "mirror": { 126 "type": "python", 127 "processes": {"spare": 0}, 128 "path": option.test_dir + "/python/mirror", 129 "working_directory": option.test_dir 130 + "/python/mirror", 131 "module": "wsgi", 132 } 133 }, 134 } 135 ), 'proxy chain configuration' |
153 | 136 |
154 self.assertEqual(self.get_http10()['status'], 200, 'status') | 137 assert self.get_http10()['status'] == 200, 'status' |
155 156 def test_proxy_body(self): 157 payload = '0123456789' 158 for _ in range(10): 159 resp = self.post_http10(body=payload) 160 | 138 139 def test_proxy_body(self): 140 payload = '0123456789' 141 for _ in range(10): 142 resp = self.post_http10(body=payload) 143 |
161 self.assertEqual(resp['status'], 200, 'status') 162 self.assertEqual(resp['body'], payload, 'body') | 144 assert resp['status'] == 200, 'status' 145 assert resp['body'] == payload, 'body' |
163 164 payload = 'X' * 4096 165 for _ in range(10): 166 resp = self.post_http10(body=payload) 167 | 146 147 payload = 'X' * 4096 148 for _ in range(10): 149 resp = self.post_http10(body=payload) 150 |
168 self.assertEqual(resp['status'], 200, 'status') 169 self.assertEqual(resp['body'], payload, 'body') | 151 assert resp['status'] == 200, 'status' 152 assert resp['body'] == payload, 'body' |
170 171 payload = 'X' * 4097 172 for _ in range(10): 173 resp = self.post_http10(body=payload) 174 | 153 154 payload = 'X' * 4097 155 for _ in range(10): 156 resp = self.post_http10(body=payload) 157 |
175 self.assertEqual(resp['status'], 200, 'status') 176 self.assertEqual(resp['body'], payload, 'body') | 158 assert resp['status'] == 200, 'status' 159 assert resp['body'] == payload, 'body' |
177 178 payload = 'X' * 4096 * 256 179 for _ in range(10): 180 resp = self.post_http10(body=payload, read_buffer_size=4096 * 128) 181 | 160 161 payload = 'X' * 4096 * 256 162 for _ in range(10): 163 resp = self.post_http10(body=payload, read_buffer_size=4096 * 128) 164 |
182 self.assertEqual(resp['status'], 200, 'status') 183 self.assertEqual(resp['body'], payload, 'body') | 165 assert resp['status'] == 200, 'status' 166 assert resp['body'] == payload, 'body' |
184 185 payload = 'X' * 4096 * 257 186 for _ in range(10): 187 resp = self.post_http10(body=payload, read_buffer_size=4096 * 128) 188 | 167 168 payload = 'X' * 4096 * 257 169 for _ in range(10): 170 resp = self.post_http10(body=payload, read_buffer_size=4096 * 128) 171 |
189 self.assertEqual(resp['status'], 200, 'status') 190 self.assertEqual(resp['body'], payload, 'body') | 172 assert resp['status'] == 200, 'status' 173 assert resp['body'] == payload, 'body' |
191 192 self.conf({'http': {'max_body_size': 32 * 1024 * 1024}}, 'settings') 193 194 payload = '0123456789abcdef' * 32 * 64 * 1024 195 resp = self.post_http10(body=payload, read_buffer_size=1024 * 1024) | 174 175 self.conf({'http': {'max_body_size': 32 * 1024 * 1024}}, 'settings') 176 177 payload = '0123456789abcdef' * 32 * 64 * 1024 178 resp = self.post_http10(body=payload, read_buffer_size=1024 * 1024) |
196 self.assertEqual(resp['status'], 200, 'status') 197 self.assertEqual(resp['body'], payload, 'body') | 179 assert resp['status'] == 200, 'status' 180 assert resp['body'] == payload, 'body' |
198 199 def test_proxy_parallel(self): 200 payload = 'X' * 4096 * 257 201 buff_size = 4096 * 258 202 203 socks = [] 204 for i in range(10): 205 _, sock = self.post_http10( --- 5 unchanged lines hidden (view full) --- 211 socks.append(sock) 212 213 for i in range(10): 214 resp = self.recvall(socks[i], buff_size=buff_size).decode() 215 socks[i].close() 216 217 resp = self._resp_to_dict(resp) 218 | 181 182 def test_proxy_parallel(self): 183 payload = 'X' * 4096 * 257 184 buff_size = 4096 * 258 185 186 socks = [] 187 for i in range(10): 188 _, sock = self.post_http10( --- 5 unchanged lines hidden (view full) --- 194 socks.append(sock) 195 196 for i in range(10): 197 resp = self.recvall(socks[i], buff_size=buff_size).decode() 198 socks[i].close() 199 200 resp = self._resp_to_dict(resp) 201 |
219 self.assertEqual(resp['status'], 200, 'status') 220 self.assertEqual(resp['body'], payload + str(i), 'body') | 202 assert resp['status'] == 200, 'status' 203 assert resp['body'] == payload + str(i), 'body' |
221 222 def test_proxy_header(self): | 204 205 def test_proxy_header(self): |
223 self.assertIn( 224 'success', 225 self.conf( 226 {"pass": "applications/custom_header"}, 'listeners/*:7081' 227 ), 228 'custom_header configure', 229 ) | 206 assert 'success' in self.conf( 207 {"pass": "applications/custom_header"}, 'listeners/*:7081' 208 ), 'custom_header configure' |
230 231 header_value = 'blah' | 209 210 header_value = 'blah' |
232 self.assertEqual( | 211 assert ( |
233 self.get_http10( 234 headers={'Host': 'localhost', 'Custom-Header': header_value} | 212 self.get_http10( 213 headers={'Host': 'localhost', 'Custom-Header': header_value} |
235 )['headers']['Custom-Header'], 236 header_value, 237 'custom header', 238 ) | 214 )['headers']['Custom-Header'] 215 == header_value 216 ), 'custom header' |
239 240 header_value = '(),/:;<=>?@[\]{}\t !#$%&\'*+-.^_`|~' | 217 218 header_value = '(),/:;<=>?@[\]{}\t !#$%&\'*+-.^_`|~' |
241 self.assertEqual( | 219 assert ( |
242 self.get_http10( 243 headers={'Host': 'localhost', 'Custom-Header': header_value} | 220 self.get_http10( 221 headers={'Host': 'localhost', 'Custom-Header': header_value} |
244 )['headers']['Custom-Header'], 245 header_value, 246 'custom header 2', 247 ) | 222 )['headers']['Custom-Header'] 223 == header_value 224 ), 'custom header 2' |
248 249 header_value = 'X' * 4096 | 225 226 header_value = 'X' * 4096 |
250 self.assertEqual( | 227 assert ( |
251 self.get_http10( 252 headers={'Host': 'localhost', 'Custom-Header': header_value} | 228 self.get_http10( 229 headers={'Host': 'localhost', 'Custom-Header': header_value} |
253 )['headers']['Custom-Header'], 254 header_value, 255 'custom header 3', 256 ) | 230 )['headers']['Custom-Header'] 231 == header_value 232 ), 'custom header 3' |
257 258 header_value = 'X' * 8191 | 233 234 header_value = 'X' * 8191 |
259 self.assertEqual( | 235 assert ( |
260 self.get_http10( 261 headers={'Host': 'localhost', 'Custom-Header': header_value} | 236 self.get_http10( 237 headers={'Host': 'localhost', 'Custom-Header': header_value} |
262 )['headers']['Custom-Header'], 263 header_value, 264 'custom header 4', 265 ) | 238 )['headers']['Custom-Header'] 239 == header_value 240 ), 'custom header 4' |
266 267 header_value = 'X' * 8192 | 241 242 header_value = 'X' * 8192 |
268 self.assertEqual( | 243 assert ( |
269 self.get_http10( 270 headers={'Host': 'localhost', 'Custom-Header': header_value} | 244 self.get_http10( 245 headers={'Host': 'localhost', 'Custom-Header': header_value} |
271 )['status'], 272 431, 273 'custom header 5', 274 ) | 246 )['status'] 247 == 431 248 ), 'custom header 5' |
275 276 def test_proxy_fragmented(self): 277 _, sock = self.http( 278 b"""GET / HTT""", raw=True, start=True, no_recv=True 279 ) 280 281 time.sleep(1) 282 283 sock.sendall("P/1.0\r\nHost: localhos".encode()) 284 285 time.sleep(1) 286 287 sock.sendall("t\r\n\r\n".encode()) 288 | 249 250 def test_proxy_fragmented(self): 251 _, sock = self.http( 252 b"""GET / HTT""", raw=True, start=True, no_recv=True 253 ) 254 255 time.sleep(1) 256 257 sock.sendall("P/1.0\r\nHost: localhos".encode()) 258 259 time.sleep(1) 260 261 sock.sendall("t\r\n\r\n".encode()) 262 |
289 self.assertRegex( 290 self.recvall(sock).decode(), '200 OK', 'fragmented send' 291 ) | 263 assert re.search( 264 '200 OK', self.recvall(sock).decode() 265 ), 'fragmented send' |
292 sock.close() 293 294 def test_proxy_fragmented_close(self): 295 _, sock = self.http( 296 b"""GET / HTT""", raw=True, start=True, no_recv=True 297 ) 298 299 time.sleep(1) --- 23 unchanged lines hidden (view full) --- 323 324 time.sleep(1) 325 326 sock.sendall(("X" * 10000).encode()) 327 328 resp = self._resp_to_dict(self.recvall(sock).decode()) 329 sock.close() 330 | 266 sock.close() 267 268 def test_proxy_fragmented_close(self): 269 _, sock = self.http( 270 b"""GET / HTT""", raw=True, start=True, no_recv=True 271 ) 272 273 time.sleep(1) --- 23 unchanged lines hidden (view full) --- 297 298 time.sleep(1) 299 300 sock.sendall(("X" * 10000).encode()) 301 302 resp = self._resp_to_dict(self.recvall(sock).decode()) 303 sock.close() 304 |
331 self.assertEqual(resp['status'], 200, 'status') 332 self.assertEqual(resp['body'], "X" * 30000, 'body') | 305 assert resp['status'] == 200, 'status' 306 assert resp['body'] == "X" * 30000, 'body' |
333 334 def test_proxy_fragmented_body_close(self): 335 _, sock = self.http( 336 b"""GET / HTT""", raw=True, start=True, no_recv=True 337 ) 338 339 time.sleep(1) 340 341 sock.sendall("P/1.0\r\nHost: localhost\r\n".encode()) 342 sock.sendall("Content-Length: 30000\r\n".encode()) 343 344 time.sleep(1) 345 346 sock.sendall("\r\n".encode()) 347 sock.sendall(("X" * 10000).encode()) 348 349 sock.close() 350 351 def test_proxy_nowhere(self): | 307 308 def test_proxy_fragmented_body_close(self): 309 _, sock = self.http( 310 b"""GET / HTT""", raw=True, start=True, no_recv=True 311 ) 312 313 time.sleep(1) 314 315 sock.sendall("P/1.0\r\nHost: localhost\r\n".encode()) 316 sock.sendall("Content-Length: 30000\r\n".encode()) 317 318 time.sleep(1) 319 320 sock.sendall("\r\n".encode()) 321 sock.sendall(("X" * 10000).encode()) 322 323 sock.close() 324 325 def test_proxy_nowhere(self): |
352 self.assertIn( 353 'success', 354 self.conf( 355 [{"action": {"proxy": "http://127.0.0.1:7082"}}], 'routes' 356 ), 357 'proxy path changed', 358 ) | 326 assert 'success' in self.conf( 327 [{"action": {"proxy": "http://127.0.0.1:7082"}}], 'routes' 328 ), 'proxy path changed' |
359 | 329 |
360 self.assertEqual(self.get_http10()['status'], 502, 'status') | 330 assert self.get_http10()['status'] == 502, 'status' |
361 362 def test_proxy_ipv6(self): | 331 332 def test_proxy_ipv6(self): |
363 self.assertIn( 364 'success', 365 self.conf( 366 { 367 "*:7080": {"pass": "routes"}, 368 "[::1]:7081": {'application': 'mirror'}, 369 }, 370 'listeners', 371 ), 372 'add ipv6 listener configure', 373 ) | 333 assert 'success' in self.conf( 334 { 335 "*:7080": {"pass": "routes"}, 336 "[::1]:7081": {'application': 'mirror'}, 337 }, 338 'listeners', 339 ), 'add ipv6 listener configure' |
374 | 340 |
375 self.assertIn( 376 'success', 377 self.conf([{"action": {"proxy": "http://[::1]:7081"}}], 'routes'), 378 'proxy ipv6 configure', 379 ) | 341 assert 'success' in self.conf( 342 [{"action": {"proxy": "http://[::1]:7081"}}], 'routes' 343 ), 'proxy ipv6 configure' |
380 | 344 |
381 self.assertEqual(self.get_http10()['status'], 200, 'status') | 345 assert self.get_http10()['status'] == 200, 'status' |
382 383 def test_proxy_unix(self): | 346 347 def test_proxy_unix(self): |
384 addr = self.testdir + '/sock' | 348 addr = self.temp_dir + '/sock' |
385 | 349 |
386 self.assertIn( 387 'success', 388 self.conf( 389 { 390 "*:7080": {"pass": "routes"}, 391 "unix:" + addr: {'application': 'mirror'}, 392 }, 393 'listeners', 394 ), 395 'add unix listener configure', 396 ) | 350 assert 'success' in self.conf( 351 { 352 "*:7080": {"pass": "routes"}, 353 "unix:" + addr: {'application': 'mirror'}, 354 }, 355 'listeners', 356 ), 'add unix listener configure' |
397 | 357 |
398 self.assertIn( 399 'success', 400 self.conf( 401 [{"action": {"proxy": 'http://unix:' + addr}}], 'routes' 402 ), 403 'proxy unix configure', 404 ) | 358 assert 'success' in self.conf( 359 [{"action": {"proxy": 'http://unix:' + addr}}], 'routes' 360 ), 'proxy unix configure' |
405 | 361 |
406 self.assertEqual(self.get_http10()['status'], 200, 'status') | 362 assert self.get_http10()['status'] == 200, 'status' |
407 408 def test_proxy_delayed(self): | 363 364 def test_proxy_delayed(self): |
409 self.assertIn( 410 'success', 411 self.conf( 412 {"pass": "applications/delayed"}, 'listeners/*:7081' 413 ), 414 'delayed configure', 415 ) | 365 assert 'success' in self.conf( 366 {"pass": "applications/delayed"}, 'listeners/*:7081' 367 ), 'delayed configure' |
416 417 body = '0123456789' * 1000 418 resp = self.post_http10( 419 headers={ 420 'Host': 'localhost', 421 'Content-Type': 'text/html', 422 'Content-Length': str(len(body)), 423 'X-Parts': '2', 424 'X-Delay': '1', 425 }, 426 body=body, 427 ) 428 | 368 369 body = '0123456789' * 1000 370 resp = self.post_http10( 371 headers={ 372 'Host': 'localhost', 373 'Content-Type': 'text/html', 374 'Content-Length': str(len(body)), 375 'X-Parts': '2', 376 'X-Delay': '1', 377 }, 378 body=body, 379 ) 380 |
429 self.assertEqual(resp['status'], 200, 'status') 430 self.assertEqual(resp['body'], body, 'body') | 381 assert resp['status'] == 200, 'status' 382 assert resp['body'] == body, 'body' |
431 432 resp = self.post_http10( 433 headers={ 434 'Host': 'localhost', 435 'Content-Type': 'text/html', 436 'Content-Length': str(len(body)), 437 'X-Parts': '2', 438 'X-Delay': '1', 439 }, 440 body=body, 441 ) 442 | 383 384 resp = self.post_http10( 385 headers={ 386 'Host': 'localhost', 387 'Content-Type': 'text/html', 388 'Content-Length': str(len(body)), 389 'X-Parts': '2', 390 'X-Delay': '1', 391 }, 392 body=body, 393 ) 394 |
443 self.assertEqual(resp['status'], 200, 'status') 444 self.assertEqual(resp['body'], body, 'body') | 395 assert resp['status'] == 200, 'status' 396 assert resp['body'] == body, 'body' |
445 446 def test_proxy_delayed_close(self): | 397 398 def test_proxy_delayed_close(self): |
447 self.assertIn( 448 'success', 449 self.conf( 450 {"pass": "applications/delayed"}, 'listeners/*:7081' 451 ), 452 'delayed configure', 453 ) | 399 assert 'success' in self.conf( 400 {"pass": "applications/delayed"}, 'listeners/*:7081' 401 ), 'delayed configure' |
454 455 _, sock = self.post_http10( 456 headers={ 457 'Host': 'localhost', 458 'Content-Type': 'text/html', 459 'Content-Length': '10000', 460 'X-Parts': '3', 461 'X-Delay': '1', 462 }, 463 body='0123456789' * 1000, 464 start=True, 465 no_recv=True, 466 ) 467 | 402 403 _, sock = self.post_http10( 404 headers={ 405 'Host': 'localhost', 406 'Content-Type': 'text/html', 407 'Content-Length': '10000', 408 'X-Parts': '3', 409 'X-Delay': '1', 410 }, 411 body='0123456789' * 1000, 412 start=True, 413 no_recv=True, 414 ) 415 |
468 self.assertRegex( 469 sock.recv(100).decode(), '200 OK', 'first' 470 ) | 416 assert re.search('200 OK', sock.recv(100).decode()), 'first' |
471 sock.close() 472 473 _, sock = self.post_http10( 474 headers={ 475 'Host': 'localhost', 476 'Content-Type': 'text/html', 477 'Content-Length': '10000', 478 'X-Parts': '3', 479 'X-Delay': '1', 480 }, 481 body='0123456789' * 1000, 482 start=True, 483 no_recv=True, 484 ) 485 | 417 sock.close() 418 419 _, sock = self.post_http10( 420 headers={ 421 'Host': 'localhost', 422 'Content-Type': 'text/html', 423 'Content-Length': '10000', 424 'X-Parts': '3', 425 'X-Delay': '1', 426 }, 427 body='0123456789' * 1000, 428 start=True, 429 no_recv=True, 430 ) 431 |
486 self.assertRegex( 487 sock.recv(100).decode(), '200 OK', 'second' 488 ) | 432 assert re.search('200 OK', sock.recv(100).decode()), 'second' |
489 sock.close() 490 | 433 sock.close() 434 |
491 @unittest.skip('not yet') | 435 @pytest.mark.skip('not yet') |
492 def test_proxy_content_length(self): | 436 def test_proxy_content_length(self): |
493 self.assertIn( 494 'success', 495 self.conf( 496 [ 497 { 498 "action": { 499 "proxy": "http://127.0.0.1:" 500 + str(self.SERVER_PORT) 501 } | 437 assert 'success' in self.conf( 438 [ 439 { 440 "action": { 441 "proxy": "http://127.0.0.1:" + str(self.SERVER_PORT) |
502 } | 442 } |
503 ], 504 'routes', 505 ), 506 'proxy backend configure', 507 ) | 443 } 444 ], 445 'routes', 446 ), 'proxy backend configure' |
508 509 resp = self.get_http10() | 447 448 resp = self.get_http10() |
510 self.assertEqual(len(resp['body']), 0, 'body lt Content-Length 0') | 449 assert len(resp['body']) == 0, 'body lt Content-Length 0' |
511 512 resp = self.get_http10(headers={'Host': 'localhost', 'X-Len': '5'}) | 450 451 resp = self.get_http10(headers={'Host': 'localhost', 'X-Len': '5'}) |
513 self.assertEqual(len(resp['body']), 5, 'body lt Content-Length 5') | 452 assert len(resp['body']) == 5, 'body lt Content-Length 5' |
514 515 resp = self.get_http10(headers={'Host': 'localhost', 'X-Len': '9'}) | 453 454 resp = self.get_http10(headers={'Host': 'localhost', 'X-Len': '9'}) |
516 self.assertEqual(len(resp['body']), 9, 'body lt Content-Length 9') | 455 assert len(resp['body']) == 9, 'body lt Content-Length 9' |
517 518 resp = self.get_http10(headers={'Host': 'localhost', 'X-Len': '11'}) | 456 457 resp = self.get_http10(headers={'Host': 'localhost', 'X-Len': '11'}) |
519 self.assertEqual(len(resp['body']), 10, 'body gt Content-Length 11') | 458 assert len(resp['body']) == 10, 'body gt Content-Length 11' |
520 521 resp = self.get_http10(headers={'Host': 'localhost', 'X-Len': '15'}) | 459 460 resp = self.get_http10(headers={'Host': 'localhost', 'X-Len': '15'}) |
522 self.assertEqual(len(resp['body']), 10, 'body gt Content-Length 15') | 461 assert len(resp['body']) == 10, 'body gt Content-Length 15' |
523 524 def test_proxy_invalid(self): 525 def check_proxy(proxy): | 462 463 def test_proxy_invalid(self): 464 def check_proxy(proxy): |
526 self.assertIn( 527 'error', 528 self.conf([{"action": {"proxy": proxy}}], 'routes'), 529 'proxy invalid', 530 ) | 465 assert 'error' in \ 466 self.conf([{"action": {"proxy": proxy}}], 'routes'), \ 467 'proxy invalid' |
531 532 check_proxy('blah') 533 check_proxy('/blah') 534 check_proxy('unix:/blah') 535 check_proxy('http://blah') 536 check_proxy('http://127.0.0.1') 537 check_proxy('http://127.0.0.1:') 538 check_proxy('http://127.0.0.1:blah') 539 check_proxy('http://127.0.0.1:-1') 540 check_proxy('http://127.0.0.1:7080b') 541 check_proxy('http://[]') 542 check_proxy('http://[]:7080') 543 check_proxy('http://[:]:7080') 544 check_proxy('http://[::7080') 545 546 def test_proxy_loop(self): | 468 469 check_proxy('blah') 470 check_proxy('/blah') 471 check_proxy('unix:/blah') 472 check_proxy('http://blah') 473 check_proxy('http://127.0.0.1') 474 check_proxy('http://127.0.0.1:') 475 check_proxy('http://127.0.0.1:blah') 476 check_proxy('http://127.0.0.1:-1') 477 check_proxy('http://127.0.0.1:7080b') 478 check_proxy('http://[]') 479 check_proxy('http://[]:7080') 480 check_proxy('http://[:]:7080') 481 check_proxy('http://[::7080') 482 483 def test_proxy_loop(self): |
547 self.skip_alerts.extend( 548 [ 549 r'socket.*failed', 550 r'accept.*failed', 551 r'new connections are not accepted', 552 ] | 484 skip_alert( 485 r'socket.*failed', 486 r'accept.*failed', 487 r'new connections are not accepted', |
553 ) 554 self.conf( 555 { 556 "listeners": { 557 "*:7080": {"pass": "routes"}, 558 "*:7081": {"pass": "applications/mirror"}, 559 "*:7082": {"pass": "routes"}, 560 }, 561 "routes": [{"action": {"proxy": "http://127.0.0.1:7082"}}], 562 "applications": { 563 "mirror": { 564 "type": "python", 565 "processes": {"spare": 0}, | 488 ) 489 self.conf( 490 { 491 "listeners": { 492 "*:7080": {"pass": "routes"}, 493 "*:7081": {"pass": "applications/mirror"}, 494 "*:7082": {"pass": "routes"}, 495 }, 496 "routes": [{"action": {"proxy": "http://127.0.0.1:7082"}}], 497 "applications": { 498 "mirror": { 499 "type": "python", 500 "processes": {"spare": 0}, |
566 "path": self.current_dir + "/python/mirror", 567 "working_directory": self.current_dir 568 + "/python/mirror", | 501 "path": option.test_dir + "/python/mirror", 502 "working_directory": option.test_dir + "/python/mirror", |
569 "module": "wsgi", 570 }, 571 }, 572 } 573 ) 574 575 self.get_http10(no_recv=True) 576 self.get_http10(read_timeout=1) | 503 "module": "wsgi", 504 }, 505 }, 506 } 507 ) 508 509 self.get_http10(no_recv=True) 510 self.get_http10(read_timeout=1) |
577 578if __name__ == '__main__': 579 TestProxy.main() | |