Hi all,<div><br></div><div>In the process of adding more coverage to WebSocket For Python [1], I was looking at creating a mock of a socket class. The idea would then be to write a bunch of mocks that would simply hand me bytes at the pace I need whenever I call the recv(size) method on the mocked socket.</div>
<div><br></div><div>I'm not familiar enough with the complexity of mocking a built-in object like socket and I'm welcoming ideas or tools to achieve this.<br clear="all"><div><br></div><div>Here's a snippet of something I'd like, more or less:</div>
<div><br></div><div><div># -*- coding: utf-8 -*-</div><div>import socket</div><div>import unittest</div><div><br></div><div>from ws4py.websocket import WebSocket</div><div><br></div><div>class TestWebSocket(unittest.TestCase):</div>
<div> def setUp(self):</div><div> self.sock = MOCK_SOCKET_SOMEHOW(socket.socket)</div><div> <a href="http://self.ws">self.ws</a> = WebSocket(self.sock, None, None)</div><div><br></div><div> def tearDown(self):</div>
<div> self.sock.close()</div><div> self.sock = None</div><div><br></div><div>class TestFraming(TestWebSocket):</div><div> def test_case_1_1_1(self):</div><div> # Pretend next is a valid frame</div>
<div> bytes = "..." </div><div><br></div><div> # Need to replace recv so that it reads from bytes above</div><div> self.sock.recv = ...</div><div> # Run is a blocking method that blocks on recv()</div>
<div> self.sock.run()</div><div> </div></div><div><br></div><div>Cheers,</div>-- <br>- Sylvain<br><a href="http://www.defuze.org">http://www.defuze.org</a><br><a href="http://twitter.com/lawouach">http://twitter.com/lawouach</a><br>
</div><div><br></div><div>[1] <a href="https://github.com/Lawouach/WebSocket-for-Python">https://github.com/Lawouach/WebSocket-for-Python</a></div>