http.py (1477:b93d1acf81bd) http.py (1596:b7e2d4d92624)
1import binascii
2import io
3import json
4import os
1import binascii
2import io
3import json
4import os
5import pytest
5import re
6import select
7import socket
8import time
9
10from unit.main import TestUnit
6import re
7import select
8import socket
9import time
10
11from unit.main import TestUnit
12from conftest import option
11
12
13class TestHTTP(TestUnit):
14 def http(self, start_str, **kwargs):
15 sock_type = (
16 'ipv4' if 'sock_type' not in kwargs else kwargs['sock_type']
17 )
18 port = 7080 if 'port' not in kwargs else kwargs['port']

--- 32 unchanged lines hidden (view full) ---

51 if 'wrapper' in kwargs:
52 sock = kwargs['wrapper'](sock)
53
54 connect_args = addr if sock_type == 'unix' else (addr, port)
55 try:
56 sock.connect(connect_args)
57 except ConnectionRefusedError:
58 sock.close()
13
14
15class TestHTTP(TestUnit):
16 def http(self, start_str, **kwargs):
17 sock_type = (
18 'ipv4' if 'sock_type' not in kwargs else kwargs['sock_type']
19 )
20 port = 7080 if 'port' not in kwargs else kwargs['port']

--- 32 unchanged lines hidden (view full) ---

53 if 'wrapper' in kwargs:
54 sock = kwargs['wrapper'](sock)
55
56 connect_args = addr if sock_type == 'unix' else (addr, port)
57 try:
58 sock.connect(connect_args)
59 except ConnectionRefusedError:
60 sock.close()
59 self.fail('Client can\'t connect to the server.')
61 pytest.fail('Client can\'t connect to the server.')
60
61 else:
62 sock = kwargs['sock']
63
64 if 'raw' not in kwargs:
65 req = ' '.join([start_str, url, http]) + crlf
66
67 if body != b'':

--- 55 unchanged lines hidden (view full) ---

123
124 if 'start' not in kwargs:
125 sock.close()
126 return resp
127
128 return (resp, sock)
129
130 def log_out(self, log, encoding):
62
63 else:
64 sock = kwargs['sock']
65
66 if 'raw' not in kwargs:
67 req = ' '.join([start_str, url, http]) + crlf
68
69 if body != b'':

--- 55 unchanged lines hidden (view full) ---

125
126 if 'start' not in kwargs:
127 sock.close()
128 return resp
129
130 return (resp, sock)
131
132 def log_out(self, log, encoding):
131 if TestUnit.detailed:
133 if option.detailed:
132 print('>>>')
133 log = self.log_truncate(log)
134 try:
135 print(log.decode(encoding, 'ignore'))
136 except UnicodeEncodeError:
137 print(log)
138
139 def log_in(self, log):
134 print('>>>')
135 log = self.log_truncate(log)
136 try:
137 print(log.decode(encoding, 'ignore'))
138 except UnicodeEncodeError:
139 print(log)
140
141 def log_in(self, log):
140 if TestUnit.detailed:
142 if option.detailed:
141 print('<<<')
142 log = self.log_truncate(log)
143 try:
144 print(log)
145 except UnicodeEncodeError:
146 print(log.encode())
147
148 def log_truncate(self, log, limit=1024):

--- 36 unchanged lines hidden (view full) ---

185
186 data = b''
187 while True:
188 rlist = select.select([sock], [], [], timeout)[0]
189 if not rlist:
190 # For all current cases if the "read_timeout" was changed
191 # than test do not expect to get a response from server.
192 if timeout == timeout_default:
143 print('<<<')
144 log = self.log_truncate(log)
145 try:
146 print(log)
147 except UnicodeEncodeError:
148 print(log.encode())
149
150 def log_truncate(self, log, limit=1024):

--- 36 unchanged lines hidden (view full) ---

187
188 data = b''
189 while True:
190 rlist = select.select([sock], [], [], timeout)[0]
191 if not rlist:
192 # For all current cases if the "read_timeout" was changed
193 # than test do not expect to get a response from server.
194 if timeout == timeout_default:
193 self.fail('Can\'t read response from server.')
195 pytest.fail('Can\'t read response from server.')
194 break
195
196 try:
197 part = sock.recv(buff_size)
198 except:
199 break
200
201 data += part

--- 36 unchanged lines hidden (view full) ---

238 def _parse_chunked_body(self, raw_body):
239 if isinstance(raw_body, str):
240 raw_body = bytes(raw_body.encode())
241
242 crlf = b'\r\n'
243 chunks = raw_body.split(crlf)
244
245 if len(chunks) < 3:
196 break
197
198 try:
199 part = sock.recv(buff_size)
200 except:
201 break
202
203 data += part

--- 36 unchanged lines hidden (view full) ---

240 def _parse_chunked_body(self, raw_body):
241 if isinstance(raw_body, str):
242 raw_body = bytes(raw_body.encode())
243
244 crlf = b'\r\n'
245 chunks = raw_body.split(crlf)
246
247 if len(chunks) < 3:
246 self.fail('Invalid chunked body')
248 pytest.fail('Invalid chunked body')
247
248 if chunks.pop() != b'':
249
250 if chunks.pop() != b'':
249 self.fail('No CRLF at the end of the body')
251 pytest.fail('No CRLF at the end of the body')
250
251 try:
252 last_size = int(chunks[-2], 16)
253 except:
252
253 try:
254 last_size = int(chunks[-2], 16)
255 except:
254 self.fail('Invalid zero size chunk')
256 pytest.fail('Invalid zero size chunk')
255
256 if last_size != 0 or chunks[-1] != b'':
257
258 if last_size != 0 or chunks[-1] != b'':
257 self.fail('Incomplete body')
259 pytest.fail('Incomplete body')
258
259 body = b''
260 while len(chunks) >= 2:
261 try:
262 size = int(chunks.pop(0), 16)
263 except:
260
261 body = b''
262 while len(chunks) >= 2:
263 try:
264 size = int(chunks.pop(0), 16)
265 except:
264 self.fail('Invalid chunk size %s' % str(size))
266 pytest.fail('Invalid chunk size %s' % str(size))
265
266 if size == 0:
267
268 if size == 0:
267 self.assertEqual(len(chunks), 1, 'last zero size')
269 assert len(chunks) == 1, 'last zero size'
268 break
269
270 temp_body = crlf.join(chunks)
271
272 body += temp_body[:size]
273
274 temp_body = temp_body[size + len(crlf) :]
275
276 chunks = temp_body.split(crlf)
277
278 return body
279
280 def _parse_json(self, resp):
281 headers = resp['headers']
282
270 break
271
272 temp_body = crlf.join(chunks)
273
274 body += temp_body[:size]
275
276 temp_body = temp_body[size + len(crlf) :]
277
278 chunks = temp_body.split(crlf)
279
280 return body
281
282 def _parse_json(self, resp):
283 headers = resp['headers']
284
283 self.assertIn('Content-Type', headers)
284 self.assertEqual(headers['Content-Type'], 'application/json')
285 assert 'Content-Type' in headers
286 assert headers['Content-Type'] == 'application/json'
285
286 resp['body'] = json.loads(resp['body'])
287
288 return resp
289
290 def getjson(self, **kwargs):
291 return self.get(json=True, **kwargs)
292

--- 7 unchanged lines hidden (view full) ---

300 ret = True
301 break
302 except:
303 sock.close()
304 time.sleep(0.1)
305
306 sock.close()
307
287
288 resp['body'] = json.loads(resp['body'])
289
290 return resp
291
292 def getjson(self, **kwargs):
293 return self.get(json=True, **kwargs)
294

--- 7 unchanged lines hidden (view full) ---

302 ret = True
303 break
304 except:
305 sock.close()
306 time.sleep(0.1)
307
308 sock.close()
309
308 self.assertTrue(ret, 'socket connected')
310 assert ret, 'socket connected'
309
310 def form_encode(self, fields):
311 is_multipart = False
312
313 for _, value in fields.items():
314 if isinstance(value, dict):
315 is_multipart = True
316 break

--- 23 unchanged lines hidden (view full) ---

340 if isinstance(value, dict):
341 datatype = 'text/plain'
342 filename = value['filename']
343
344 if value.get('type'):
345 datatype = value['type']
346
347 if not isinstance(value['data'], io.IOBase):
311
312 def form_encode(self, fields):
313 is_multipart = False
314
315 for _, value in fields.items():
316 if isinstance(value, dict):
317 is_multipart = True
318 break

--- 23 unchanged lines hidden (view full) ---

342 if isinstance(value, dict):
343 datatype = 'text/plain'
344 filename = value['filename']
345
346 if value.get('type'):
347 datatype = value['type']
348
349 if not isinstance(value['data'], io.IOBase):
348 self.fail('multipart encoding of file requires a stream.')
350 pytest.fail('multipart encoding of file requires a stream.')
349
350 data = value['data'].read()
351
352 elif isinstance(value, str):
353 data = value
354
355 else:
351
352 data = value['data'].read()
353
354 elif isinstance(value, str):
355 data = value
356
357 else:
356 self.fail('multipart requires a string or stream data')
358 pytest.fail('multipart requires a string or stream data')
357
358 body += (
359 "--%s\r\nContent-Disposition: form-data; name=\"%s\""
360 ) % (boundary, field)
361
362 if filename != '':
363 body += "; filename=\"%s\"" % filename
364
365 body += "\r\n"
366
367 if datatype != '':
368 body += "Content-Type: %s\r\n" % datatype
369
370 body += "\r\n%s\r\n" % data
371
372 body += "--%s--\r\n" % boundary
373
374 return body.encode(), "multipart/form-data; boundary=%s" % boundary
359
360 body += (
361 "--%s\r\nContent-Disposition: form-data; name=\"%s\""
362 ) % (boundary, field)
363
364 if filename != '':
365 body += "; filename=\"%s\"" % filename
366
367 body += "\r\n"
368
369 if datatype != '':
370 body += "Content-Type: %s\r\n" % datatype
371
372 body += "\r\n%s\r\n" % data
373
374 body += "--%s--\r\n" % boundary
375
376 return body.encode(), "multipart/form-data; boundary=%s" % boundary