Skip to content
This repository has been archived by the owner on May 26, 2021. It is now read-only.

#47 empty headers in admin #48

Open
wants to merge 3 commits into
base: develop
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
18 changes: 15 additions & 3 deletions http_stubs/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
class TestHTTPStubView:
"""Tests representation of the http stubs."""

path = '/default_path/'

@pytest.mark.parametrize('method', HTTPMethod.names())
def test_nonexistent_stub(self, method: str, client):
"""Tests response when stub is not found.
Expand All @@ -33,7 +35,7 @@ def test_exist_not_regexp_stub(
:param client: http client fixture
"""
http_stub_factory(method=method)
response = getattr(client, method.lower())('/default_path/')
response = getattr(client, method.lower())(self.path)

if method != HTTPMethod.HEAD.name:
assert response.content == b'[]'
Expand All @@ -49,7 +51,7 @@ def test_write_log(self, http_stub_factory, client):
"""
content_type = 'text/plain'
http_body = http_stub_factory(method=HTTPMethod.POST.name)
client.post('/default_path/', 'test', content_type=content_type)
client.post(self.path, 'test', content_type=content_type)
log = LogEntry.objects.last()

def _datefmt(date) -> str: # noqa:WPS430
Expand All @@ -67,13 +69,23 @@ def _datefmt(date) -> str: # noqa:WPS430
assert log.headers == {
'Content-Length': '4',
'Content-Type': content_type,
'Cookie': '',
}
assert log.body == 'test'
assert log.http_stub == http_body
assert log.method == HTTPMethod.POST.name
assert log.path == 'http://testserver/default_path/'

def test_write_log_without_empty_headers(self, http_stub_factory, client):
"""Tests request logging without empty headers.

:param http_stub_factory: HTTPStub factory
:param client: http client fixture
"""
http_stub_factory(method=HTTPMethod.GET.name)
client.get(self.path)
log = LogEntry.objects.last()
assert bool(log.headers) is False
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicking, but:

Suggested change
assert bool(log.headers) is False
assert not bool(log.headers)


@pytest.mark.parametrize('method', HTTPMethod.names())
def test_exist_regexp_stub(self, method: str, http_stub_factory, client):
"""Tests response for the regex stub.
Expand Down
15 changes: 13 additions & 2 deletions http_stubs/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from time import sleep
from typing import Optional
from typing import Dict, Optional

from django.http import HttpRequest, HttpResponse, HttpResponseNotFound
from django.views import View
Expand Down Expand Up @@ -52,7 +52,7 @@ def dispatch(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
method=request.method,
source_ip=request.META['REMOTE_ADDR'],
body=request.body.decode('utf-8'),
headers=dict(request.headers),
headers=self.remove_empty_headers(request.headers),
http_stub=stub,
)
sleep(stub.resp_delay / 1000)
Expand All @@ -64,3 +64,14 @@ def dispatch(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
for header_name, header_value in stub.resp_headers.items():
response[header_name] = header_value
return response

def remove_empty_headers(self, headers: Dict[str, str]) -> Dict[str, str]:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just asking: can't be empty headers significant? E.g. indicate there's something wrong on the sending side?

"""Remove empty headers.

:param headers: dictionary of headers
:returns: dictionary without empty headers
"""
return {
header: header_value
for header, header_value in headers.items() if header_value != ''
}