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