From 901bc98edf1cda510c417b8d2af384e43fca7a14 Mon Sep 17 00:00:00 2001 From: Hubert Tarasiuk Date: Mon, 11 May 2015 12:34:05 +0200 Subject: [PATCH] Support conditional GET in testenv server. * src/exc/server_error.py: Add exception for GET to HEAD fallback. * src/server/http/http_server.py: Do not send body if 304 return code requested for a file. --- testenv/exc/server_error.py | 6 ++++++ testenv/server/http/http_server.py | 7 ++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/testenv/exc/server_error.py b/testenv/exc/server_error.py index dc676a85..fe359f54 100644 --- a/testenv/exc/server_error.py +++ b/testenv/exc/server_error.py @@ -6,6 +6,12 @@ class ServerError (Exception): def __init__(self, err_message): self.err_message = err_message +class NoBodyServerError (Exception): + """ A custom exception which is raised by the test servers. + Used if no body should be sent in response. """ + + def __init__(self, err_message): + self.err_message = err_message class AuthError (ServerError): """ A custom exception raised byt he servers when authentication of the diff --git a/testenv/server/http/http_server.py b/testenv/server/http/http_server.py index 7c0b4727..2356f1c2 100644 --- a/testenv/server/http/http_server.py +++ b/testenv/server/http/http_server.py @@ -1,5 +1,5 @@ from http.server import HTTPServer, BaseHTTPRequestHandler -from exc.server_error import ServerError, AuthError +from exc.server_error import ServerError, AuthError, NoBodyServerError from socketserver import BaseServer from posixpath import basename, splitext from base64 import b64encode @@ -201,6 +201,8 @@ class _Handler(BaseHTTPRequestHandler): def Response(self, resp_obj): self.send_response(resp_obj.response_code) self.finish_headers() + if resp_obj.response_code == 304: + raise NoBodyServerError("Conditional get falling to head") raise ServerError("Custom Response code sent.") def custom_response(self): @@ -401,6 +403,9 @@ class _Handler(BaseHTTPRequestHandler): except AuthError as ae: print(ae.__str__()) return(None, None) + except NoBodyServerError as nbse: + print(nbse.__str__()) + return(None, None) except ServerError as se: print(se.__str__()) return(content, None)