1import re 2 3import pytest 4from unit.applications.lang.go import TestApplicationGo 5 6 7class TestGoApplication(TestApplicationGo): 8 prerequisites = {'modules': {'go': 'all'}} 9 10 @pytest.fixture(autouse=True) 11 def setup_method_fixture(self, request, skip_alert): 12 skip_alert(r'\[unit\] close\(\d+\) failed: Bad file descriptor') 13 14 def test_go_application_variables(self): 15 self.load('variables') 16 17 body = 'Test body string.' 18 19 resp = self.post( 20 headers={ 21 'Host': 'localhost', 22 'Content-Type': 'text/html', 23 'Custom-Header': 'blah', 24 'Connection': 'close', 25 }, 26 body=body, 27 ) 28 29 assert resp['status'] == 200, 'status' 30 headers = resp['headers'] 31 header_server = headers.pop('Server') 32 assert re.search(r'Unit/[\d\.]+', header_server), 'server 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 'Content-Length': str(len(body)), 42 'Content-Type': 'text/html', 43 'Request-Method': 'POST', 44 'Request-Uri': '/', 45 'Http-Host': 'localhost', 46 'Server-Protocol': 'HTTP/1.1', 47 'Server-Protocol-Major': '1', 48 'Server-Protocol-Minor': '1', 49 'Custom-Header': 'blah', 50 'Connection': 'close', 51 }, 'headers' 52 assert resp['body'] == body, 'body' 53 54 def test_go_application_get_variables(self): 55 self.load('get_variables') 56 57 resp = self.get(url='/?var1=val1&var2=&var3') 58 assert resp['headers']['X-Var-1'] == 'val1', 'GET variables' 59 assert resp['headers']['X-Var-2'] == '', 'GET variables 2' 60 assert resp['headers']['X-Var-3'] == '', 'GET variables 3' 61 62 def test_go_application_post_variables(self): 63 self.load('post_variables') 64 65 resp = self.post( 66 headers={ 67 'Host': 'localhost', 68 'Content-Type': 'application/x-www-form-urlencoded', 69 'Connection': 'close', 70 }, 71 body='var1=val1&var2=&var3', 72 ) 73 74 assert resp['headers']['X-Var-1'] == 'val1', 'POST variables' 75 assert resp['headers']['X-Var-2'] == '', 'POST variables 2' 76 assert resp['headers']['X-Var-3'] == '', 'POST variables 3' 77 78 def test_go_application_404(self): 79 self.load('404') 80 81 resp = self.get() 82 83 assert resp['status'] == 404, '404 status' 84 assert re.search( 85 r'<title>404 Not Found</title>', resp['body'] 86 ), '404 body' 87 88 def test_go_keepalive_body(self): 89 self.load('mirror') 90 91 assert self.get()['status'] == 200, 'init' 92 93 body = '0123456789' * 500 94 (resp, sock) = self.post( 95 headers={ 96 'Host': 'localhost', 97 'Connection': 'keep-alive', 98 'Content-Type': 'text/html', 99 }, 100 start=True, 101 body=body, 102 read_timeout=1, 103 ) 104 105 assert resp['body'] == body, 'keep-alive 1' 106 107 body = '0123456789' 108 resp = self.post( 109 headers={ 110 'Host': 'localhost', 111 'Content-Type': 'text/html', 112 'Connection': 'close', 113 }, 114 sock=sock, 115 body=body, 116 ) 117 118 assert resp['body'] == body, 'keep-alive 2' 119 120 def test_go_application_cookies(self): 121 self.load('cookies') 122 123 resp = self.get( 124 headers={ 125 'Host': 'localhost', 126 'Cookie': 'var1=val1; var2=val2', 127 'Connection': 'close', 128 } 129 ) 130 131 assert resp['headers']['X-Cookie-1'] == 'val1', 'cookie 1' 132 assert resp['headers']['X-Cookie-2'] == 'val2', 'cookie 2' 133 134 def test_go_application_command_line_arguments_type(self): 135 self.load('command_line_arguments') 136 137 assert 'error' in self.conf( 138 '' "a b c", 'applications/command_line_arguments/arguments' 139 ), 'arguments type' 140 141 def test_go_application_command_line_arguments_0(self): 142 self.load('command_line_arguments') 143 144 assert self.get()['headers']['X-Arg-0'] == self.conf_get( 145 'applications/command_line_arguments/executable' 146 ), 'argument 0' 147 148 def test_go_application_command_line_arguments(self): 149 self.load('command_line_arguments') 150 151 arg1 = '--cc=gcc-7.2.0' 152 arg2 = '--cc-opt=\'-O0 -DNXT_DEBUG_MEMORY=1 -fsanitize=address\'' 153 arg3 = '--debug' 154 155 assert 'success' in self.conf( 156 '["' + arg1 + '", "' + arg2 + '", "' + arg3 + '"]', 157 'applications/command_line_arguments/arguments', 158 ) 159 160 assert self.get()['body'] == arg1 + ',' + arg2 + ',' + arg3, 'arguments' 161 162 def test_go_application_command_line_arguments_change(self): 163 self.load('command_line_arguments') 164 165 args_path = 'applications/command_line_arguments/arguments' 166 167 assert 'success' in self.conf('["0", "a", "$", ""]', args_path) 168 169 assert self.get()['body'] == '0,a,$,', 'arguments' 170 171 assert 'success' in self.conf('["-1", "b", "%"]', args_path) 172 173 assert self.get()['body'] == '-1,b,%', 'arguments change' 174 175 assert 'success' in self.conf('[]', args_path) 176 177 assert self.get()['headers']['Content-Length'] == '0', 'arguments empty' 178