Skip to content

Commit

Permalink
issue-75: adds status saving to the log
Browse files Browse the repository at this point in the history
  • Loading branch information
lowitea committed Oct 21, 2021
1 parent afc56d0 commit 1e64bc2
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 1 deletion.
2 changes: 2 additions & 0 deletions http_stubs/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def pretty_request_body(self, instance) -> str:
'pk',
'request_date',
'http_stub',
'resp_status',
'source_ip',
'result_script',
'path',
Expand All @@ -70,6 +71,7 @@ def pretty_request_body(self, instance) -> str:
'pk',
'request_date',
'http_stub',
'resp_status',
'source_ip',
'result_script',
),
Expand Down
26 changes: 26 additions & 0 deletions http_stubs/migrations/0008_log_response_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 3.1.13 on 2021-10-21 21:22

import django.core.validators
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('http_stubs', '0007_add_proxy_stubs'),
]

operations = [
migrations.AddField(
model_name='logentry',
name='resp_status',
field=models.IntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), django.core.validators.MaxValueValidator(600)], verbose_name='Response status'),
preserve_default=False,
),
migrations.AddField(
model_name='proxylogentity',
name='resp_status',
field=models.IntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), django.core.validators.MaxValueValidator(600)], verbose_name='Response status'),
preserve_default=False,
),
]
4 changes: 4 additions & 0 deletions http_stubs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ class AbstractLogEntry(models.Model):
max_length=10,
choices=HTTPMethod.choices,
)
resp_status = models.IntegerField(
verbose_name='Response status',
validators=(MinValueValidator(0), MaxValueValidator(600)),
)
source_ip = models.GenericIPAddressField(
verbose_name='Source IP',
)
Expand Down
1 change: 1 addition & 0 deletions http_stubs/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def factory(**kwargs) -> LogEntry:
'request_headers': {},
'http_stub': http_stub,
'result_script': '',
'resp_status': 418,
}
default_params.update(kwargs)
return LogEntry.objects.create(**default_params)
Expand Down
5 changes: 4 additions & 1 deletion http_stubs/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def test_write_log(self, http_stub_factory, client):
regex_path=True,
request_script='a = 1',
enable_logging=True,
resp_status=401,
)

request_path = f'/regex/?query={"search" * 300}'
Expand Down Expand Up @@ -87,6 +88,7 @@ def _datefmt(date) -> str: # noqa:WPS430
assert log.method == HTTPMethod.POST.name
assert log.path == f'http://testserver{request_path}'
assert log.result_script == 'Done'
assert log.resp_status == http_stub.resp_status

def test_empty_log(self, http_stub_factory, client):
"""Tests http stub without logs.
Expand Down Expand Up @@ -198,7 +200,7 @@ def test_proxy_httpstub_executor(
# not supported wsgi header
fake_response.headers['Trailers'] = 'value'
fake_response._content = b'I am a teapot'
fake_response.status_code = 418
fake_response.status_code = 420
fake_response.request = Request(url='target_url')
mock_request_func.return_value = fake_response

Expand Down Expand Up @@ -247,6 +249,7 @@ def test_proxy_httpstub_executor(
assert log.response_latency == 0
assert log.response_body == 'I am a teapot'
assert log.response_headers == fake_response.headers
assert log.resp_status == fake_response.status_code


@pytest.mark.parametrize(
Expand Down
2 changes: 2 additions & 0 deletions http_stubs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def _httpstub_executor(
request_body=_request_body_decode(request),
http_stub=stub,
result_script=result_script,
resp_status=stub.resp_status,
) if stub.enable_logging else None

return response, log
Expand Down Expand Up @@ -112,6 +113,7 @@ def _proxy_httpstub_executor( # noqa: WPS210
response_latency=t_response.elapsed.microseconds,
response_body=t_response.text,
response_headers=dict(t_response.headers),
resp_status=t_response.status_code,
)

return response, log
Expand Down

0 comments on commit 1e64bc2

Please sign in to comment.