| 1 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 |
# not use this file except in compliance with the License. You may obtain |
| 3 |
# a copy of the License at |
| 4 |
# |
| 5 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
# |
| 7 |
# Unless required by applicable law or agreed to in writing, software |
| 8 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 |
# License for the specific language governing permissions and limitations |
| 11 |
# under the License. |
| 12 |
|
| 13 |
import json |
| 14 |
from unittest import mock |
| 15 |
|
| 16 |
from zaqarclient.tests import base |
| 17 |
from zaqarclient.transport import request |
| 18 |
from zaqarclient.transport import ws |
| 19 |
|
| 20 |
|
| 21 |
class TestWsTransport(base.TestBase): |
| 22 |
|
| 23 |
def setUp(self): |
| 24 |
super().setUp() |
| 25 |
os_opts = { |
| 26 |
'os_auth_token': 'FAKE_TOKEN', |
| 27 |
'os_auth_url': 'http://127.0.0.0:5000/v3', |
| 28 |
'os_project_id': 'admin', |
| 29 |
'os_service_type': 'messaging-websocket', |
| 30 |
} |
| 31 |
auth_opts = {'backend': 'keystone', |
| 32 |
'options': os_opts} |
| 33 |
self.options = {'auth_opts': auth_opts} |
| 34 |
self.endpoint = 'ws://127.0.0.1:9000' |
| 35 |
|
| 36 |
@mock.patch.object(ws.WebsocketTransport, "_create_connection") |
| 37 |
def test_make_client(self, ws_create_connection): |
| 38 |
ws_create_connection.return_value.recv.return_value = json.dumps({ |
| 39 |
"headers": { |
| 40 |
"status": 200 |
| 41 |
} |
| 42 |
}) |
| 43 |
|
| 44 |
transport = ws.WebsocketTransport(self.options) |
| 45 |
req = request.Request(self.endpoint) |
| 46 |
transport.send(req) |
| 47 |
ws_create_connection.assert_called_with("ws://127.0.0.1:9000") |
| 48 |
|
| 49 |
@mock.patch.object(ws.WebsocketTransport, "recv") |
| 50 |
@mock.patch.object(ws.WebsocketTransport, "_create_connection") |
| 51 |
def test_recv(self, ws_create_connection, recv_mock): |
| 52 |
|
| 53 |
send_ack = { |
| 54 |
"headers": { |
| 55 |
"status": 200 |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
recv_mock.side_effect = [send_ack, send_ack, send_ack, { |
| 60 |
"body": { |
| 61 |
"payload": "foo" |
| 62 |
} |
| 63 |
}, send_ack] |
| 64 |
|
| 65 |
transport = ws.WebsocketTransport(self.options) |
| 66 |
req = request.Request(self.endpoint) |
| 67 |
transport.send(req) |
| 68 |
|
| 69 |
count = 0 |
| 70 |
|
| 71 |
while True: |
| 72 |
count += 1 |
| 73 |
data = transport.recv() |
| 74 |
if 'body' in data: |
| 75 |
self.assertEqual(data['body']['payload'], 'foo') |
| 76 |
break |
| 77 |
if count >= 4: |
| 78 |
self.fail('Failed to receive expected message.') |