How to use the mocket.mocket.MocketEntry function in mocket

To help you get started, we’ve selected a few mocket examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mindflayer / python-mocket / tests / test_mocket.py View on Github external
def test_register(self):
        entry_1 = MocketEntry(('localhost', 80), True)
        entry_2 = MocketEntry(('localhost', 80), True)
        entry_3 = MocketEntry(('localhost', 8080), True)
        Mocket.register(entry_1, entry_2, entry_3)
        self.assertEqual(Mocket._entries, {
            ('localhost', 80): [entry_1, entry_2],
            ('localhost', 8080): [entry_3],
        })
github ampledata / kiss / tests / test_tcpkiss.py View on Github external
def test_write_and_read(self):
        """Tests writing-to and reading-from TCP Host."""
        frame = "%s>%s:%s" % (
            self.random(6),
            ','.join([self.random(6), self.random(6), self.random(6)]),
            ' '.join([
                self.random(), 'test_write_and_read', self.random()])
        )
        aprs_frame = aprs.Frame(frame)
        kiss_frame = aprs_frame.encode_kiss()

        ks = kiss.TCPKISS(host=self.random_host, port=self.random_port)
        a = (self.random_host, self.random_port)

        entry = MocketEntry(a, [kiss_frame])
        entry_1 = MocketEntry(('localhost', 80), True)
        Mocket.register(entry)

        ks.interface = create_connection(a)
        ks._write_handler = ks.interface.sendall

        def _pass(): pass
        ks.stop = _pass

        ks.write(kiss_frame)

        _read_data = ks.read(len(kiss_frame), readmode=False)

        self._logger.info(
            '_read_data(%s)="%s"', len(_read_data), _read_data)
github mindflayer / python-mocket / tests / test_mocket.py View on Github external
def test_register(self):
        entry_1 = MocketEntry(('localhost', 80), True)
        entry_2 = MocketEntry(('localhost', 80), True)
        entry_3 = MocketEntry(('localhost', 8080), True)
        Mocket.register(entry_1, entry_2, entry_3)
        self.assertEqual(Mocket._entries, {
            ('localhost', 80): [entry_1, entry_2],
            ('localhost', 8080): [entry_3],
        })
github ampledata / kiss / tests / test_tcpkiss.py View on Github external
def _test_write(self):
        frame = "%s>%s:%s" % (
            self.random(6),
            ','.join([self.random(6), self.random(6), self.random(6)]),
            ' '.join([
                self.random(), 'test_write', self.random()])
        )
        aprs_frame = aprs.Frame(frame)
        kiss_frame = aprs_frame.encode_kiss()

        ks = kiss.TCPKISS(host=self.random_host, port=self.random_port)
        a = (self.random_host, self.random_port)

        entry = MocketEntry(a, kiss_frame)
        Mocket.register(entry)
        self._logger.debug(a)
        self._logger.debug(entry.get_response())

        ks.interface = create_connection(a)
        ks._write_handler = ks.interface.sendall

        def _pass(): pass

        ks.stop = _pass

        ks.write(kiss_frame)
github mindflayer / python-mocket / tests / test_mocket.py View on Github external
def test_getentry(self):
        entry = MocketEntry(('localhost', 80), True)
        Mocket.register(entry)
        self.assertEqual(Mocket.get_entry('localhost', 80, True), entry)
github ampledata / kiss / tests / test_tcpkiss.py View on Github external
def test_write_and_read(self):
        """Tests writing-to and reading-from TCP Host."""
        frame = "%s>%s:%s" % (
            self.random(6),
            ','.join([self.random(6), self.random(6), self.random(6)]),
            ' '.join([
                self.random(), 'test_write_and_read', self.random()])
        )
        aprs_frame = aprs.Frame(frame)
        kiss_frame = aprs_frame.encode_kiss()

        ks = kiss.TCPKISS(host=self.random_host, port=self.random_port)
        a = (self.random_host, self.random_port)

        entry = MocketEntry(a, [kiss_frame])
        entry_1 = MocketEntry(('localhost', 80), True)
        Mocket.register(entry)

        ks.interface = create_connection(a)
        ks._write_handler = ks.interface.sendall

        def _pass(): pass
        ks.stop = _pass

        ks.write(kiss_frame)

        _read_data = ks.read(len(kiss_frame), readmode=False)

        self._logger.info(
            '_read_data(%s)="%s"', len(_read_data), _read_data)
github mindflayer / python-mocket / tests / test_mocket.py View on Github external
def test_register(self):
        entry_1 = MocketEntry(('localhost', 80), True)
        entry_2 = MocketEntry(('localhost', 80), True)
        entry_3 = MocketEntry(('localhost', 8080), True)
        Mocket.register(entry_1, entry_2, entry_3)
        self.assertEqual(Mocket._entries, {
            ('localhost', 80): [entry_1, entry_2],
            ('localhost', 8080): [entry_3],
        })
github mindflayer / python-mocket / mocket / mockredis.py View on Github external
    @staticmethod
    def command(description, _type='+'):
        return Redisizer('{0}{1}{2}'.format(_type, description, '\r\n').encode('utf-8'))

    @staticmethod
    def error(description):
        return Redisizer.command(description, _type='-')


OK = Redisizer.command('OK')
QUEUED = Redisizer.command('QUEUED')
ERROR = Redisizer.error


class Entry(MocketEntry):
    request_cls = Request
    response_cls = Response

    def __init__(self, addr, command, responses):
        super(Entry, self).__init__(addr or ('localhost', 6379), responses)
        d = shsplit(command)
        d[0] = d[0].upper()
        self.command = Redisizer.tokens(d)

    def can_handle(self, data):
        return data.splitlines() == self.command

    @classmethod
    def register(cls, addr, command, *responses):
        responses = [cls.response_cls(r) for r in responses]
        Mocket.register(cls(addr, command, responses))
github mindflayer / python-mocket / mocket / mockhttp.py View on Github external
'Date': time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime()),
            'Server': 'Python/Mocket',
            'Connection': 'close',
            'Content-Length': str(len(self.body)),
        }
        if not self.is_file_object:
            self.headers['Content-Type'] = 'text/plain; charset=utf-8'
        elif self.magic:
            self.headers['Content-Type'] = decode_from_bytes(magic.from_buffer(self.body, mime=True))

    def set_extra_headers(self, headers):
        for k, v in headers.items():
            self.headers['-'.join([token.capitalize() for token in k.split('-')])] = v


class Entry(MocketEntry):
    CONNECT = 'CONNECT'
    DELETE = 'DELETE'
    GET = 'GET'
    HEAD = 'HEAD'
    OPTIONS = 'OPTIONS'
    PATCH = 'PATCH'
    POST = 'POST'
    PUT = 'PUT'
    TRACE = 'TRACE'

    METHODS = (CONNECT, DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT, TRACE)

    request_cls = Request
    response_cls = Response

    def __init__(self, uri, method, responses, match_querystring=True):
github lyft / amundsenmetadatalibrary / metadata_service / proxy / aws4authwebsocket / transport.py View on Github external
def monkey_patch_mocket() -> None:
    # monkey patch this method since the existing behavior is really broken
    T = TypeVar('T', bound=mocket.mocket.MocketSocket)

    def monkey_fileno(self: T) -> int:
        if mocket.mocket.Mocket.r_fd is None and mocket.mocket.Mocket.w_fd is None:
            mocket.mocket.Mocket.r_fd, mocket.mocket.Mocket.w_fd = os.pipe()
        return mocket.mocket.Mocket.r_fd

    mocket.mocket.MocketSocket.fileno = monkey_fileno


monkey_patch_mocket()


class SelfRecordingWebSocketEntry(mocket.mocket.MocketEntry):
    """
    This is like  mocket.mockhttp.Entry, but it is less picky about the data and records locally.
    """
    request_cls = mocket.mockhttp.Request
    response_cls = mocket.mockhttp.Response

    def __init__(self, host: str, port: int, responses: Any) -> None:
        self.host: str = host
        self.port: int = port
        self._data: List[bytes] = []
        self._data_lock = threading.Lock()
        super(SelfRecordingWebSocketEntry, self).__init__(location=(host, port), responses=responses)

    @overrides
    def collect(self, data: bytes) -> None:
        with self._data_lock: