def test_tls_unsupported(self): '''Raises an exception if the server does not support TLS''' res = response.Response(self.connection, response.Response.FRAME_TYPE, json.dumps({'tls_v1': False})) options = {'tls_v1': True} with mock.patch.object(self.connection, '_identify_options', options): self.assertRaises(exceptions.UnsupportedException, self.connection.identified, res)
def test_auth_required_provided(self): '''Sends the auth message if required and provided''' res = response.Response(self.connection, response.Response.FRAME_TYPE, json.dumps({'auth_required': True})) with mock.patch.object(self.connection, 'auth') as mock_auth: with mock.patch.object(self.connection, '_auth_secret', 'hello'): self.connection.identified(res) mock_auth.assert_called_with('hello')
def test_auth_provided_not_required(self): '''Logs a warning if you provide auth when none is required''' res = response.Response(self.connection, response.Response.FRAME_TYPE, json.dumps({'auth_required': False})) with mock.patch('nsq.connection.logger') as mock_logger: with mock.patch.object(self.connection, '_auth_secret', 'hello'): self.connection.identified(res) mock_logger.warn.assert_called_with( 'Authentication secret provided but not required')
def test_skip_non_messages(self): '''Skips all non-messages''' iterator = iter(self.client) message_id = uuid.uuid4().hex[0:16] packed = response.Message.pack(0, 0, message_id, 'hello') messages = [response.Message(None, None, packed) for _ in range(10)] packed = response.Response.pack('hello') responses = [ response.Response(None, None, packed) for _ in range(10)] + messages with mock.patch.object(self.client, 'read', return_value=responses): found = [iterator.next() for _ in range(10)] self.assertEqual(messages, found)
def test_auth_required_not_provided(self): '''Raises an exception if auth is required but not provided''' res = response.Response(self.connection, response.Response.FRAME_TYPE, json.dumps({'auth_required': True})) self.assertRaises(exceptions.UnsupportedException, self.connection.identified, res)
def test_ok_response(self): '''Sets our _identify_response to {} if 'OK' is provided''' res = response.Response(self.connection, response.Response.FRAME_TYPE, 'OK') self.connection.identified(res) self.assertEqual(self.connection._identify_response, {})
def test_read_multiple(self): '''Returns multiple responses if available''' self.socket.write(response.Response.pack('hello') * 10) expected = response.Response(self.connection, constants.FRAME_TYPE_RESPONSE, 'hello') self.assertEqual(self.connection.read(), [expected] * 10)
def test_read_whole(self): '''Returns a single message if it has read a complete one''' self.socket.write(response.Response.pack('hello')) expected = response.Response(self.connection, constants.FRAME_TYPE_RESPONSE, 'hello') self.assertEqual(self.connection.read(), [expected])
def response(self, message): self._responses.append( response.Response(self, response.Response.FRAME_TYPE, message))