YAML Test Data Fixtures
Relevant source files
The following files were used as context for generating this wiki page:
YAML Test Data Fixtures serve as the descriptive metadata for the integration and regression testing suite of the python-multipart library. These files define the expected behavior and structural outcome of parsing raw HTTP request streams, allowing the test suite to validate that the MultipartParser and FormParser correctly identify boundaries, fields, and file attachments.
The fixtures are typically paired with raw .http files containing the byte streams to be parsed. The YAML component provides the ground truth, including the specific boundary string used in the request and a list of expected results that the parser should produce, such as field names, filenames, and binary data content.
Sources: tests/test_multipart.py:657-684, tests/test_data/http/single_file.yaml:1-7
Fixture Structure and Schema
Each YAML fixture follows a standardized schema that allows the test runner to programmatically verify parser output. The schema primary consists of a boundary definition and an expected list of objects.
Core Schema Components
| Field | Type | Description |
|---|---|---|
boundary | String | The unique boundary string used to separate parts in the multipart stream. |
expected | List | A collection of objects representing the fields or files expected after parsing. |
error | Integer | (Optional) The expected byte offset where a MultipartParseError should occur. |
Sources: tests/test_data/http/single_file.yaml:1-7, tests/test_multipart.py:734-738
Expected Object Properties
Objects within the expected list represent either a form field or a file upload.
| Property | Type | Description |
|---|---|---|
name | String | The form field name (from Content-Disposition). |
type | String | Either field or file. |
file_name | String | The original filename for file types. |
content_type | String | The MIME type (e.g., text/plain). |
data | Binary | The raw content of the part, often represented as !!binary. |
Sources: tests/test_data/http/multiple_files.yaml:1-12, tests/test_data/http/case_insensitive_headers.yaml:1-24
Data Flow in Test Execution
The test suite utilizes these YAML fixtures to drive parameterized tests. The TestFormParser class loads these files to verify the integrity of the streaming parser.
The diagram shows how the YAML fixture provides the configuration (boundary) and the validation criteria (expected results) for the raw HTTP data processing.
Sources: tests/test_multipart.py:657-684, tests/test_multipart.py:721-764
Specialized Fixture Use Cases
The fixtures cover various edge cases in multipart parsing, ranging from character encoding to boundary collisions.
Binary and UTF-8 Handling
Fixtures use YAML's binary tag (!!binary) to represent file contents that may contain non-printable characters. They also validate the parser's ability to handle internationalized filenames in the Content-Disposition header.
boundary: ----WebKitFormBoundaryI9SCEFp2lpx5DR2K
expected:
- name: file
type: file
file_name: ???.txt
content_type: 'text/plain'
data: !!binary |
44GT44KM44Gv44OG44K544OI44Gn44GZ44CC
Sources: tests/test_data/http/utf8_filename.yaml:1-7, tests/test_data/http/single_file.yaml:1-7
Boundary Collision Testing
The almost_match_boundary.yaml fixture is used to test the parser's robustness against data that resembles a boundary but is not one. This ensures the parser's state machine correctly distinguishes between partial matches and actual delimiters.
Sources: tests/test_data/http/almost_match_boundary.yaml:1-12, tests/test_multipart.py:821-825
Header Case-Insensitivity
Fixtures like case_insensitive_headers.yaml ensure that the parser correctly handles headers regardless of casing (e.g., Content-Type vs content-type), which is a requirement for HTTP compliance.
Sources: tests/test_data/http/case_insensitive_headers.yaml:1-24, python_multipart/multipart.py:1260-1262
Integration with Test Runner
The test runner in tests/test_multipart.py iterates through the tests/test_data/http/ directory, pairing .http and .yaml files by name.
# Load both.
with open(os.path.join(http_tests_dir, f), "rb") as fh:
test_data = fh.read()
with open(yaml_file, "rb") as fy:
yaml_data = yaml.safe_load(fy)
http_tests.append({"name": fname, "test": test_data, "result": yaml_data})
Sources: tests/test_multipart.py:677-684
Summary
YAML Test Data Fixtures are a critical component of the python-multipart quality assurance pipeline. By decoupling the test logic from the test data, the project maintains a clear and extensible way to verify the streaming parser against complex multipart structures, including multiple files, binary data, and various boundary conditions.
Sources: tests/test_multipart.py:721-764, tests/test_data/http/multiple_files.yaml:1-12