def do_request(self, command=None, path=None, request_version=None, headers=None, rfile=None, wfile=None): if command is None: command = self.command if path is None: path = self.path if request_version is None: request_version = self.request_version if headers is None: headers = self.headers if rfile is None: rfile = self.rfile if wfile is None: wfile = self.wfile req = HTTPRequest(self.client_address, command, path, request_version, headers, rfile) del req.headers["Connection"] req.headers["Connection"] = "Close" res = self.server.handler(req) del res.headers["Connection"] res.headers["Connection"] = "Close" inp = res.get_input() while True: c = inp.read(settings.CHUNK_SIZE) if len(c) == 0: inp.close() break wfile.write(c)
def test_get_body(self): """ Tests the get_body() method of the HTTPRequest class. """ req = HTTPRequest("127.0.0.1", "GET", "http://www.example.com/path/here.qqq?a=1&b=2&c=3", "HTTP/1.1", {"Argyle-Fishsticks": "Procrastinating"}) self.assertEqual(req.body.length, 0) self.assertEqual(req.body.read(10), b"") self.assertEqual(req.body.length, 0) req = HTTPRequest("127.0.0.1", "POST", "http://www.example.com/path/here.qqq?a=1&b=2&c=3", "HTTP/1.1", {"Argyle-Fishsticks": "Procrastinating"}, content=io.BytesIO(b"POST body.")) self.assertEqual(req.body.length, 10) self.assertEqual(req.get_input(head=False).read(32), b"POST body.") self.assertEqual(req.body.length, 10)
def test_get_status_line(self): """ Tests the get_status_line() method of the HTTPRequest class. """ req = HTTPRequest("127.0.0.1", "GET", "http://www.example.com/path/here.qqq?a=1&b=2&c=3", "HTTP/1.1", {"Argyle-Fishsticks": "Procrastinating"}) self.assertEqual(req.get_status_line(), b"GET /path/here.qqq?a=1&b=2&c=3 HTTP/1.1") self.assertEqual(req.get_status_line(proxy=True), b"GET " b"http://www.example.com/path/here.qqq?a=1&b=2&c=3 " b"HTTP/1.1") req = HTTPRequest("127.0.0.1", "POST", "http://www.example.com/path/here.qqq?a=1&b=2&c=3", "HTTP/1.0", {"Argyle-Fishsticks": "Procrastinating", "Content-Length": "0"}) self.assertEqual(req.get_status_line(), b"POST /path/here.qqq?a=1&b=2&c=3 HTTP/1.0") self.assertEqual(req.get_status_line(proxy=True), b"POST " b"http://www.example.com/path/here.qqq?a=1&b=2&c=3 " b"HTTP/1.0")