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_illegal_request(self): server = HttpServer(hello_app) server.listen(('localhost', 0)) addr = server.addresses[0] client = StreamClient() client.connect(addr) client.write(b'foo\r\n') client.write_eof() buf = client.read() self.assertEqual(buf, b'') 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()
# HTTP client and server example. from gruvi.http import HttpClient, HttpServer def handler(env, start_response): headers = [('Content-Type', 'text/plain; charset=UTF-8')] status = '200 OK' body = 'pong' start_response(status, headers) yield body.encode('utf-8') server = HttpServer(handler) server.listen(('localhost', 0)) addr = server.addresses[0] client = HttpClient() client.connect(addr) client.request('GET', '/ping') resp = client.getresponse() assert resp.status_code == 200 body = resp.body.read() print('result = {}'.format(body.decode(resp.charset)))
# Example: hello world web server from __future__ import print_function import logging import argparse from gruvi import get_hub from gruvi.http import HttpServer logging.basicConfig() parser = argparse.ArgumentParser() parser.add_argument('hostname'); parser.add_argument('port', type=int); args = parser.parse_args() def hello_app(environ, start_response): headers = [('Content-Type', 'text/plain')] start_response('200 OK', headers) return [b'Hello, world!'] server = HttpServer(hello_app) server.listen((args.hostname, args.port)) hub = get_hub() print('Press CTRL-C to exit') hub.switch(interrupt=True)
# Example: hello world web server from __future__ import print_function import logging import argparse from gruvi import get_hub from gruvi.http import HttpServer logging.basicConfig() parser = argparse.ArgumentParser() parser.add_argument('hostname') parser.add_argument('port', type=int) args = parser.parse_args() def hello_app(environ, start_response): headers = [('Content-Type', 'text/plain')] start_response('200 OK', headers) return [b'Hello, world!'] server = HttpServer(hello_app) server.listen((args.hostname, args.port)) hub = get_hub() print('Press CTRL-C to exit') hub.switch(interrupt=True)