Skip to content

Commit

Permalink
build: Fix CI
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Dygalo <[email protected]>
  • Loading branch information
Stranger6667 committed Jun 29, 2024
1 parent 4340d0f commit 5cc7a5e
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 31 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,3 @@ dmypy.json

# Cython debug symbols
cython_debug/

2 changes: 1 addition & 1 deletion benches/entries.json

Large diffs are not rendered by default.

43 changes: 24 additions & 19 deletions src/harfile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,19 @@
from types import TracebackType
from typing import IO, Any

from ._models import Browser, Cache, Creator, Request, Response, Timings
from ._models import (
Browser,
Cache,
Content,
Cookie,
Creator,
PostData,
PostParameter,
Record,
Request,
Response,
Timings,
)
from ._version import VERSION

__all__ = [
Expand All @@ -22,6 +34,11 @@
"Request",
"Response",
"Timings",
"Cookie",
"Record",
"PostData",
"PostParameter",
"Content",
"HAR_VERSION",
]

Expand Down Expand Up @@ -135,27 +152,15 @@ def _write_entry(
separator = "" if self._is_first_entry else ",\n"
self._is_first_entry = False
self._fd.write(f"{separator} {{")
self._fd.write(
f'\n "startedDateTime": "{startedDateTime.isoformat()}",'
)
self._fd.write(f'\n "startedDateTime": "{startedDateTime.isoformat()}",')
self._fd.write(f'\n "time": {time},')
self._fd.write(
f'\n "request": {json.dumps(asdict(request, dict_factory=_dict_factory))},'
)
self._fd.write(
f'\n "response": {json.dumps(asdict(response, dict_factory=_dict_factory))},'
)
self._fd.write(
f'\n "timings": {json.dumps(asdict(timings, dict_factory=_dict_factory))}'
)
self._fd.write(f'\n "request": {json.dumps(asdict(request, dict_factory=_dict_factory))},')
self._fd.write(f'\n "response": {json.dumps(asdict(response, dict_factory=_dict_factory))},')
self._fd.write(f'\n "timings": {json.dumps(asdict(timings, dict_factory=_dict_factory))}')
if cache:
self._fd.write(
f',\n "cache": {json.dumps(asdict(cache, dict_factory=_dict_factory))}'
)
self._fd.write(f',\n "cache": {json.dumps(asdict(cache, dict_factory=_dict_factory))}')
if serverIPAddress:
self._fd.write(
f',\n "serverIPAddress": {json.dumps(serverIPAddress)}'
)
self._fd.write(f',\n "serverIPAddress": {json.dumps(serverIPAddress)}')
if connection:
self._fd.write(f',\n "connection": {json.dumps(connection)}')
if comment:
Expand Down
1 change: 0 additions & 1 deletion src/harfile/_version.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from importlib import metadata


try:
VERSION = metadata.version(__package__)
except metadata.PackageNotFoundError:
Expand Down
134 changes: 127 additions & 7 deletions tests/test_har.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import datetime
import io
import json
import sys

import harfile
import jsonschema
from hypothesis import HealthCheck, Phase, given, settings
from hypothesis import strategies as st

import harfile

# Derived from http://www.softwareishard.com/har/viewer/
DATETIME_PATTERN = r"^(\d{4})(-)?(\d\d)(-)?(\d\d)(T)?(\d\d)(:)?(\d\d)(:)?(\d\d)(\.\d+)?(Z|([+-])(\d\d)(:)?(\d\d))"
RECORD_SCHEMA = {
Expand Down Expand Up @@ -145,9 +147,7 @@
"name": {"type": "string"},
"value": {"type": "string"},
"fileName": {"type": "string"},
"contentType": {
"type": "string"
},
"contentType": {"type": "string"},
"comment": {"type": "string"},
},
"required": ["name"],
Expand Down Expand Up @@ -267,11 +267,128 @@
}
HAR_VALIDATOR = jsonschema.Draft7Validator(HAR_SCHEMA)


if sys.version_info < (3, 10):
st.register_type_strategy(
harfile.Request,
st.builds(
harfile.Request,
bodySize=st.integers(),
comment=st.text() | st.none(),
cookies=st.lists(
st.builds(
harfile.Cookie,
comment=st.text() | st.none(),
domain=st.text() | st.none(),
expires=st.text() | st.none(),
httpOnly=st.booleans() | st.none(),
name=st.text(),
path=st.text() | st.none(),
secure=st.booleans() | st.none(),
value=st.text(),
)
),
headers=st.lists(
st.builds(
harfile.Record,
comment=st.text() | st.none(),
name=st.text(),
value=st.text(),
)
),
headersSize=st.integers(),
httpVersion=st.text(),
method=st.text(),
postData=st.builds(
harfile.PostData,
comment=st.text() | st.none(),
mimeType=st.text(),
params=st.lists(
st.builds(
harfile.PostParameter,
comment=st.text() | st.none(),
contentType=st.text() | st.none(),
fileName=st.text() | st.none(),
name=st.text(),
value=st.text() | st.none(),
)
),
),
text=st.text() | st.none(),
queryString=st.lists(
st.builds(
harfile.Record,
comment=st.text() | st.none(),
name=st.text(),
value=st.text(),
)
),
url=st.text(),
),
)
st.register_type_strategy(
harfile.Response,
st.builds(
harfile.Response,
bodySize=st.integers(),
comment=st.text() | st.none(),
content=st.builds(
harfile.Content,
comment=st.text() | st.none(),
compression=st.floats(allow_nan=False, allow_infinity=False) | st.integers(),
encoding=st.text() | st.none(),
mimeType=st.text() | st.none(),
size=st.integers(),
text=st.text() | st.none(),
),
cookies=st.lists(
st.builds(
harfile.Cookie,
comment=st.text() | st.none(),
domain=st.text() | st.none(),
expires=st.text() | st.none(),
httpOnly=st.booleans() | st.none(),
name=st.text(),
path=st.text() | st.none(),
secure=st.booleans() | st.none(),
value=st.text(),
)
),
headers=st.lists(
st.builds(
harfile.Record,
comment=st.text() | st.none(),
name=st.text(),
value=st.text(),
)
),
headersSize=st.integers(),
httpVersion=st.text(),
redirectURL=st.text(),
status=st.integers(),
statusText=st.text(),
),
st.register_type_strategy(
harfile.Timings,
st.builds(
harfile.Timings,
blocked=st.floats() | st.integers(),
comment=st.text() | st.none(),
connect=st.floats() | st.integers(),
dns=st.floats() | st.integers(),
receive=st.floats() | st.integers(),
send=st.floats() | st.integers(),
ssl=st.floats() | st.integers(),
wait=st.floats() | st.integers(),
),
),
)


ENTRY_STRATEGY = st.fixed_dictionaries(
{
"startedDateTime": st.datetimes(timezones=st.just(datetime.timezone.utc)),
"time": st.floats(min_value=0, allow_nan=False, allow_infinity=False)
| st.integers(min_value=0),
"time": st.floats(min_value=0, allow_nan=False, allow_infinity=False) | st.integers(min_value=0),
"request": st.from_type(harfile.Request),
"response": st.from_type(harfile.Response),
"timings": st.from_type(harfile.Timings),
Expand All @@ -294,7 +411,7 @@ def write_har(arg, entries):
@given(entries=st.lists(ENTRY_STRATEGY, max_size=10))
@settings(
phases=[Phase.reuse, Phase.generate, Phase.shrink],
suppress_health_check=[HealthCheck.function_scoped_fixture],
suppress_health_check=[HealthCheck.function_scoped_fixture, HealthCheck.too_slow],
)
def test_write_har(entries, tmp_path):
buffer = io.StringIO()
Expand All @@ -305,3 +422,6 @@ def test_write_har(entries, tmp_path):
write_har(path, entries)
with open(path) as fd:
HAR_VALIDATOR.validate(json.load(fd))
write_har(path, entries)
with open(path) as fd:
HAR_VALIDATOR.validate(json.load(fd))
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tox]
isolated_build = true
envlist = py{38.39310,311,312},coverage-report
envlist = py{38,39,310,311,312},coverage-report

[gh-actions]
python =
Expand All @@ -16,7 +16,7 @@ extras =
cov
setenv = COVERAGE_PROCESS_START={toxinidir}/pyproject.toml
commands =
coverage run -m pytest {posargs:-n auto --durations=10} tests
coverage run -m pytest {posargs} tests
coverage combine --keep
coverage report
coverage xml -i
Expand Down

0 comments on commit 5cc7a5e

Please sign in to comment.