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