def test_socket_as_context_manager(self): addr = ("localhost", 80) Mocket.register(MocketEntry(addr, ["Show me.\r\n"])) with Mocketizer(): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as _so: _so.connect(addr) _so.sendall(encode_to_bytes("Whatever...")) data = _so.recv(4096) self.assertEqual(data, encode_to_bytes("Show me.\r\n"))
def test_makefile(self): addr = ("localhost", 80) Mocket.register(MocketEntry(addr, ["Show me.\r\n"])) with Mocketizer(): _so = socket.socket(socket.AF_INET, socket.SOCK_STREAM) _so.connect(addr) fp = _so.makefile("rb") _so.sendall(encode_to_bytes("...\r\n")) self.assertEqual(fp.read().strip(), encode_to_bytes("Show me.")) self.assertEqual(len(Mocket.request_list()), 1)
def __getattr__(self, name): if name == "last_request": last_request = getattr(Mocket, "last_request")() last_request.body = encode_to_bytes(last_request.body) return last_request elif name == "latest_requests": return getattr(Mocket, "_requests") else: return getattr(Entry, name)
def __getattr__(self, name): if name == 'last_request': last_request = getattr(Mocket, 'last_request')() last_request.body = encode_to_bytes(last_request.body) return last_request elif name == 'latest_requests': return getattr(Mocket, '_requests') else: return getattr(Entry, name)
def __getattr__(self, name): if name == 'last_request': last_request = getattr(Mocket, 'last_request')() last_request.body = encode_to_bytes(last_request.body) return last_request elif name == 'latest_requests': return getattr(Mocket, '_requests') else: return getattr(Entry, name)
def test_empty_getresponse(self): entry = MocketEntry(('localhost', 8080), []) self.assertEqual(entry.get_response(), encode_to_bytes(''))
def test_getresponse(self): entry = MocketEntry(('localhost', 8080), ['Show me.\r\n']) self.assertEqual(entry.get_response(), encode_to_bytes('Show me.\r\n'))
def test_getresponse(self): entry = MocketEntry(("localhost", 8080), ["Show me.\r\n"]) self.assertEqual(entry.get_response(), encode_to_bytes("Show me.\r\n"))
def test_empty_getresponse(self): entry = MocketEntry(('localhost', 8080), []) self.assertEqual(entry.get_response(), encode_to_bytes(''))
def test_getresponse(self): entry = MocketEntry(('localhost', 8080), ['Show me.\r\n']) self.assertEqual(entry.get_response(), encode_to_bytes('Show me.\r\n'))