Skip to main content

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

AttributeTypeDescription
read_startedboolean = FalseA boolean flag indicating whether a read operation has commenced, which prevents further write operations to the payload.
__contentBytesIOA BytesIO buffer used to store the raw byte data of the payload.
__lenint = 0An integer tracking the remaining number of bytes available to be read from the payload.

Constructor

Signature

def FakePayload(
initial_bytes: bytes = None
) - > null

Parameters

NameTypeDescription
initial_bytesbytes = NoneThe initial byte data to populate the payload with.

Signature

def FakePayload(
initial_bytes: bytes = None
)

Parameters

NameTypeDescription
initial_bytesbytes = NoneThe 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

NameTypeDescription
sizeint = -1The maximum number of bytes to read; defaults to the entire remaining content length.

Returns

TypeDescription
bytesThe 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

NameTypeDescription
sizeint = -1The maximum number of bytes to read for the line.

Returns

TypeDescription
bytesThe 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

NameTypeDescription
bbytesThe byte data or object to be converted to bytes and added to the payload.

Returns

TypeDescription
Nonenull