From 3c06608acd876f85f3009e5831b895218c63e58d Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 19 May 2015 21:34:26 +0530 Subject: [PATCH] Tests for POST --- src/calibre/srv/tests/http.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/calibre/srv/tests/http.py b/src/calibre/srv/tests/http.py index 278d53d394..7368cef0dc 100644 --- a/src/calibre/srv/tests/http.py +++ b/src/calibre/srv/tests/http.py @@ -6,7 +6,7 @@ from __future__ import (unicode_literals, division, absolute_import, __license__ = 'GPL v3' __copyright__ = '2015, Kovid Goyal ' -import textwrap, httplib +import textwrap, httplib, socket from io import BytesIO from calibre.srv.tests.base import BaseTest, TestServer @@ -80,11 +80,24 @@ class TestHTTP(BaseTest): self.ae(r.status, httplib.NOT_FOUND) self.ae(r.read(), 'Requested resource not found') - server.change_handler(lambda conn:conn.path[1]) + server.change_handler(lambda conn:conn.path[1] + conn.input_reader.read().decode('ascii')) # Test simple GET conn.request('GET', '/test') self.ae(conn.getresponse().read(), 'test') + # Test POST with simple body + conn.request('POST', '/test', 'body') + r = conn.getresponse() + self.ae(r.status, httplib.CREATED) + self.ae(r.read(), 'testbody') + + # Test POST with chunked transfer encoding + conn.request('POST', '/test', headers={'Transfer-Encoding': 'chunked'}) + conn.send(b'4\r\nbody\r\n0\r\n\r\n') + r = conn.getresponse() + self.ae(r.status, httplib.CREATED) + self.ae(r.read(), 'testbody') + # Test pipelining responses = [] for i in xrange(10): @@ -97,4 +110,12 @@ class TestHTTP(BaseTest): self.ae(r.read(), ('%d' % i).encode('ascii')) conn._HTTPConnection__state = httplib._CS_IDLE + # Test closing + conn.request('GET', '/close', headers={'Connection':'close'}) + r = conn.getresponse() + self.ae(r.status, 200), self.ae(r.read(), 'close') + conn.request('HEAD', '/close') + with self.assertRaises(socket.error): + conn.sock.send(b'xxx') + # }}}