/usr/lib/python3/dist-packages/seamicroclient/tests/test_http.py is in python3-seamicroclient 0.4.0-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | #
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import json
import mock
import requests
from seamicroclient import client
from seamicroclient import exceptions
from seamicroclient.tests import utils
fake_response = utils.TestResponse({
"status_code": 200,
"text": '{"hi": "there"}',
})
mock_request = mock.Mock(return_value=(fake_response))
refused_response = utils.TestResponse({
"status_code": 400,
"text": '[Errno 111] Connection refused',
})
refused_mock_request = mock.Mock(return_value=(refused_response))
bad_req_response = utils.TestResponse({
"status_code": 400,
"text": '',
})
bad_req_mock_request = mock.Mock(return_value=(bad_req_response))
def get_client():
cl = client.HTTPClient("username", "password", "http://example.com")
return cl
def get_authed_client():
cl = get_client()
cl.auth_url = "http://example.com"
cl.auth_token = "token"
cl.user = "user"
cl.password = "password"
return cl
class ClientTest(utils.TestCase):
def test_get(self):
cl = get_authed_client()
@mock.patch.object(requests.Session, "request", mock_request)
@mock.patch('time.time', mock.Mock(return_value=1234))
def test_get_call():
resp, body = cl.get("/hi")
headers = {'Accept': 'application/json',
'User-Agent': 'python-seamicroclient'}
mock_request.assert_called_with(
"GET",
"http://example.com/hi?username=%s&password=%s" % (cl.user,
cl.password),
headers=headers)
# Automatic JSON parsing
self.assertEqual(body, {"hi": "there"})
test_get_call()
def test_post(self):
cl = get_authed_client()
@mock.patch.object(requests.Session, "request", mock_request)
def test_post_call():
body = {'k1': 'v1', 'k2': 'v2', 'authtoken': cl.auth_token}
cl.post("/hi", body=body)
headers = {'Content-Type': 'application/json', 'Accept':
'application/json',
'User-Agent': 'python-seamicroclient'}
mock_request.assert_called_with(
"POST",
"http://example.com/hi",
headers=headers,
data=json.dumps(body))
test_post_call()
def test_connection_refused(self):
cl = get_client()
@mock.patch.object(requests.Session, "request", refused_mock_request)
def test_refused_call():
self.assertRaises(exceptions.ConnectionRefused, cl.get, "/hi")
test_refused_call()
def test_bad_request(self):
cl = get_client()
@mock.patch.object(requests.Session, "request", bad_req_mock_request)
def test_refused_call():
self.assertRaises(exceptions.BadRequest, cl.get, "/hi")
test_refused_call()
def test_client_logger(self):
cl1 = client.HTTPClient("username", "password",
"http://example.com",
http_log_debug=True)
self.assertEqual(len(cl1._logger.handlers), 1)
cl2 = client.HTTPClient("username", "password",
"http://example.com",
http_log_debug=True)
self.assertEqual(len(cl2._logger.handlers), 1)
|