harfile 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,155 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ .ruff_cache
55
+
56
+ # Translations
57
+ *.mo
58
+ *.pot
59
+
60
+ # Django stuff:
61
+ *.log
62
+ local_settings.py
63
+ db.sqlite3
64
+ db.sqlite3-journal
65
+
66
+ # Flask stuff:
67
+ instance/
68
+ .webassets-cache
69
+
70
+ # Scrapy stuff:
71
+ .scrapy
72
+
73
+ # Sphinx documentation
74
+ docs/_build/
75
+
76
+ # PyBuilder
77
+ .pybuilder/
78
+ target/
79
+
80
+ # Jupyter Notebook
81
+ .ipynb_checkpoints
82
+
83
+ # IPython
84
+ profile_default/
85
+ ipython_config.py
86
+
87
+ # pyenv
88
+ # For a library or package, you might want to ignore these files since the code is
89
+ # intended to run in multiple environments; otherwise, check them in:
90
+ # .python-version
91
+
92
+ # pipenv
93
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
94
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
95
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
96
+ # install all needed dependencies.
97
+ #Pipfile.lock
98
+
99
+ # poetry
100
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
101
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
102
+ # commonly ignored for libraries.
103
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
104
+ #poetry.lock
105
+
106
+ # pdm
107
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
108
+ #pdm.lock
109
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
110
+ # in version control.
111
+ # https://pdm.fming.dev/#use-with-ide
112
+ .pdm.toml
113
+
114
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
115
+ __pypackages__/
116
+
117
+ # Celery stuff
118
+ celerybeat-schedule
119
+ celerybeat.pid
120
+
121
+ # SageMath parsed files
122
+ *.sage.py
123
+
124
+ # Environments
125
+ .env
126
+ .venv
127
+ env/
128
+ venv/
129
+ ENV/
130
+ env.bak/
131
+ venv.bak/
132
+
133
+ # Spyder project settings
134
+ .spyderproject
135
+ .spyproject
136
+
137
+ # Rope project settings
138
+ .ropeproject
139
+
140
+ # mkdocs documentation
141
+ /site
142
+
143
+ # mypy
144
+ .mypy_cache/
145
+ .dmypy.json
146
+ dmypy.json
147
+
148
+ # Pyre type checker
149
+ .pyre/
150
+
151
+ # pytype static type analyzer
152
+ .pytype/
153
+
154
+ # Cython debug symbols
155
+ cython_debug/
harfile-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Schemathesis.io
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
harfile-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,102 @@
1
+ Metadata-Version: 2.3
2
+ Name: harfile
3
+ Version: 0.1.0
4
+ Summary: Writer for HTTP Archive (HAR) files
5
+ Project-URL: Changelog, https://github.com/schemathesis/harfile/blob/main/CHANGELOG.md
6
+ Project-URL: Bug Tracker, https://github.com/schemathesis/harfile
7
+ Project-URL: Funding, https://github.com/sponsors/Stranger6667
8
+ Project-URL: Source Code, https://github.com/schemathesis/harfile
9
+ Author-email: Dmitry Dygalo <dmitry@dygalo.dev>
10
+ Maintainer-email: Dmitry Dygalo <dmitry@dygalo.dev>
11
+ License-Expression: MIT
12
+ License-File: LICENSE
13
+ Keywords: har,http,testing
14
+ Classifier: Development Status :: 3 - Alpha
15
+ Classifier: Environment :: Console
16
+ Classifier: Intended Audience :: Developers
17
+ Classifier: License :: OSI Approved :: MIT License
18
+ Classifier: Operating System :: OS Independent
19
+ Classifier: Programming Language :: Python :: 3 :: Only
20
+ Classifier: Programming Language :: Python :: 3.8
21
+ Classifier: Programming Language :: Python :: 3.9
22
+ Classifier: Programming Language :: Python :: 3.10
23
+ Classifier: Programming Language :: Python :: 3.11
24
+ Classifier: Programming Language :: Python :: 3.12
25
+ Classifier: Programming Language :: Python :: Implementation :: CPython
26
+ Classifier: Topic :: Software Development :: Testing
27
+ Requires-Python: >=3.8
28
+ Provides-Extra: bench
29
+ Requires-Dist: pytest-codspeed==2.2.1; extra == 'bench'
30
+ Provides-Extra: cov
31
+ Requires-Dist: coverage-enable-subprocess; extra == 'cov'
32
+ Requires-Dist: coverage[toml]>=7; extra == 'cov'
33
+ Provides-Extra: dev
34
+ Requires-Dist: coverage-enable-subprocess; extra == 'dev'
35
+ Requires-Dist: coverage>=7; extra == 'dev'
36
+ Requires-Dist: coverage[toml]>=7; extra == 'dev'
37
+ Requires-Dist: hypothesis-jsonschema>=0.23.1; extra == 'dev'
38
+ Requires-Dist: hypothesis>=6; extra == 'dev'
39
+ Requires-Dist: jsonschema>=4.18.0; extra == 'dev'
40
+ Requires-Dist: pytest-codspeed==2.2.1; extra == 'dev'
41
+ Requires-Dist: pytest<8,>=6.2.0; extra == 'dev'
42
+ Provides-Extra: tests
43
+ Requires-Dist: coverage>=7; extra == 'tests'
44
+ Requires-Dist: hypothesis-jsonschema>=0.23.1; extra == 'tests'
45
+ Requires-Dist: hypothesis>=6; extra == 'tests'
46
+ Requires-Dist: jsonschema>=4.18.0; extra == 'tests'
47
+ Requires-Dist: pytest<8,>=6.2.0; extra == 'tests'
48
+ Description-Content-Type: text/markdown
49
+
50
+ # harfile
51
+
52
+ [![CI](https://github.com/schemathesis/harfile/actions/workflows/ci.yml/badge.svg)](https://github.com/schemathesis/harfile/actions/workflows/ci.yml)
53
+ [![Coverage](https://codecov.io/gh/Stranger6667/hypothesis-openapi/branch/main/graph/badge.svg)](https://codecov.io/gh/schemathesis/harfile/branch/main)
54
+ [![Version](https://img.shields.io/pypi/v/harfile.svg)](https://pypi.org/project/harfile/)
55
+ [![Python versions](https://img.shields.io/pypi/pyversions/harfile.svg)](https://pypi.org/project/harfile/)
56
+ [![License](https://img.shields.io/pypi/l/harfile.svg)](https://opensource.org/licenses/MIT)
57
+
58
+ This package provides zero dependency writer for building HAR (HTTP Archive) files in Python.
59
+
60
+ **NOTES**:
61
+
62
+ - The writer assumes single-threaded
63
+ - Pages are not supported
64
+
65
+ ## Usage
66
+
67
+ ```python
68
+ import datetime
69
+ import io
70
+
71
+ import harfile
72
+
73
+
74
+ # Write to a file
75
+ with harfile.open("filename.har") as har:
76
+ har.add_entry(
77
+ started_date_time=datetime.datetime.now(),
78
+ time=42,
79
+ request=harfile.Request(
80
+ method="GET",
81
+ url="http://example.com",
82
+ http_version="HTTP/1.1",
83
+ ),
84
+ response=harfile.Response(
85
+ status=200,
86
+ status_text="OK",
87
+ http_version="HTTP/1.1",
88
+ ),
89
+ )
90
+
91
+
92
+ # Write to a string buffer
93
+ buffer = io.StringIO()
94
+ with harfile.open(buffer) as har:
95
+ pass
96
+
97
+ ```
98
+
99
+ ## License
100
+
101
+ The code in this project is licensed under [MIT license](https://opensource.org/licenses/MIT).
102
+ By contributing to `harfile`, you agree that your contributions will be licensed under its MIT license.
@@ -0,0 +1,53 @@
1
+ # harfile
2
+
3
+ [![CI](https://github.com/schemathesis/harfile/actions/workflows/ci.yml/badge.svg)](https://github.com/schemathesis/harfile/actions/workflows/ci.yml)
4
+ [![Coverage](https://codecov.io/gh/Stranger6667/hypothesis-openapi/branch/main/graph/badge.svg)](https://codecov.io/gh/schemathesis/harfile/branch/main)
5
+ [![Version](https://img.shields.io/pypi/v/harfile.svg)](https://pypi.org/project/harfile/)
6
+ [![Python versions](https://img.shields.io/pypi/pyversions/harfile.svg)](https://pypi.org/project/harfile/)
7
+ [![License](https://img.shields.io/pypi/l/harfile.svg)](https://opensource.org/licenses/MIT)
8
+
9
+ This package provides zero dependency writer for building HAR (HTTP Archive) files in Python.
10
+
11
+ **NOTES**:
12
+
13
+ - The writer assumes single-threaded
14
+ - Pages are not supported
15
+
16
+ ## Usage
17
+
18
+ ```python
19
+ import datetime
20
+ import io
21
+
22
+ import harfile
23
+
24
+
25
+ # Write to a file
26
+ with harfile.open("filename.har") as har:
27
+ har.add_entry(
28
+ started_date_time=datetime.datetime.now(),
29
+ time=42,
30
+ request=harfile.Request(
31
+ method="GET",
32
+ url="http://example.com",
33
+ http_version="HTTP/1.1",
34
+ ),
35
+ response=harfile.Response(
36
+ status=200,
37
+ status_text="OK",
38
+ http_version="HTTP/1.1",
39
+ ),
40
+ )
41
+
42
+
43
+ # Write to a string buffer
44
+ buffer = io.StringIO()
45
+ with harfile.open(buffer) as har:
46
+ pass
47
+
48
+ ```
49
+
50
+ ## License
51
+
52
+ The code in this project is licensed under [MIT license](https://opensource.org/licenses/MIT).
53
+ By contributing to `harfile`, you agree that your contributions will be licensed under its MIT license.
@@ -0,0 +1,113 @@
1
+ [project]
2
+ name = "harfile"
3
+ version = "0.1.0"
4
+ description = "Writer for HTTP Archive (HAR) files"
5
+ keywords = ["har", "http", "testing"]
6
+ classifiers = [
7
+ "Development Status :: 3 - Alpha",
8
+ "Environment :: Console",
9
+ "Intended Audience :: Developers",
10
+ "License :: OSI Approved :: MIT License",
11
+ "Operating System :: OS Independent",
12
+ "Programming Language :: Python :: 3 :: Only",
13
+ "Programming Language :: Python :: 3.8",
14
+ "Programming Language :: Python :: 3.9",
15
+ "Programming Language :: Python :: 3.10",
16
+ "Programming Language :: Python :: 3.11",
17
+ "Programming Language :: Python :: 3.12",
18
+ "Programming Language :: Python :: Implementation :: CPython",
19
+ "Topic :: Software Development :: Testing",
20
+ ]
21
+ authors = [{ name = "Dmitry Dygalo", email = "dmitry@dygalo.dev" }]
22
+ maintainers = [{ name = "Dmitry Dygalo", email = "dmitry@dygalo.dev" }]
23
+ readme = "README.md"
24
+ license = "MIT"
25
+ include = ["src/harfile/py.typed"]
26
+ requires-python = ">=3.8"
27
+
28
+ [project.optional-dependencies]
29
+ tests = [
30
+ "coverage>=7",
31
+ "hypothesis>=6",
32
+ "hypothesis-jsonschema>=0.23.1",
33
+ "jsonschema>=4.18.0",
34
+ "pytest>=6.2.0,<8",
35
+ ]
36
+ cov = [
37
+ "coverage-enable-subprocess",
38
+ "coverage[toml]>=7",
39
+ ]
40
+ bench = [
41
+ "pytest-codspeed==2.2.1",
42
+ ]
43
+ dev = ["harfile[tests,cov,bench]"]
44
+
45
+ [project.urls]
46
+ Changelog = "https://github.com/schemathesis/harfile/blob/main/CHANGELOG.md"
47
+ "Bug Tracker" = "https://github.com/schemathesis/harfile"
48
+ Funding = "https://github.com/sponsors/Stranger6667"
49
+ "Source Code" = "https://github.com/schemathesis/harfile"
50
+
51
+ [tool.hatch.build.targets.sdist]
52
+ include = [
53
+ "/src",
54
+ "/tests",
55
+ ]
56
+
57
+ [tool.coverage.run]
58
+ parallel = true
59
+ branch = true
60
+ source_pkgs = ["harfile"]
61
+
62
+ [tool.coverage.paths]
63
+ source = ["src", ".tox/*/site-packages"]
64
+
65
+ [tool.coverage.report]
66
+ show_missing = true
67
+ skip_covered = true
68
+ precision = 2
69
+ exclude_lines = [
70
+ "raise NotImplementedError",
71
+ "if TYPE_CHECKING:",
72
+ "pass"
73
+ ]
74
+
75
+ [tool.ruff]
76
+ line-length = 120
77
+ target-version = "py38"
78
+
79
+ [tool.ruff.lint]
80
+ select = [
81
+ "E", # pycodestyle errors
82
+ "W", # pycodestyle warnings
83
+ "F", # pyflakes
84
+ "C", # flake8-comprehensions
85
+ "B", # flake8-bugbear
86
+ "D", # pydocstyle
87
+ ]
88
+ ignore = [
89
+ "E501", # Line too long
90
+ "B008", # Do not perform function calls in argument defaults
91
+ "C901", # Too complex
92
+ "D100", # Missing docstring in public module
93
+ "D101", # Missing docstring in public class
94
+ "D102", # Missing docstring in public method
95
+ "D103", # Missing docstring in public function
96
+ "D104", # Missing docstring in public package
97
+ "D105", # Missing docstring in magic method
98
+ "D107", # Missing docstring in `__init__`
99
+ "D203", # One blank line before class
100
+ "D213", # Multiline summary second line
101
+ "D401", # Imperative mood
102
+ ]
103
+
104
+ [tool.ruff.format]
105
+ skip-magic-trailing-comma = false
106
+
107
+ [tool.ruff.lint.isort]
108
+ known-first-party = ["harfile"]
109
+ known-third-party = ["hypothesis", "hypothesis-jsonschema", "pytest"]
110
+
111
+ [build-system]
112
+ requires = ["hatchling"]
113
+ build-backend = "hatchling.build"
@@ -0,0 +1,203 @@
1
+ """Write HTTP Archive (HAR) files."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import builtins
6
+ import json
7
+ from dataclasses import asdict
8
+ from datetime import datetime
9
+ from os import PathLike
10
+ from types import TracebackType
11
+ from typing import IO, Any
12
+
13
+ from ._models import (
14
+ Browser,
15
+ Cache,
16
+ Content,
17
+ Cookie,
18
+ Creator,
19
+ PostData,
20
+ PostParameter,
21
+ Record,
22
+ Request,
23
+ Response,
24
+ Timings,
25
+ )
26
+ from ._version import VERSION
27
+
28
+ __all__ = [
29
+ "open",
30
+ "HarFile",
31
+ "Browser",
32
+ "Creator",
33
+ "Cache",
34
+ "Request",
35
+ "Response",
36
+ "Timings",
37
+ "Cookie",
38
+ "Record",
39
+ "PostData",
40
+ "PostParameter",
41
+ "Content",
42
+ "HAR_VERSION",
43
+ ]
44
+
45
+ HAR_VERSION = "1.2"
46
+
47
+
48
+ class HarFile:
49
+ _fd: IO[str]
50
+ _creator: Creator
51
+ _browser: Browser
52
+ _comment: str | None
53
+ _is_first_entry: bool = True
54
+ _has_preable: bool = False
55
+ closed: bool = False
56
+
57
+ def __init__(
58
+ self,
59
+ fd: IO[str],
60
+ *,
61
+ creator: Creator | None = None,
62
+ browser: Browser | None = None,
63
+ comment: str | None = None,
64
+ ):
65
+ self._fd = fd
66
+ self._creator = creator or Creator(name="harfile", version=VERSION)
67
+ self._browser = browser or Browser(name="", version="")
68
+ self._comment = comment
69
+
70
+ @classmethod
71
+ def open(
72
+ cls,
73
+ name_or_fd: str | PathLike | IO[str],
74
+ *,
75
+ creator: Creator | None = None,
76
+ browser: Browser | None = None,
77
+ comment: str | None = None,
78
+ ) -> HarFile:
79
+ """Open a HAR file for writing."""
80
+ fd: IO[str]
81
+ if isinstance(name_or_fd, (str, PathLike)):
82
+ fd = builtins.open(name_or_fd, "w")
83
+ else:
84
+ fd = name_or_fd
85
+ return cls(fd=fd, creator=creator, browser=browser, comment=comment)
86
+
87
+ def close(self) -> None:
88
+ """Close the HAR file."""
89
+ if self.closed:
90
+ return None
91
+ self.closed = True
92
+ if not self._has_preable:
93
+ self._write_preamble()
94
+ self._has_preable = True
95
+ self._write_postscript()
96
+
97
+ def __enter__(self) -> HarFile:
98
+ return self
99
+
100
+ def __exit__(
101
+ self,
102
+ type: type[BaseException] | None,
103
+ value: BaseException | None,
104
+ traceback: TracebackType | None,
105
+ ) -> None:
106
+ if type is None:
107
+ self.close()
108
+ return None
109
+
110
+ def add_entry(
111
+ self,
112
+ *,
113
+ startedDateTime: datetime,
114
+ time: int | float,
115
+ request: Request,
116
+ response: Response,
117
+ timings: Timings,
118
+ cache: Cache | None = None,
119
+ serverIPAddress: str | None = None,
120
+ connection: str | None = None,
121
+ comment: str | None = None,
122
+ ) -> None:
123
+ """Add an HTTP request to the HAR file."""
124
+ if not self._has_preable:
125
+ self._write_preamble()
126
+ self._has_preable = True
127
+ self._write_entry(
128
+ startedDateTime=startedDateTime,
129
+ time=time,
130
+ request=request,
131
+ response=response,
132
+ cache=cache,
133
+ timings=timings,
134
+ serverIPAddress=serverIPAddress,
135
+ connection=connection,
136
+ comment=comment,
137
+ )
138
+
139
+ def _write_entry(
140
+ self,
141
+ *,
142
+ startedDateTime: datetime,
143
+ time: int | float,
144
+ request: Request,
145
+ response: Response,
146
+ timings: Timings,
147
+ cache: Cache | None = None,
148
+ serverIPAddress: str | None = None,
149
+ connection: str | None = None,
150
+ comment: str | None = None,
151
+ ) -> None:
152
+ separator = "\n" if self._is_first_entry else ",\n"
153
+ self._is_first_entry = False
154
+ self._fd.write(f"{separator} {{")
155
+ self._fd.write(f'\n "startedDateTime": "{startedDateTime.isoformat()}",')
156
+ self._fd.write(f'\n "time": {time},')
157
+ self._fd.write(f'\n "request": {json.dumps(asdict(request, dict_factory=_dict_factory))},')
158
+ self._fd.write(f'\n "response": {json.dumps(asdict(response, dict_factory=_dict_factory))},')
159
+ self._fd.write(f'\n "timings": {json.dumps(asdict(timings, dict_factory=_dict_factory))}')
160
+ if cache:
161
+ self._fd.write(f',\n "cache": {json.dumps(asdict(cache, dict_factory=_dict_factory))}')
162
+ if serverIPAddress:
163
+ self._fd.write(f',\n "serverIPAddress": {json.dumps(serverIPAddress)}')
164
+ if connection:
165
+ self._fd.write(f',\n "connection": {json.dumps(connection)}')
166
+ if comment:
167
+ self._fd.write(f',\n "comment": {json.dumps(comment)}')
168
+ self._fd.write("\n }")
169
+
170
+ def _write_preamble(self) -> None:
171
+ creator = f"""{{
172
+ "name": "{self._creator.name}",
173
+ "version": "{self._creator.version}\""""
174
+ if self._creator.comment:
175
+ creator += f',\n "comment": "{self._creator.comment}"'
176
+ creator += "\n }"
177
+ browser = f"""{{
178
+ "name": "{self._browser.name}",
179
+ "version": "{self._browser.version}\""""
180
+ if self._browser.comment:
181
+ browser += f',\n "comment": "{self._browser.comment}"'
182
+ browser += "\n }"
183
+ self._fd.write(f"""{{
184
+ "log": {{
185
+ "version": "{HAR_VERSION}",
186
+ "creator": {creator},
187
+ "browser": {browser}""")
188
+ if self._comment:
189
+ self._fd.write(f' "comment": "{self._comment}"')
190
+ self._fd.write(',\n "entries": [')
191
+
192
+ def _write_postscript(self) -> None:
193
+ if self._is_first_entry:
194
+ self._fd.write("]\n }\n}")
195
+ else:
196
+ self._fd.write("\n ]\n }\n}")
197
+
198
+
199
+ def _dict_factory(value: list[tuple[str, Any]]) -> dict[str, Any]:
200
+ return {key: value for key, value in value if value is not None}
201
+
202
+
203
+ open = HarFile.open
@@ -0,0 +1,167 @@
1
+ from __future__ import annotations
2
+
3
+ from dataclasses import dataclass, field
4
+
5
+
6
+ @dataclass
7
+ class Creator:
8
+ # Name of the application used to export the log
9
+ name: str
10
+ # Version of the application used to export the log
11
+ version: str
12
+ comment: str | None = None
13
+
14
+
15
+ @dataclass
16
+ class Browser:
17
+ # Name of the browser used to export the log
18
+ name: str
19
+ # Version of the browser used to export the log
20
+ version: str
21
+ comment: str | None = None
22
+
23
+
24
+ @dataclass
25
+ class Cache:
26
+ """Info about a request coming from browser cache."""
27
+
28
+ # State of a cache entry before the request
29
+ beforeRequest: CacheEntry | None = None
30
+ # State of a cache entry after the request
31
+ afterRequest: CacheEntry | None = None
32
+ comment: str | None = None
33
+
34
+
35
+ @dataclass
36
+ class CacheEntry:
37
+ # Expiration time of the cache entry
38
+ expires: str
39
+ # The last time the cache entry was opened
40
+ lastAccess: str
41
+ eTag: str
42
+ # The number of times the cache entry has been opened
43
+ hitCount: int
44
+ comment: str | None = None
45
+
46
+
47
+ @dataclass
48
+ class Request:
49
+ method: str
50
+ # Absolute URL of the request (fragments are not included)
51
+ url: str
52
+ # Request HTTP Version
53
+ httpVersion: str
54
+ cookies: list[Cookie] = field(default_factory=list)
55
+ headers: list[Record] = field(default_factory=list)
56
+ queryString: list[Record] = field(default_factory=list)
57
+ postData: PostData | None = None
58
+ # Total number of bytes from the start of the HTTP request message until (and including) the double CRLF
59
+ # before the body
60
+ headersSize: int = -1
61
+ # Size of the request body (POST data payload) in bytes
62
+ bodySize: int = -1
63
+ comment: str | None = None
64
+
65
+
66
+ @dataclass
67
+ class Cookie:
68
+ # The name of the cookie
69
+ name: str
70
+ # The cookie value
71
+ value: str
72
+ # The path pertaining to the cookie
73
+ path: str | None = None
74
+ # The host of the cookie
75
+ domain: str | None = None
76
+ # Cookie expiration time
77
+ expires: str | None = None
78
+ # Set to true if the cookie is HTTP only, false otherwise
79
+ httpOnly: bool | None = None
80
+ # True if the cookie was transmitted over ssl, false otherwise
81
+ secure: bool | None = None
82
+ comment: str | None = None
83
+
84
+
85
+ @dataclass
86
+ class Record:
87
+ name: str
88
+ value: str
89
+ comment: str | None = None
90
+
91
+
92
+ @dataclass
93
+ class PostData:
94
+ # MIME type of the posted data
95
+ mimeType: str
96
+ # List of posted parameters (in case of URL encoded parameters)
97
+ params: list[PostParameter] | None = None
98
+ # Plain text posted data
99
+ text: str | None = None
100
+ comment: str | None = None
101
+
102
+
103
+ @dataclass
104
+ class PostParameter:
105
+ name: str
106
+ value: str | None = None
107
+ fileName: str | None = None
108
+ contentType: str | None = None
109
+ comment: str | None = None
110
+
111
+
112
+ @dataclass
113
+ class Content:
114
+ size: int = 0
115
+ # Number of bytes saved
116
+ compression: int | float | None = None
117
+ # MIME type of the response text (value of the Content-Type response header).
118
+ # The charset attribute of the MIME type is included
119
+ mimeType: str | None = None
120
+ # Response body sent from the server or loaded from the browser cache.
121
+ # This field is populated with textual content only.
122
+ # The text field is either HTTP decoded text or a encoded (e.g. "base64") representation of the response body
123
+ text: str | None = None
124
+ encoding: str | None = None
125
+ # Encoding used for response text field e.g "base64"
126
+ comment: str | None = None
127
+
128
+
129
+ @dataclass
130
+ class Response:
131
+ # Response status
132
+ status: int
133
+ # Response status description
134
+ statusText: str
135
+ # Response HTTP Version
136
+ httpVersion: str
137
+ cookies: list[Cookie] = field(default_factory=list)
138
+ headers: list[Record] = field(default_factory=list)
139
+ # Details about the response body
140
+ content: Content = field(default_factory=Content)
141
+ # Redirection target URL from the Location response header
142
+ redirectURL: str = ""
143
+ # Total number of bytes from the start of the HTTP response message
144
+ # until (and including) the double CRLF before the body
145
+ headersSize: int = -1
146
+ # Size of the received response body in bytes
147
+ bodySize: int = -1
148
+ comment: str | None = None
149
+
150
+
151
+ @dataclass
152
+ class Timings:
153
+ # Time required to send HTTP request to the server
154
+ send: int | float
155
+ # Waiting for a response from the server
156
+ wait: int | float
157
+ # Time required to read entire response from the server (or cache)
158
+ receive: int | float
159
+ # Time spent in a queue waiting for a network connection
160
+ blocked: int | float = -1
161
+ # DNS resolution time. The time required to resolve a host name
162
+ dns: int | float = -1
163
+ # Time required to create TCP connection
164
+ connect: int | float = -1
165
+ # Time required for SSL/TLS negotiation
166
+ ssl: int | float = -1
167
+ comment: str | None = None
@@ -0,0 +1,7 @@
1
+ from importlib import metadata
2
+
3
+ try:
4
+ VERSION = metadata.version(__package__)
5
+ except metadata.PackageNotFoundError:
6
+ # Local run without installation
7
+ VERSION = "dev"
@@ -0,0 +1,470 @@
1
+ import datetime
2
+ import io
3
+ import json
4
+ import sys
5
+
6
+ import jsonschema
7
+ import pytest
8
+ from hypothesis import HealthCheck, Phase, given, settings
9
+ from hypothesis import strategies as st
10
+
11
+ import harfile
12
+
13
+ # Derived from http://www.softwareishard.com/har/viewer/
14
+ DATETIME_PATTERN = r"^(\d{4})(-)?(\d\d)(-)?(\d\d)(T)?(\d\d)(:)?(\d\d)(:)?(\d\d)(\.\d+)?(Z|([+-])(\d\d)(:)?(\d\d))"
15
+ RECORD_SCHEMA = {
16
+ "type": "object",
17
+ "properties": {
18
+ "name": {"type": "string"},
19
+ "value": {"type": "string"},
20
+ "comment": {"type": "string"},
21
+ },
22
+ "required": ["name", "value"],
23
+ "additionalProperties": False,
24
+ }
25
+ COOKIE_SCHEMA = {
26
+ "type": "object",
27
+ "properties": {
28
+ "name": {"type": "string"},
29
+ "value": {"type": "string"},
30
+ "path": {"type": "string"},
31
+ "domain": {"type": "string"},
32
+ "expires": {"type": "string"},
33
+ "httpOnly": {"type": "boolean"},
34
+ "secure": {"type": "boolean"},
35
+ "comment": {"type": "string"},
36
+ },
37
+ "required": ["name", "value"],
38
+ "additionalProperties": False,
39
+ }
40
+ CACHE_ENTRY_SCHEMA = {
41
+ "type": "object",
42
+ "properties": {
43
+ "expires": {"type": "string"},
44
+ "lastAccess": {"type": "string"},
45
+ "eTag": {"type": "string"},
46
+ "hitCount": {"type": "integer"},
47
+ "comment": {"type": "string"},
48
+ },
49
+ "required": ["lastAccess", "eTag", "hitCount"],
50
+ "additionalProperties": False,
51
+ }
52
+ HAR_SCHEMA = {
53
+ "type": "object",
54
+ "properties": {
55
+ "log": {
56
+ "type": "object",
57
+ "properties": {
58
+ "version": {"type": "string"},
59
+ "creator": {
60
+ "type": "object",
61
+ "properties": {
62
+ "name": {"type": "string"},
63
+ "version": {"type": "string"},
64
+ "comment": {"type": "string"},
65
+ },
66
+ "required": ["name", "version"],
67
+ "additionalProperties": False,
68
+ },
69
+ "browser": {
70
+ "type": "object",
71
+ "properties": {
72
+ "name": {"type": "string"},
73
+ "version": {"type": "string"},
74
+ "comment": {"type": "string"},
75
+ },
76
+ "required": ["name", "version"],
77
+ "additionalProperties": False,
78
+ },
79
+ "pages": {
80
+ "type": "array",
81
+ "items": {
82
+ "type": "object",
83
+ "properties": {
84
+ "startedDateTime": {
85
+ "type": "string",
86
+ "format": "date-time",
87
+ "pattern": DATETIME_PATTERN,
88
+ },
89
+ # NOTE: The original schema requires unique `id`, but JSON Schema does not support
90
+ # uniqueness based on a property value.
91
+ "id": {"type": "string"},
92
+ "title": {"type": "string"},
93
+ "pageTimings": {
94
+ "type": "object",
95
+ "properties": {
96
+ "onContentLoad": {"type": "number"},
97
+ "onLoad": {"type": "number"},
98
+ "comment": {"type": "string"},
99
+ },
100
+ "additionalProperties": False,
101
+ },
102
+ "comment": {"type": "string"},
103
+ },
104
+ "required": ["startedDateTime", "id", "title", "pageTimings"],
105
+ "additionalProperties": False,
106
+ },
107
+ },
108
+ "entries": {
109
+ "type": "array",
110
+ "items": {
111
+ "type": "object",
112
+ "properties": {
113
+ "pageref": {"type": "string"},
114
+ "startedDateTime": {
115
+ "type": "string",
116
+ "format": "date-time",
117
+ "pattern": DATETIME_PATTERN,
118
+ },
119
+ "time": {"type": "number"},
120
+ "request": {
121
+ "type": "object",
122
+ "properties": {
123
+ "method": {"type": "string"},
124
+ "url": {"type": "string"},
125
+ "httpVersion": {"type": "string"},
126
+ "cookies": {
127
+ "type": "array",
128
+ "items": COOKIE_SCHEMA,
129
+ },
130
+ "headers": {
131
+ "type": "array",
132
+ "items": RECORD_SCHEMA,
133
+ },
134
+ "queryString": {
135
+ "type": "array",
136
+ "items": RECORD_SCHEMA,
137
+ },
138
+ "postData": {
139
+ "type": "object",
140
+ "properties": {
141
+ "mimeType": {"type": "string"},
142
+ "text": {"type": "string"},
143
+ "params": {
144
+ "type": "array",
145
+ "items": {
146
+ "type": "object",
147
+ "properties": {
148
+ "name": {"type": "string"},
149
+ "value": {"type": "string"},
150
+ "fileName": {"type": "string"},
151
+ "contentType": {"type": "string"},
152
+ "comment": {"type": "string"},
153
+ },
154
+ "required": ["name"],
155
+ "additionalProperties": False,
156
+ },
157
+ },
158
+ "comment": {"type": "string"},
159
+ },
160
+ "required": ["mimeType"],
161
+ "additionalProperties": False,
162
+ },
163
+ "headersSize": {"type": "integer"},
164
+ "bodySize": {"type": "integer"},
165
+ "comment": {"type": "string"},
166
+ },
167
+ "required": [
168
+ "method",
169
+ "url",
170
+ "httpVersion",
171
+ "cookies",
172
+ "headers",
173
+ "queryString",
174
+ "headersSize",
175
+ "bodySize",
176
+ ],
177
+ "additionalProperties": False,
178
+ },
179
+ "response": {
180
+ "type": "object",
181
+ "properties": {
182
+ "status": {"type": "integer"},
183
+ "statusText": {"type": "string"},
184
+ "httpVersion": {"type": "string"},
185
+ "cookies": {
186
+ "type": "array",
187
+ "items": COOKIE_SCHEMA,
188
+ },
189
+ "headers": {
190
+ "type": "array",
191
+ "items": RECORD_SCHEMA,
192
+ },
193
+ "content": {
194
+ "type": "object",
195
+ "properties": {
196
+ "size": {"type": "integer"},
197
+ "compression": {"type": "number"},
198
+ "mimeType": {"type": "string"},
199
+ "text": {"type": "string"},
200
+ "encoding": {"type": "string"},
201
+ "comment": {"type": "string"},
202
+ },
203
+ "required": ["size"],
204
+ "additionalProperties": False,
205
+ },
206
+ "redirectURL": {"type": "string"},
207
+ "headersSize": {"type": "integer"},
208
+ "bodySize": {"type": "integer"},
209
+ "comment": {"type": "string"},
210
+ },
211
+ "required": [
212
+ "status",
213
+ "statusText",
214
+ "httpVersion",
215
+ "cookies",
216
+ "headers",
217
+ "headersSize",
218
+ "bodySize",
219
+ ],
220
+ "additionalProperties": False,
221
+ },
222
+ "cache": {
223
+ "type": "object",
224
+ "properties": {
225
+ "beforeRequest": CACHE_ENTRY_SCHEMA,
226
+ "afterRequest": CACHE_ENTRY_SCHEMA,
227
+ "comment": {"type": "string"},
228
+ },
229
+ "additionalProperties": False,
230
+ },
231
+ "timings": {
232
+ "type": "object",
233
+ "properties": {
234
+ "dns": {"type": "number"},
235
+ "connect": {"type": "number"},
236
+ "blocked": {"type": "number"},
237
+ "send": {"type": "number"},
238
+ "wait": {"type": "number"},
239
+ "receive": {"type": "number"},
240
+ "ssl": {"type": "number"},
241
+ "comment": {"type": "string"},
242
+ },
243
+ "required": ["send", "wait", "receive"],
244
+ "additionalProperties": False,
245
+ },
246
+ "serverIPAddress": {"type": "string"},
247
+ "connection": {"type": "string"},
248
+ "comment": {"type": "string"},
249
+ },
250
+ "required": [
251
+ "startedDateTime",
252
+ "time",
253
+ "request",
254
+ "response",
255
+ "timings",
256
+ ],
257
+ "additionalProperties": False,
258
+ },
259
+ },
260
+ "comment": {"type": "string"},
261
+ },
262
+ "required": ["version", "creator", "browser", "entries"],
263
+ "additionalProperties": False,
264
+ }
265
+ },
266
+ "required": ["log"],
267
+ "additionalProperties": False,
268
+ }
269
+ HAR_VALIDATOR = jsonschema.Draft7Validator(HAR_SCHEMA)
270
+
271
+
272
+ if sys.version_info < (3, 10):
273
+ st.register_type_strategy(
274
+ harfile.Request,
275
+ st.builds(
276
+ harfile.Request,
277
+ bodySize=st.integers(),
278
+ comment=st.text() | st.none(),
279
+ cookies=st.lists(
280
+ st.builds(
281
+ harfile.Cookie,
282
+ comment=st.text() | st.none(),
283
+ domain=st.text() | st.none(),
284
+ expires=st.text() | st.none(),
285
+ httpOnly=st.booleans() | st.none(),
286
+ name=st.text(),
287
+ path=st.text() | st.none(),
288
+ secure=st.booleans() | st.none(),
289
+ value=st.text(),
290
+ )
291
+ ),
292
+ headers=st.lists(
293
+ st.builds(
294
+ harfile.Record,
295
+ comment=st.text() | st.none(),
296
+ name=st.text(),
297
+ value=st.text(),
298
+ )
299
+ ),
300
+ headersSize=st.integers(),
301
+ httpVersion=st.text(),
302
+ method=st.text(),
303
+ postData=st.builds(
304
+ harfile.PostData,
305
+ comment=st.text() | st.none(),
306
+ mimeType=st.text(),
307
+ params=st.lists(
308
+ st.builds(
309
+ harfile.PostParameter,
310
+ comment=st.text() | st.none(),
311
+ contentType=st.text() | st.none(),
312
+ fileName=st.text() | st.none(),
313
+ name=st.text(),
314
+ value=st.text() | st.none(),
315
+ )
316
+ ),
317
+ ),
318
+ queryString=st.lists(
319
+ st.builds(
320
+ harfile.Record,
321
+ comment=st.text() | st.none(),
322
+ name=st.text(),
323
+ value=st.text(),
324
+ )
325
+ ),
326
+ url=st.text(),
327
+ ),
328
+ )
329
+ st.register_type_strategy(
330
+ harfile.Response,
331
+ st.builds(
332
+ harfile.Response,
333
+ bodySize=st.integers(),
334
+ comment=st.text() | st.none(),
335
+ content=st.builds(
336
+ harfile.Content,
337
+ comment=st.text() | st.none(),
338
+ compression=st.floats(allow_nan=False, allow_infinity=False) | st.integers(),
339
+ encoding=st.text() | st.none(),
340
+ mimeType=st.text() | st.none(),
341
+ size=st.integers(),
342
+ text=st.text() | st.none(),
343
+ ),
344
+ cookies=st.lists(
345
+ st.builds(
346
+ harfile.Cookie,
347
+ comment=st.text() | st.none(),
348
+ domain=st.text() | st.none(),
349
+ expires=st.text() | st.none(),
350
+ httpOnly=st.booleans() | st.none(),
351
+ name=st.text(),
352
+ path=st.text() | st.none(),
353
+ secure=st.booleans() | st.none(),
354
+ value=st.text(),
355
+ )
356
+ ),
357
+ headers=st.lists(
358
+ st.builds(
359
+ harfile.Record,
360
+ comment=st.text() | st.none(),
361
+ name=st.text(),
362
+ value=st.text(),
363
+ )
364
+ ),
365
+ headersSize=st.integers(),
366
+ httpVersion=st.text(),
367
+ redirectURL=st.text(),
368
+ status=st.integers(),
369
+ statusText=st.text(),
370
+ ),
371
+ )
372
+ st.register_type_strategy(
373
+ harfile.Timings,
374
+ st.builds(
375
+ harfile.Timings,
376
+ blocked=st.floats() | st.integers(),
377
+ comment=st.text() | st.none(),
378
+ connect=st.floats() | st.integers(),
379
+ dns=st.floats() | st.integers(),
380
+ receive=st.floats() | st.integers(),
381
+ send=st.floats() | st.integers(),
382
+ ssl=st.floats() | st.integers(),
383
+ wait=st.floats() | st.integers(),
384
+ ),
385
+ )
386
+
387
+
388
+ ENTRY_STRATEGY = st.fixed_dictionaries(
389
+ {
390
+ "startedDateTime": st.datetimes(timezones=st.just(datetime.timezone.utc)),
391
+ "time": st.floats(min_value=0, allow_nan=False, allow_infinity=False) | st.integers(min_value=0),
392
+ "request": st.from_type(harfile.Request),
393
+ "response": st.from_type(harfile.Response),
394
+ "timings": st.from_type(harfile.Timings),
395
+ },
396
+ optional={
397
+ "cache": st.from_type(harfile.Cache) | st.none(),
398
+ "serverIPAddress": st.text() | st.none(),
399
+ "connection": st.text() | st.none(),
400
+ "comment": st.text() | st.none(),
401
+ },
402
+ )
403
+
404
+
405
+ def write_har(arg, entries):
406
+ with harfile.open(arg) as har:
407
+ for entry in entries:
408
+ har.add_entry(**entry)
409
+
410
+
411
+ @given(entries=st.lists(ENTRY_STRATEGY, max_size=10))
412
+ @settings(
413
+ phases=[Phase.reuse, Phase.generate, Phase.shrink],
414
+ suppress_health_check=[HealthCheck.function_scoped_fixture, HealthCheck.too_slow],
415
+ )
416
+ def test_write_har(entries, tmp_path):
417
+ buffer = io.StringIO()
418
+ write_har(buffer, entries)
419
+ har = buffer.getvalue()
420
+ HAR_VALIDATOR.validate(json.loads(har))
421
+ path = tmp_path / "test.har"
422
+ write_har(path, entries)
423
+ with open(path) as fd:
424
+ HAR_VALIDATOR.validate(json.load(fd))
425
+ write_har(path, entries)
426
+ with open(path) as fd:
427
+ HAR_VALIDATOR.validate(json.load(fd))
428
+
429
+
430
+ def test_close():
431
+ buffer = io.StringIO()
432
+ with harfile.open(buffer) as har:
433
+ har.close()
434
+
435
+
436
+ def test_exception():
437
+ buffer = io.StringIO()
438
+ with pytest.raises(ZeroDivisionError):
439
+ with harfile.open(buffer):
440
+ raise ZeroDivisionError
441
+ assert buffer.getvalue() == ""
442
+
443
+
444
+ def test_with_comments():
445
+ buffer = io.StringIO()
446
+ with harfile.open(
447
+ buffer,
448
+ creator=harfile.Creator(name="test", version="0.1", comment="EXAMPLE-1"),
449
+ browser=harfile.Browser(name="test", version="0.2", comment="EXAMPLE-2"),
450
+ ):
451
+ pass
452
+ assert (
453
+ buffer.getvalue()
454
+ == """{
455
+ "log": {
456
+ "version": "1.2",
457
+ "creator": {
458
+ "name": "test",
459
+ "version": "0.1",
460
+ "comment": "EXAMPLE-1"
461
+ },
462
+ "browser": {
463
+ "name": "test",
464
+ "version": "0.2",
465
+ "comment": "EXAMPLE-2"
466
+ },
467
+ "entries": []
468
+ }
469
+ }"""
470
+ )