From 8045fec3134e5184354a37226987aa452db50398 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 19 May 2015 17:38:23 +0530 Subject: [PATCH] Tests for a basic GET and HTTP pipelining --- src/calibre/srv/tests/base.py | 4 ++++ src/calibre/srv/tests/http.py | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/calibre/srv/tests/base.py b/src/calibre/srv/tests/base.py index 4b3dd6e3b3..292f68757f 100644 --- a/src/calibre/srv/tests/base.py +++ b/src/calibre/srv/tests/base.py @@ -61,3 +61,7 @@ class TestServer(Thread): def connect(self): return httplib.HTTPConnection(self.address[0], self.address[1], strict=True, timeout=0.1) + + def change_handler(self, handler): + from calibre.srv.http import create_http_handler + self.loop.http_handler = create_http_handler(handler) diff --git a/src/calibre/srv/tests/http.py b/src/calibre/srv/tests/http.py index 25e962df98..278d53d394 100644 --- a/src/calibre/srv/tests/http.py +++ b/src/calibre/srv/tests/http.py @@ -65,6 +65,7 @@ class TestHTTP(BaseTest): def handler(conn): raise HTTP404(body) with TestServer(handler) as server: + # Test 404 conn = server.connect() conn.request('HEAD', '/moose') r = conn.getresponse() @@ -78,4 +79,22 @@ class TestHTTP(BaseTest): r = conn.getresponse() self.ae(r.status, httplib.NOT_FOUND) self.ae(r.read(), 'Requested resource not found') + + server.change_handler(lambda conn:conn.path[1]) + # Test simple GET + conn.request('GET', '/test') + self.ae(conn.getresponse().read(), 'test') + + # Test pipelining + responses = [] + for i in xrange(10): + conn._HTTPConnection__state = httplib._CS_IDLE + conn.request('GET', '/%d'%i) + responses.append(conn.response_class(conn.sock, strict=conn.strict, method=conn._method)) + for i in xrange(10): + r = responses[i] + r.begin() + self.ae(r.read(), ('%d' % i).encode('ascii')) + conn._HTTPConnection__state = httplib._CS_IDLE + # }}}