Tests for a basic GET and HTTP pipelining

This commit is contained in:
Kovid Goyal 2015-05-19 17:38:23 +05:30
parent eee18f4d76
commit 8045fec313
2 changed files with 23 additions and 0 deletions

View File

@ -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)

View File

@ -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
# }}}