def test_recv(self, ws_create_connection, recv_mock): send_ack = {"headers": {"status": 200}} recv_mock.side_effect = [ send_ack, send_ack, send_ack, { "body": { "payload": "foo" } }, send_ack ] transport = ws.WebsocketTransport(self.options) req = request.Request(self.endpoint) transport.send(req) count = 0 while True: count += 1 data = transport.recv() if 'body' in data: self.assertEqual(data['body']['payload'], 'foo') break if count >= 4: self.fail('Failed to receive expected message.')
def test_basic_send(self): params = {'name': 'Test', 'address': 'Outer space'} req = request.Request('http://example.org/', operation='test_operation', params=params) with mock.patch.object(self.transport.client, 'request', autospec=True) as request_method: resp = prequest.Response() request_method.return_value = resp # NOTE(flaper87): Bypass the API # loading step by setting the _api # attribute req._api = self.api self.transport.send(req) final_url = 'http://example.org/v1/test/Test' final_params = {'address': 'Outer space'} final_headers = {'content-type': 'application/json'} request_method.assert_called_with('GET', url=final_url, params=final_params, headers=final_headers, data=None)
def test_send_without_api(self, mock_stream): params = {'name': 'Test', 'address': 'Outer space'} req = request.Request('http://example.org/', operation='test_operation', params=params) with mock.patch.object(self.transport.client, 'request', autospec=True) as request_method: resp = prequest.Response() raw = response.HTTPResponse() resp.raw = raw request_method.return_value = resp self.transport.send(req) final_url = 'http://example.org/' final_headers = {'content-type': 'application/json'} request_method.assert_called_with('GET', url=final_url, params=params, headers=final_headers, data=None, verify=True, cert=None)
def test_queue_delete(self): with mock.patch.object(self.transport, 'send', autospec=True) as send_method: send_method.return_value = response.Response(None, None) req = request.Request() core.queue_delete(self.transport, req, 'test') self.assertIn('queue_name', req.params)
def test_health(self): with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, None) send_method.return_value = resp req = request.Request() core.health(self.transport, req)
def test_get_queue_metadata(self): with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, '{}') send_method.return_value = resp req = request.Request() core.queue_get_metadata(self.transport, req, 'test')
def test_queue_get_stats(self): with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, '{}') send_method.return_value = resp req = request.Request() result = core.queue_get_stats(self.transport, req, 'test') self.assertEqual({}, result)
def test_message_delete(self): with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, None) send_method.return_value = resp req = request.Request() core.message_delete(self.transport, req, 'test', 'message_id')
def test_set_queue_metadata(self): update_data = {'some': 'data'} with mock.patch.object(self.transport, 'send', autospec=True) as send_method: send_method.return_value = response.Response(None, None) req = request.Request() core.queue_exists(self.transport, req, update_data, 'test') self.assertIn('queue_name', req.params)
def test_pool_create(self): with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, None) send_method.return_value = resp req = request.Request() core.pool_create(self.transport, req, 'test_pool', {'uri': 'sqlite://', 'weight': 0})
def test_message_list(self): with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, '{}') send_method.return_value = resp req = request.Request() core.message_list(self.transport, req, 'test') self.assertIn('queue_name', req.params)
def test_queue_exists_not_found(self): with mock.patch.object(self.transport, 'send', autospec=True) as send_method: send_method.side_effect = errors.ResourceNotFound req = request.Request() ret = core.queue_exists(self.transport, req, 'test') self.assertIn('queue_name', req.params) self.assertFalse(ret)
def test_make_client(self, ws_create_connection): ws_create_connection.return_value.recv.return_value = json.dumps( {"headers": { "status": 200 }}) transport = ws.WebsocketTransport(self.options) req = request.Request(self.endpoint) transport.send(req) ws_create_connection.assert_called_with("ws://127.0.0.1:9000")
def test_message_post_one(self): messages = {'ttl': 30, 'body': 'Post one!'} with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, '{}') send_method.return_value = resp req = request.Request() core.message_post(self.transport, req, 'test', messages) self.assertIn('queue_name', req.params) self.assertEqual(messages, json.loads(req.content))
def test_no_token(self): test_endpoint = 'http://example.org:8888' with mock.patch.object(ksclient, 'Client', new_callable=lambda: _FakeKeystoneClient): with mock.patch.object(self.auth, '_get_endpoint') as get_endpoint: get_endpoint.return_value = test_endpoint req = self.auth.authenticate(1, request.Request()) self.assertEqual(req.endpoint, test_endpoint) self.assertIn('X-Auth-Token', req.headers)
def test_message_delete_many(self): with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, None) send_method.return_value = resp ids = ['a', 'b'] req = request.Request() core.message_delete_many(self.transport, req, 'test', ids=ids) self.assertIn('queue_name', req.params) self.assertIn('ids', req.params) self.assertEqual(ids, req.params['ids'])
def test_no_token(self, fake_session): test_endpoint = 'http://example.org:8888' keystone_session = session.Session() with mock.patch.object(self.auth, '_get_endpoint') as get_endpoint: with mock.patch.object(self.auth, '_get_keystone_session') as get_session: get_endpoint.return_value = test_endpoint get_session.return_value = keystone_session req = self.auth.authenticate(1, request.Request()) self.assertEqual(test_endpoint, req.endpoint) self.assertIn('X-Auth-Token', req.headers) self.assertIn(req.headers['X-Auth-Token'], 'fake-token')
def _init_client(self, endpoint): """Initialize a websocket transport client. :param endpoint: The websocket endpoint. Example: ws://127.0.0.1:9000/. Required. :type endpoint: string """ self._websocket_client_id = uuidutils.generate_uuid() LOG.debug('Instantiating messaging websocket client: %s', endpoint) self._ws = self._create_connection(endpoint) auth_req = request.Request(endpoint, 'authenticate', headers={'X-Auth-Token': self._token}) self.send(auth_req)
def test_message_list_kwargs(self): with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, '{}') send_method.return_value = resp req = request.Request() core.message_list(self.transport, req, 'test', marker='supermarket', echo=False, limit=10) self.assertIn('queue_name', req.params) self.assertIn('limit', req.params) self.assertIn('echo', req.params) self.assertIn('marker', req.params)
def test_error_handling(self): params = {'name': 'Opportunity', 'address': 'NASA'} req = request.Request('http://example.org/', operation='test_operation', params=params) with mock.patch.object(self.transport.client, 'request', autospec=True) as request_method: exception_iterator = self.transport.http_to_zaqar.items() for response_code, exception in exception_iterator: resp = prequest.Response() resp.status_code = response_code request_method.return_value = resp self.assertRaises(exception, lambda: self.transport.send(req))
def _create_req(self, endpoint, action, body): return request.Request(endpoint, action, content=json.dumps(body))
def test_with_token(self): self.config(os_auth_token='test-token') req = request.Request(endpoint='http://example.org:8888') req = self.auth.authenticate(1, req) self.assertIn('X-Auth-Token', req.headers) self.assertIn(req.headers['X-Auth-Token'], 'test-token')