-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring, improved http structure and implementation
- Loading branch information
Showing
13 changed files
with
194 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
class HttpResponse(object): | ||
"""Class representing a http response""" | ||
|
||
def __init__(self, request, status_code, headers, body): | ||
"""Initializes the http response object | ||
Args: | ||
request (HttpRequest): | ||
The request that produces this response | ||
status_code (int): | ||
The status code as a number | ||
headers (dict): | ||
The response headers | ||
body (bytes or None): | ||
The body of the response, if any | ||
""" | ||
self.request = request | ||
self.status_code = status_code | ||
self.headers = headers | ||
if isinstance(body, str): | ||
body = body.encode() | ||
if not isinstance(body, bytes): | ||
raise TypeError( | ||
'body must be in bytes, not {}'.format(type(body).__name__)) | ||
self.body = body | ||
|
||
@property | ||
def body_str(self): | ||
"""Returns the decoded content of the request, if possible. | ||
May raise UnicodeDecodeError if the body does not represent a valid | ||
unicode encoded sequence. | ||
""" | ||
return self.body.decode() | ||
|
||
@property | ||
def body_bytes(self): | ||
return self.body | ||
|
||
@property | ||
def server(self): | ||
"""Returns the server header if available""" | ||
return self.headers.get('Server') | ||
|
||
@property | ||
def content_type(self): | ||
"""Returns the response content type if available""" | ||
return self.headers.get('Content-Type') | ||
|
||
def is_success(self): | ||
return self.status_code < 400 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
-r test.txt | ||
-e . | ||
bumpversion | ||
twine | ||
sphinx | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,37 @@ | ||
import pytest | ||
|
||
from crawlster.helpers.request.request import HttpRequest | ||
from crawlster.helpers.http.request import ( | ||
HttpRequest, GetRequest, PostRequest, XhrRequest, JsonRequest) | ||
|
||
|
||
def test_request_invalid_method(): | ||
with pytest.raises(ValueError): | ||
HttpRequest(method='invalid', url="http://example.com") | ||
|
||
|
||
def test_request_invalid_url(): | ||
with pytest.raises(ValueError): | ||
HttpRequest('invalid_url') | ||
|
||
|
||
# noinspection PyTypeChecker | ||
def test_request_invalid_headers(): | ||
with pytest.raises(TypeError): | ||
HttpRequest('http://localhost', headers='invalid type') | ||
|
||
|
||
@pytest.mark.parametrize('obj_type, init_args, expected_attrs', [ | ||
(GetRequest, ('http://localhost',), {'method': 'GET'}), | ||
(PostRequest, ('http://localhost',), {'method': 'POST'}), | ||
(XhrRequest, ('http://localhost',), | ||
{'method': 'GET', 'headers': {'X-Requested-With': 'XMLHttpRequest'}}), | ||
(JsonRequest, ('http://localhost', 'POST', {'hello': 'world'}), | ||
{ | ||
'content_type': 'application/json', | ||
'data': '{"hello":"world"}' | ||
}) | ||
]) | ||
def test_request_initialisation(obj_type, init_args, expected_attrs): | ||
instance = obj_type(*init_args) | ||
for k, v in expected_attrs.items(): | ||
assert getattr(instance, k) == v |
Oops, something went wrong.