6
from keystoneclient import httpclient
7
from tests import utils
10
FAKE_RESPONSE = utils.TestResponse({
12
"text": '{"hi": "there"}',
14
MOCK_REQUEST = mock.Mock(return_value=(FAKE_RESPONSE))
18
cl = httpclient.HTTPClient(username="username", password="password",
19
tenant_id="tenant", auth_url="auth_test",
20
cacert="ca.pem", key="key.pem", cert="cert.pem")
24
def get_authed_client():
26
cl.management_url = "https://127.0.0.1:5000"
27
cl.auth_token = "token"
31
class ClientTest(utils.TestCase):
34
cl = get_authed_client()
36
with mock.patch.object(requests, "request", MOCK_REQUEST):
37
with mock.patch('time.time', mock.Mock(return_value=1234)):
38
resp, body = cl.get("/hi")
39
headers = {"X-Auth-Token": "token",
40
"User-Agent": httpclient.USER_AGENT}
41
kwargs = copy.copy(self.TEST_REQUEST_BASE)
42
kwargs['cert'] = ('cert.pem', 'key.pem')
43
kwargs['verify'] = 'ca.pem'
44
MOCK_REQUEST.assert_called_with(
46
"https://127.0.0.1:5000/hi",
49
# Automatic JSON parsing
50
self.assertEqual(body, {"hi": "there"})
53
cl = get_authed_client()
55
with mock.patch.object(requests, "request", MOCK_REQUEST):
56
cl.post("/hi", body=[1, 2, 3])
58
"X-Auth-Token": "token",
59
"Content-Type": "application/json",
60
"User-Agent": httpclient.USER_AGENT
62
kwargs = copy.copy(self.TEST_REQUEST_BASE)
63
kwargs['cert'] = ('cert.pem', 'key.pem')
64
kwargs['verify'] = 'ca.pem'
65
MOCK_REQUEST.assert_called_with(
67
"https://127.0.0.1:5000/hi",
72
def test_post_auth(self):
73
with mock.patch.object(requests, "request", MOCK_REQUEST):
74
cl = httpclient.HTTPClient(
75
username="username", password="password", tenant_id="tenant",
76
auth_url="auth_test", cacert="ca.pem", key="key.pem",
78
cl.management_url = "https://127.0.0.1:5000"
79
cl.auth_token = "token"
80
cl.post("/hi", body=[1, 2, 3])
82
"X-Auth-Token": "token",
83
"Content-Type": "application/json",
84
"User-Agent": httpclient.USER_AGENT
86
kwargs = copy.copy(self.TEST_REQUEST_BASE)
87
kwargs['cert'] = ('cert.pem', 'key.pem')
88
kwargs['verify'] = 'ca.pem'
89
MOCK_REQUEST.assert_called_with(
91
"https://127.0.0.1:5000/hi",