Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added PATCH as main keyword #28

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/HttpLibrary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down
27 changes: 25 additions & 2 deletions tests/http/mockserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
14 changes: 12 additions & 2 deletions tests/http/simple.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down