FakePayload
A wrapper around BytesIO that restricts what can be read since data from the network can't be sought and cannot be read outside of its content length. This makes sure that views can't do anything under the test client that wouldn't work in real life.
Attributes
| Attribute | Type | Description |
|---|---|---|
| read_started | boolean = False | A boolean flag indicating whether a read operation has commenced, which prevents further write operations to the payload. |
| __content | BytesIO | A BytesIO buffer used to store the raw byte data of the payload. |
| __len | int = 0 | An integer tracking the remaining number of bytes available to be read from the payload. |
Constructor
Signature
def FakePayload(
initial_bytes: bytes = None
) - > null
Parameters
| Name | Type | Description |
|---|---|---|
| initial_bytes | bytes = None | The initial byte data to populate the payload with. |
Signature
def FakePayload(
initial_bytes: bytes = None
)
Parameters
| Name | Type | Description |
|---|---|---|
| initial_bytes | bytes = None | The initial byte content to populate the payload buffer. |
Methods
read()
@classmethod
def read(
size: int = -1
) - > bytes
Reads a specified number of bytes from the payload, ensuring the request does not exceed the available content length.
Parameters
| Name | Type | Description |
|---|---|---|
| size | int = -1 | The maximum number of bytes to read; defaults to the entire remaining content length. |
Returns
| Type | Description |
|---|---|
bytes | The byte string read from the internal buffer. |
readline()
@classmethod
def readline(
size: int = -1
) - > bytes
Reads a single line or a specific number of bytes from the payload buffer while enforcing content length limits.
Parameters
| Name | Type | Description |
|---|---|---|
| size | int = -1 | The maximum number of bytes to read for the line. |
Returns
| Type | Description |
|---|---|
bytes | The line of bytes read from the buffer. |
write()
@classmethod
def write(
b: bytes
) - > None
Appends byte data to the internal buffer before reading has commenced.
Parameters
| Name | Type | Description |
|---|---|---|
| b | bytes | The byte data or object to be converted to bytes and added to the payload. |
Returns
| Type | Description |
|---|---|
None | null |