|
4 | 4 | from case import patch, call, Mock
|
5 | 5 | from amqp import spec, Connection, Channel, sasl, Message
|
6 | 6 | from amqp.platform import pack
|
7 |
| -from amqp.serialization import dumps |
| 7 | +from amqp.serialization import dumps, loads |
8 | 8 |
|
9 | 9 |
|
10 | 10 | def ret_factory(method, channel=0, args=b'', arg_format=None):
|
@@ -61,6 +61,19 @@ def ret_factory(method, channel=0, args=b'', arg_format=None):
|
61 | 61 | }
|
62 | 62 |
|
63 | 63 |
|
| 64 | +class DataComparator(object): |
| 65 | + # Comparator used for asserting serialized data. It can be used |
| 66 | + # in cases when direct comparision of bytestream cannot be used |
| 67 | + # (mainly cases of Table type where order of items can vary) |
| 68 | + def __init__(self, argsig, items): |
| 69 | + self.argsig = argsig |
| 70 | + self.items = items |
| 71 | + |
| 72 | + def __eq__(self, other): |
| 73 | + values, offset = loads(self.argsig, other) |
| 74 | + return tuple(values) == tuple(self.items) |
| 75 | + |
| 76 | + |
64 | 77 | def handshake(conn, transport_mock):
|
65 | 78 | # Helper function simulating connection handshake with server
|
66 | 79 | transport_mock().read_frame.side_effect = [
|
@@ -103,18 +116,25 @@ def test_connect(self):
|
103 | 116 | with patch.object(conn, 'Transport') as transport_mock:
|
104 | 117 | handshake(conn, transport_mock)
|
105 | 118 | on_open_mock.assert_called_once_with(conn)
|
| 119 | + security_mechanism = sasl.AMQPLAIN( |
| 120 | + 'guest', 'guest' |
| 121 | + ).start(conn).decode('utf-8', 'surrogatepass') |
| 122 | + |
106 | 123 | # Expected responses from client
|
107 | 124 | frame_writer_mock.assert_has_calls(
|
108 | 125 | [
|
109 | 126 | call(
|
110 |
| - 1, 0, spec.Connection.StartOk, dumps( |
| 127 | + 1, 0, spec.Connection.StartOk, |
| 128 | + # Due Table type, we cannot compare bytestream directly |
| 129 | + DataComparator( |
111 | 130 | 'FsSs',
|
112 | 131 | (
|
113 |
| - CLIENT_CAPABILITIES, b'AMQPLAIN', |
114 |
| - sasl.AMQPLAIN('guest', 'guest').start(conn), |
| 132 | + CLIENT_CAPABILITIES, 'AMQPLAIN', |
| 133 | + security_mechanism, |
115 | 134 | 'en_US'
|
116 | 135 | )
|
117 |
| - ), None |
| 136 | + ), |
| 137 | + None |
118 | 138 | ),
|
119 | 139 | call(
|
120 | 140 | 1, 0, spec.Connection.TuneOk,
|
|
0 commit comments