From d2bf066b1ceabcb951c0c8be3c21b5ca4a833ed6 Mon Sep 17 00:00:00 2001 From: Andrey Zelenkov Date: Wed, 13 Dec 2017 15:23:32 +0300 Subject: Tests: http() function introduced. --- test/unit.py | 46 ++++++++++++++-------------------------------- 1 file changed, 14 insertions(+), 32 deletions(-) diff --git a/test/unit.py b/test/unit.py index 0857272e..0860d134 100644 --- a/test/unit.py +++ b/test/unit.py @@ -110,39 +110,32 @@ class TestUnitControl(TestUnit): # TODO socket reuse # TODO http client - def get(self, path='/'): + def http(self, req): with self._control_sock() as sock: - req = ('GET ' + path - + ' HTTP/1.1\r\nHost: localhost\r\n\r\n').encode() - sock.sendall(req) if '--verbose' in sys.argv: - print('>>>\n', req) + print('>>>', req, sep='\n') resp = self._recvall(sock) if '--verbose' in sys.argv: - print('<<<\n', resp) - - return self._body_json(resp) + print('<<<', resp, sep='\n') - def delete(self, path='/'): + return resp - with self._control_sock() as sock: - req = ('DELETE ' + path - + ' HTTP/1.1\r\nHost: localhost\r\n\r\n').encode() + def get(self, path='/'): - sock.sendall(req) + resp = self.http(('GET ' + path + + ' HTTP/1.1\r\nHost: localhost\r\n\r\n').encode()) - if '--verbose' in sys.argv: - print('>>>\n', req) + return self._body_json(resp) - resp = self._recvall(sock) + def delete(self, path='/'): - if '--verbose' in sys.argv: - print('<<<\n', resp) + resp = self.http(('DELETE ' + path + + ' HTTP/1.1\r\nHost: localhost\r\n\r\n').encode()) return self._body_json(resp) @@ -151,20 +144,9 @@ class TestUnitControl(TestUnit): if isinstance(data, str): data = data.encode() - with self._control_sock() as sock: - req = ('PUT ' + path + ' HTTP/1.1\nHost: localhost\n' - + 'Content-Length: ' + str(len(data)) - + '\r\n\r\n').encode() + data - - sock.sendall(req) - - if '--verbose' in sys.argv: - print('>>>\n', req) - - resp = self._recvall(sock) - - if '--verbose' in sys.argv: - print('<<<\n', resp) + resp = self.http(('PUT ' + path + ' HTTP/1.1\nHost: localhost\n' + + 'Content-Length: ' + str(len(data)) + + '\r\n\r\n').encode() + data) return self._body_json(resp) -- cgit