diff --git a/src/HttpLibrary/__init__.py b/src/HttpLibrary/__init__.py index ae11328..82d7f84 100644 --- a/src/HttpLibrary/__init__.py +++ b/src/HttpLibrary/__init__.py @@ -138,6 +138,9 @@ def response(self): def _path_from_url_or_path(self, url_or_path): + if url_or_path.startswith("\"") and url_or_path.endswith("\""): + url_or_path = url_or_path[1:-1] + if url_or_path.startswith("/"): return url_or_path @@ -267,6 +270,25 @@ def PUT(self, url): self.context.request_headers, **kwargs) ) + def PATCH(self, url): + """ + Issues an HTTP PATCH request. + + `url` is the URL relative to the server root, e.g. '/_utils/config.html' + """ + path = self._path_from_url_or_path(url) + kwargs = {} + if 'Content-Type' in self.context.request_headers: + kwargs[ + 'content_type'] = self.context.request_headers['Content-Type'] + self.context.pre_process_request() + logger.debug("Performing PATCH request on %s://%s%s" % ( + self.context._scheme, self.app.host, url)) + self.context.post_process_request( + self.app.patch(path, self.context.request_body or {}, + self.context.request_headers, **kwargs) + ) + def DELETE(self, url): """ Issues a HTTP DELETE request. @@ -281,6 +303,20 @@ def DELETE(self, url): self.app.delete(path, {}, self.context.request_headers) ) + def OPTIONS(self, url): + """ + Issues a HTTP OPTIONS request. + + `url` is the URL relative to the server root, e.g. '/_utils/config.html' + """ + path = self._path_from_url_or_path(url) + self.context.pre_process_request() + logger.debug("Performing OPTIONS request on %s://%s%s" % ( + self.context._scheme, self.app.host, path,)) + self.context.post_process_request( + self.app.options(path, self.context.request_headers) + ) + def follow_response(self): """ Follows a HTTP redirect if the previous response status code was a 301 or 302. diff --git a/tests/http/mockserver.py b/tests/http/mockserver.py index fe2bb8e..bff6ca7 100755 --- a/tests/http/mockserver.py +++ b/tests/http/mockserver.py @@ -22,9 +22,32 @@ def do_DELETE(self): self.finish() def do_PATCH(self): - self.send_response(200, "Patch request ok") + if self.path == '/echo': + data = self.rfile.read(int(self.headers['Content-Length'])) + self.rfile.close() + self.send_response(200, "OK") + self.send_header('Content-Type', 'text/plain; charset=utf-8') + self.end_headers() + self.wfile.write(data) + self.finish() + elif self.path == '/content_type': + self.send_response(200, "OK") + self.wfile.write(self.rfile.read) + self.end_headers() + self.wfile.write(self.headers['Content-Type']) + self.finish() + elif self.path == '/kill': + global server + self.send_response(201, "Killing myself") + server.socket.close() + sys.exit(0) + else: + self.send_error(500) + + def do_OPTIONS(self): + self.send_response(200, "Options request ok") self.end_headers() - self.wfile.write("Got a patch request") + self.wfile.write("Got an OPTIONS request") self.finish() def do_GET(self): diff --git a/tests/http/simple.txt b/tests/http/simple.txt index 827950a..3c97e6b 100644 --- a/tests/http/simple.txt +++ b/tests/http/simple.txt @@ -102,8 +102,8 @@ Next Request Should Have Status Code w/ Status Line FAIL ... GET /418 HTTP Request with custom HTTP verb should work - HTTP Request PATCH /patch - Response Body Should Contain Got a patch request + HTTP Request OPTIONS /options + Response Body Should Contain Got an OPTIONS request Response Should Have Header OK GET /302 @@ -174,6 +174,16 @@ PUT with two word request body PUT /echo Response Body Should Contain Tooot Tooooot +PATCH with two word request body + Set Request Body Tooot Tooooot + Set Request Header Content-Type text/plain + PATCH /echo + Response Body Should Contain Tooot Tooooot + +Simple OPTIONS request + OPTIONS /options + Response Body Should Contain Got an OPTIONS request + Get Response Status GET /200 ${status}= Get Response Status