Skip to content

MocketEntry.request_class str vs bytes #175

@michael-lazar

Description

@michael-lazar

Hi, thanks for creating this awesome library!

I've just started using mocket to test raw socket connections and uncovered some weirdness with MocketEntry.request_cls. The default behavior is to cast the request data as a str, but this doesn't really make sense for python 3 because sockets use bytes. This results in the request data being mangled as a string. Here's an example

import socket

from mocket import Mocket, MocketEntry, Mocketizer

entry = MocketEntry(("example.com", 1965), "")
Mocket.register(entry)

with Mocketizer():
    with socket.create_connection(("example.com", 1965)) as sock:
        sock.sendall(b"example request data")

    request = Mocket.last_request()

    # I expect the request to contain the bytes that I sent, but it's actually a string object
    print(type(request), repr(request))
    # <class 'str'> "b'example request data'"

I suspect this is a holdout bug from the python 2 -> 3 conversion, and not the intended behavior. For now I've fixed it for myself by subclassing the mocket entry.

class RawMocketEntry(MocketEntry):
    request_cls = bytes

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions