1import re 2import subprocess 3 4import pytest 5from unit.applications.lang.ruby import TestApplicationRuby 6 7 8class TestRubyApplication(TestApplicationRuby): 9 prerequisites = {'modules': {'ruby': 'all'}} 10 11 def test_ruby_application(self): 12 self.load('variables') 13 14 body = 'Test body string.' 15 16 resp = self.post( 17 headers={ 18 'Host': 'localhost', 19 'Content-Type': 'text/html', 20 'Custom-Header': 'blah', 21 'Connection': 'close', 22 }, 23 body=body, 24 ) 25 26 assert resp['status'] == 200, 'status' 27 headers = resp['headers'] 28 header_server = headers.pop('Server') 29 assert re.search(r'Unit/[\d\.]+', header_server), 'server header' 30 assert ( 31 headers.pop('Server-Software') == header_server 32 ), 'server software header' 33 34 date = headers.pop('Date') 35 assert date[-4:] == ' GMT', 'date header timezone' 36 assert ( 37 abs(self.date_to_sec_epoch(date) - self.sec_epoch()) < 5 38 ), 'date header' 39 40 assert headers == { 41 'Connection': 'close', 42 'Content-Length': str(len(body)), 43 'Content-Type': 'text/html', 44 'Request-Method': 'POST', 45 'Request-Uri': '/', 46 'Http-Host': 'localhost', 47 'Script-Name': 'config.ru', 48 'Server-Protocol': 'HTTP/1.1', 49 'Custom-Header': 'blah', 50 'Rack-Version': '13', 51 'Rack-Url-Scheme': 'http', 52 'Rack-Multithread': 'false', 53 'Rack-Multiprocess': 'true', 54 'Rack-Run-Once': 'false', 55 'Rack-Hijack-Q': 'false', 56 'Rack-Hijack': '', 57 'Rack-Hijack-IO': '', 58 }, 'headers' 59 assert resp['body'] == body, 'body' 60 61 def test_ruby_application_query_string(self): 62 self.load('query_string') 63 64 resp = self.get(url='/?var1=val1&var2=val2') 65 66 assert ( 67 resp['headers']['Query-String'] == 'var1=val1&var2=val2' 68 ), 'Query-String header' 69 70 def test_ruby_application_query_string_empty(self): 71 self.load('query_string') 72 73 resp = self.get(url='/?') 74 75 assert resp['status'] == 200, 'query string empty status' 76 assert resp['headers']['Query-String'] == '', 'query string empty' 77 78 def test_ruby_application_query_string_absent(self): 79 self.load('query_string') 80 81 resp = self.get() 82 83 assert resp['status'] == 200, 'query string absent status' 84 assert resp['headers']['Query-String'] == '', 'query string absent' 85 86 @pytest.mark.skip('not yet') 87 def test_ruby_application_server_port(self): 88 self.load('server_port') 89 90 assert ( 91 self.get()['headers']['Server-Port'] == '7080' 92 ), 'Server-Port header' 93 94 def test_ruby_application_status_int(self): 95 self.load('status_int') 96 97 assert self.get()['status'] == 200, 'status int' 98 99 def test_ruby_application_input_read_empty(self): 100 self.load('input_read_empty') 101 102 assert self.get()['body'] == '', 'read empty' 103 104 def test_ruby_application_input_read_parts(self): 105 self.load('input_read_parts') 106 107 assert ( 108 self.post(body='0123456789')['body'] == '012345678' 109 ), 'input read parts' 110 111 def test_ruby_application_input_read_buffer(self): 112 self.load('input_read_buffer') 113 114 assert ( 115 self.post(body='0123456789')['body'] == '0123456789' 116 ), 'input read buffer' 117 118 def test_ruby_application_input_read_buffer_not_empty(self): 119 self.load('input_read_buffer_not_empty') 120 121 assert ( 122 self.post(body='0123456789')['body'] == '0123456789' 123 ), 'input read buffer not empty' 124 125 def test_ruby_application_input_gets(self): 126 self.load('input_gets') 127 128 body = '0123456789' 129 130 assert self.post(body=body)['body'] == body, 'input gets' 131 132 def test_ruby_application_input_gets_2(self): 133 self.load('input_gets') 134 135 assert ( 136 self.post(body='01234\n56789\n')['body'] == '01234\n' 137 ), 'input gets 2' 138 139 def test_ruby_application_input_gets_all(self): 140 self.load('input_gets_all') 141 142 body = '\n01234\n56789\n\n' 143 144 assert self.post(body=body)['body'] == body, 'input gets all' 145 146 def test_ruby_application_input_each(self): 147 self.load('input_each') 148 149 body = '\n01234\n56789\n\n' 150 151 assert self.post(body=body)['body'] == body, 'input each' 152 153 @pytest.mark.skip('not yet') 154 def test_ruby_application_input_rewind(self): 155 self.load('input_rewind') 156 157 body = '0123456789' 158 159 assert self.post(body=body)['body'] == body, 'input rewind' 160 161 @pytest.mark.skip('not yet') 162 def test_ruby_application_syntax_error(self, skip_alert): 163 skip_alert( 164 r'Failed to parse rack script', 165 r'syntax error', 166 r'new_from_string', 167 r'parse_file', 168 ) 169 self.load('syntax_error') 170 171 assert self.get()['status'] == 500, 'syntax error' 172 173 def test_ruby_application_errors_puts(self): 174 self.load('errors_puts') 175 176 assert self.get()['status'] == 200 177 178 assert ( 179 self.wait_for_record(r'\[error\].+Error in application') is not None 180 ), 'errors puts' 181 182 def test_ruby_application_errors_puts_int(self): 183 self.load('errors_puts_int') 184 185 assert self.get()['status'] == 200 186 187 assert ( 188 self.wait_for_record(r'\[error\].+1234567890') is not None 189 ), 'errors puts int' 190 191 def test_ruby_application_errors_write(self): 192 self.load('errors_write') 193 194 assert self.get()['status'] == 200 195 assert ( 196 self.wait_for_record(r'\[error\].+Error in application') is not None 197 ), 'errors write' 198 199 def test_ruby_application_errors_write_to_s_custom(self): 200 self.load('errors_write_to_s_custom') 201 202 assert self.get()['status'] == 200, 'errors write to_s custom' 203 204 def test_ruby_application_errors_write_int(self): 205 self.load('errors_write_int') 206 207 assert self.get()['status'] == 200 208 assert ( 209 self.wait_for_record(r'\[error\].+1234567890') is not None 210 ), 'errors write int' 211 212 def test_ruby_application_at_exit(self): 213 self.load('at_exit') 214 215 assert self.get()['status'] == 200 216 217 assert 'success' in self.conf({"listeners": {}, "applications": {}}) 218 219 assert ( 220 self.wait_for_record(r'\[error\].+At exit called\.') is not None 221 ), 'at exit' 222 223 def test_ruby_application_encoding(self): 224 self.load('encoding') 225 226 try: 227 locales = ( 228 subprocess.check_output( 229 ['locale', '-a'], 230 stderr=subprocess.STDOUT, 231 ) 232 .decode() 233 .split('\n') 234 ) 235 236 except (FileNotFoundError, subprocess.CalledProcessError): 237 pytest.skip('require locale') 238 239 def get_locale(pattern): 240 return next( 241 ( 242 l 243 for l in locales 244 if re.match(pattern, l.upper()) is not None 245 ), 246 None, 247 ) 248 249 utf8 = get_locale(r'.*UTF[-_]?8') 250 iso88591 = get_locale(r'.*ISO[-_]?8859[-_]?1') 251 252 def check_locale(enc): 253 assert 'success' in self.conf( 254 {"LC_CTYPE": enc, "LC_ALL": ""}, 255 '/config/applications/encoding/environment', 256 ) 257 258 resp = self.get() 259 assert resp['status'] == 200, 'status' 260 261 enc_default = re.sub(r'[-_]', '', resp['headers']['X-Enc']).upper() 262 assert ( 263 enc_default == re.sub(r'[-_]', '', enc.split('.')[-1]).upper() 264 ) 265 266 if utf8: 267 check_locale(utf8) 268 269 if iso88591: 270 check_locale(iso88591) 271 272 if not utf8 and not iso88591: 273 pytest.skip('no available locales') 274 275 def test_ruby_application_header_custom(self): 276 self.load('header_custom') 277 278 resp = self.post(body="\ntc=one,two\ntc=three,four,\n\n") 279 280 assert resp['headers']['Custom-Header'] == [ 281 '', 282 'tc=one,two', 283 'tc=three,four,', 284 '', 285 '', 286 ], 'header custom' 287 288 @pytest.mark.skip('not yet') 289 def test_ruby_application_header_custom_non_printable(self): 290 self.load('header_custom') 291 292 assert ( 293 self.post(body='\b')['status'] == 500 294 ), 'header custom non printable' 295 296 def test_ruby_application_header_status(self): 297 self.load('header_status') 298 299 assert self.get()['status'] == 200, 'header status' 300 301 @pytest.mark.skip('not yet') 302 def test_ruby_application_header_rack(self): 303 self.load('header_rack') 304 305 assert self.get()['status'] == 500, 'header rack' 306 307 def test_ruby_application_body_empty(self): 308 self.load('body_empty') 309 310 assert self.get()['body'] == '', 'body empty' 311 312 def test_ruby_application_body_array(self): 313 self.load('body_array') 314 315 assert self.get()['body'] == '0123456789', 'body array' 316 317 def test_ruby_application_body_large(self): 318 self.load('mirror') 319 320 body = '0123456789' * 1000 321 322 assert self.post(body=body)['body'] == body, 'body large' 323 324 @pytest.mark.skip('not yet') 325 def test_ruby_application_body_each_error(self): 326 self.load('body_each_error') 327 328 assert self.get()['status'] == 500, 'body each error status' 329 330 assert ( 331 self.wait_for_record(r'\[error\].+Failed to run ruby script') 332 is not None 333 ), 'body each error' 334 335 def test_ruby_application_body_file(self): 336 self.load('body_file') 337 338 assert self.get()['body'] == 'body\n', 'body file' 339 340 def test_ruby_keepalive_body(self): 341 self.load('mirror') 342 343 assert self.get()['status'] == 200, 'init' 344 345 body = '0123456789' * 500 346 (resp, sock) = self.post( 347 headers={ 348 'Host': 'localhost', 349 'Connection': 'keep-alive', 350 'Content-Type': 'text/html', 351 }, 352 start=True, 353 body=body, 354 read_timeout=1, 355 ) 356 357 assert resp['body'] == body, 'keep-alive 1' 358 359 body = '0123456789' 360 resp = self.post( 361 headers={ 362 'Host': 'localhost', 363 'Connection': 'close', 364 'Content-Type': 'text/html', 365 }, 366 sock=sock, 367 body=body, 368 ) 369 370 assert resp['body'] == body, 'keep-alive 2' 371 372 def test_ruby_application_constants(self): 373 self.load('constants') 374 375 resp = self.get() 376 377 assert resp['status'] == 200, 'status' 378 379 headers = resp['headers'] 380 assert len(headers['X-Copyright']) > 0, 'RUBY_COPYRIGHT' 381 assert len(headers['X-Description']) > 0, 'RUBY_DESCRIPTION' 382 assert len(headers['X-Engine']) > 0, 'RUBY_ENGINE' 383 assert len(headers['X-Engine-Version']) > 0, 'RUBY_ENGINE_VERSION' 384 assert len(headers['X-Patchlevel']) > 0, 'RUBY_PATCHLEVEL' 385 assert len(headers['X-Platform']) > 0, 'RUBY_PLATFORM' 386 assert len(headers['X-Release-Date']) > 0, 'RUBY_RELEASE_DATE' 387 assert len(headers['X-Revision']) > 0, 'RUBY_REVISION' 388 assert len(headers['X-Version']) > 0, 'RUBY_VERSION' 389 390 def test_ruby_application_threads(self): 391 self.load('threads') 392 393 assert 'success' in self.conf( 394 '4', 'applications/threads/threads' 395 ), 'configure 4 threads' 396 397 socks = [] 398 399 for i in range(4): 400 (_, sock) = self.get( 401 headers={ 402 'Host': 'localhost', 403 'X-Delay': '2', 404 'Connection': 'close', 405 }, 406 no_recv=True, 407 start=True, 408 ) 409 410 socks.append(sock) 411 412 threads = set() 413 414 for sock in socks: 415 resp = self.recvall(sock).decode('utf-8') 416 417 self.log_in(resp) 418 419 resp = self._resp_to_dict(resp) 420 421 assert resp['status'] == 200, 'status' 422 423 threads.add(resp['headers']['X-Thread']) 424 425 assert resp['headers']['Rack-Multithread'] == 'true', 'multithread' 426 427 sock.close() 428 429 assert len(socks) == len(threads), 'threads differs' 430