Make the body bytes with associated headers. Arguments: data: Raw data to send in the request body. form: Key value paired data to send form encoded in the request body. files: Key FileStorage paired data to send as file encoded in the request bod
(
*,
data: AnyStr | None = None,
form: dict | None = None,
files: dict[str, FileStorage] | None = None,
json: Any = sentinel,
app: Quart | None = None,
)
| 79 | |
| 80 | |
| 81 | def make_test_body_with_headers( |
| 82 | *, |
| 83 | data: AnyStr | None = None, |
| 84 | form: dict | None = None, |
| 85 | files: dict[str, FileStorage] | None = None, |
| 86 | json: Any = sentinel, |
| 87 | app: Quart | None = None, |
| 88 | ) -> tuple[bytes, Headers]: |
| 89 | """Make the body bytes with associated headers. |
| 90 | |
| 91 | Arguments: |
| 92 | data: Raw data to send in the request body. |
| 93 | form: Key value paired data to send form encoded in the |
| 94 | request body. |
| 95 | files: Key FileStorage paired data to send as file |
| 96 | encoded in the request body. |
| 97 | json: Data to send json encoded in the request body. |
| 98 | |
| 99 | """ |
| 100 | if [json is not sentinel, form is not None, data is not None].count(True) > 1: |
| 101 | raise ValueError( |
| 102 | "Quart test args 'json', 'form', and 'data' are mutually exclusive" |
| 103 | ) |
| 104 | if [json is not sentinel, files is not None, data is not None].count(True) > 1: |
| 105 | raise ValueError( |
| 106 | "Quart test args 'files', 'json', and 'data' are mutually exclusive" |
| 107 | ) |
| 108 | |
| 109 | request_data = b"" |
| 110 | |
| 111 | headers = Headers() |
| 112 | |
| 113 | if isinstance(data, str): |
| 114 | request_data = data.encode("utf-8") |
| 115 | elif isinstance(data, bytes): |
| 116 | request_data = data |
| 117 | |
| 118 | if json is not sentinel: |
| 119 | request_data = dumps(json).encode("utf-8") |
| 120 | headers["Content-Type"] = "application/json" |
| 121 | elif files is not None: |
| 122 | boundary = "----QuartBoundary" |
| 123 | headers["Content-Type"] = f"multipart/form-data; boundary={boundary}" |
| 124 | encoder = MultipartEncoder(boundary.encode()) |
| 125 | request_data += encoder.send_event(Preamble(data=b"")) |
| 126 | for key, file_storage in files.items(): |
| 127 | request_data += encoder.send_event( |
| 128 | File( |
| 129 | name=key, |
| 130 | filename=file_storage.filename, |
| 131 | headers=file_storage.headers, |
| 132 | ) |
| 133 | ) |
| 134 | chunk = file_storage.read(16384) |
| 135 | while chunk != b"": |
| 136 | request_data += encoder.send_event(Data(data=chunk, more_data=True)) |
| 137 | chunk = file_storage.read(16384) |
| 138 | request_data += encoder.send_event(Data(data=b"", more_data=False)) |
searching dependent graphs…