def test_simple_pipe(self): server = HttpServer(hello_app) server.listen(self.pipename()) addr = server.addresses[0] client = HttpClient() client.connect(addr) client.request('GET', '/') resp = client.getresponse() self.assertEqual(resp.body.read(), b'Hello!') server.close() client.close()
def test_simple_ssl(self): server = HttpServer(hello_app) context = self.get_ssl_context() server.listen(('localhost', 0), ssl=context) addr = server.addresses[0] client = HttpClient() client.connect(addr, ssl=context) client.request('GET', '/') resp = client.getresponse() self.assertEqual(resp.body.read(), b'Hello!') server.close() client.close()
def test_request_body_sequence(self): server = HttpServer(echo_app) server.listen(('localhost', 0)) addr = server.addresses[0] client = HttpClient() client.connect(addr) client.request('POST', '/', body=[b'foo', b'bar']) resp = client.getresponse() body = resp.body.read() self.assertEqual(body, b'foobar') server.close() client.close()
def test_request_headers(self): server = HttpServer(echo_app) server.listen(('localhost', 0)) addr = server.addresses[0] client = HttpClient() client.connect(addr) client.request('GET', '/', headers=[('X-Echo', 'Bar')]) resp = client.getresponse() self.assertEqual(resp.get_header('X-Echo'), 'Bar') body = resp.body.read() self.assertEqual(body, b'') server.close() client.close()
def test_request_body_string(self): # When making a request with a string body, it should be encoded as # ISO-8859-1. server = HttpServer(echo_app) server.listen(('localhost', 0)) addr = server.addresses[0] client = HttpClient() client.connect(addr) client.request('POST', '/', body='foo') response = client.getresponse() body = response.read() self.assertEqual(body, b'foo') server.close() client.close()
def test_simple(self): server = HttpServer(hello_app) server.listen(('localhost', 0)) port = server.transport.getsockname()[1] client = HttpClient() client.connect(('localhost', port)) client.request('GET', '/') response = client.getresponse() assert response.version == (1, 1) assert response.status == 200 server = response.get_header('Server') assert server.startswith('gruvi.http') ctype = response.get_header('Content-Type') assert ctype == 'text/plain' assert response.read() == b'Hello!'
def test_simple(self): server = HttpServer(hello_app) server.listen(('localhost', 0)) addr = server.addresses[0] client = HttpClient() client.connect(addr) client.request('GET', '/') resp = client.getresponse() self.assertEqual(resp.version, '1.1') self.assertEqual(resp.status_code, 200) serv = resp.get_header('Server') self.assertTrue(serv.startswith('gruvi')) ctype = resp.get_header('Content-Type') self.assertEqual(ctype, 'text/plain') self.assertEqual(resp.body.read(), b'Hello!') server.close() client.close()
def perf_server_throughput(self): server = HttpServer(hello_app) server.listen(('localhost', 0)) addr = server.addresses[0] client = HttpClient() client.connect(addr) nrequests = 0 t0 = t1 = time.time() while t1 - t0 < 0.2: client.request('GET', '/') resp = client.getresponse() self.assertEqual(resp.body.read(), b'Hello!') nrequests += 1 t1 = time.time() throughput = nrequests / (t1 - t0) self.add_result(throughput) server.close() client.close()
def test_request_trailers(self): server = HttpServer(echo_app) server.listen(('localhost', 0)) addr = server.addresses[0] client = HttpClient() client.connect(addr) headers = [('X-Echo', 'Bar'), ('Trailer', 'X-Length')] def genbody(): yield b'FooBody' headers.append(('X-Length', '7')) client.request('GET', '/', body=genbody(), headers=headers) resp = client.getresponse() self.assertEqual(resp.get_header('X-Echo'), 'Bar') self.assertEqual(resp.get_header('X-Length'), '7') body = resp.body.read() self.assertEqual(body, b'FooBody') server.close() client.close()
# Example: cURL like URL downloader import sys import logging import argparse from gruvi.http import HttpClient, geturlinfo logging.basicConfig() parser = argparse.ArgumentParser() parser.add_argument('url') args = parser.parse_args() client = HttpClient() host, port, ssl, path = geturlinfo(args.url) client.connect((host, port), ssl=ssl) client.request('GET', path) response = client.getresponse() while True: buf = response.read(4096) if not buf: break print(buf)
# Example: cURL like URL downloader import sys import logging import argparse from gruvi.http import HttpClient, geturlinfo logging.basicConfig() parser = argparse.ArgumentParser() parser.add_argument('url'); args = parser.parse_args() client = HttpClient() host, port, ssl, path = geturlinfo(args.url) client.connect((host, port), ssl=ssl) client.request('GET', path) response = client.getresponse() while True: buf = response.read(4096) if not buf: break print(buf)