Ejemplo n.º 1
0
    def test_passed_cert_to_verify_cert(self, mock_request):
        client = http.HTTPClient('https://foo', ca_file="NOWHERE")
        self.assertEqual("NOWHERE", client.verify_cert)

        with mock.patch('pankoclient.common.http.get_system_ca_file') as gsf:
            gsf.return_value = "SOMEWHERE"
            client = http.HTTPClient('https://foo')
            self.assertEqual("SOMEWHERE", client.verify_cert)
Ejemplo n.º 2
0
    def test_http_json_request_redirect_get(self, mock_request):
        # Record the 302
        mock_request.side_effect = [
            fakes.FakeHTTPResponse(302, 'Found',
                                   {'location': 'http://example.com:6688'},
                                   ''),
            fakes.FakeHTTPResponse(200, 'OK',
                                   {'Content-Type': 'application/json'}, '{}')
        ]

        client = http.HTTPClient('http://example.com:6688')
        resp, body = client.json_request('GET', '')
        self.assertEqual(200, resp.status_code)
        self.assertEqual({}, body)

        mock_request.assert_has_calls([
            mock.call('GET',
                      'http://example.com:6688',
                      allow_redirects=False,
                      headers={
                          'Content-Type': 'application/json',
                          'Accept': 'application/json',
                          'User-Agent': 'python-pankoclient'
                      }),
            mock.call('GET',
                      'http://example.com:6688',
                      allow_redirects=False,
                      headers={
                          'Content-Type': 'application/json',
                          'Accept': 'application/json',
                          'User-Agent': 'python-pankoclient'
                      })
        ])
Ejemplo n.º 3
0
    def test_http_json_request_argument_passed_to_requests(self, mock_request):
        """Check that we have sent the proper arguments to requests."""
        # Record a 200
        mock_request.return_value = fakes.FakeHTTPResponse(
            200, 'OK', {'Content-Type': 'application/json'}, '{}')

        client = http.HTTPClient('http://example.com:6688')
        client.verify_cert = True
        client.cert_file = 'RANDOM_CERT_FILE'
        client.key_file = 'RANDOM_KEY_FILE'
        client.auth_url = osc_fakes.AUTH_URL
        resp, body = client.json_request('POST', '', data='text')
        self.assertEqual(200, resp.status_code)
        self.assertEqual({}, body)

        mock_request.assert_called_once_with(
            'POST',
            'http://example.com:6688',
            allow_redirects=False,
            cert=('RANDOM_CERT_FILE', 'RANDOM_KEY_FILE'),
            verify=True,
            data='"text"',
            headers={
                'Content-Type': 'application/json',
                'Accept': 'application/json',
                'X-Auth-Url': osc_fakes.AUTH_URL,
                'User-Agent': 'python-pankoclient'
            })
Ejemplo n.º 4
0
    def test_methods(self, mock_request):
        fake = fakes.FakeHTTPResponse(200, 'OK',
                                      {'Content-Type': 'application/json'},
                                      '{}')
        mock_request.return_value = fake

        client = http.HTTPClient('http://example.com:6688')
        methods = [
            client.get, client.put, client.post, client.patch, client.delete,
            client.head
        ]
        for method in methods:
            resp, body = method('')
            self.assertEqual(200, resp.status_code)
Ejemplo n.º 5
0
    def test_http_request_socket_timeout(self, mock_request):
        headers = {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
            'User-Agent': 'python-pankoclient'
        }
        mock_request.side_effect = [socket.timeout]

        client = http.HTTPClient('http://example.com:6688')
        self.assertRaises(exc.ConnectionError, client.json_request, "GET", "/")
        mock_request.assert_called_once_with('GET',
                                             'http://example.com:6688/',
                                             allow_redirects=False,
                                             headers=headers)
Ejemplo n.º 6
0
 def test_http_raw_request(self, mock_request):
     headers = {
         'User-Agent': 'python-pankoclient',
         'Content-Type': 'application/octet-stream'
     }
     mock_request.return_value = fakes.FakeHTTPResponse(200, 'OK', {}, '')
     client = http.HTTPClient('http://example.com:6688')
     resp, body = client.raw_request('GET', '/prefix')
     self.assertEqual(200, resp.status_code)
     self.assertEqual('', ''.join([x for x in resp.content]))
     mock_request.assert_called_once_with('GET',
                                          'http://example.com:6688/prefix',
                                          allow_redirects=False,
                                          headers=headers)
Ejemplo n.º 7
0
    def test_fake_json_request(self, mock_request):
        headers = {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
            'User-Agent': 'python-pankoclient'
        }
        mock_request.side_effect = [socket.gaierror]

        client = http.HTTPClient('fake://example.com:6688')
        self.assertRaises(exc.EndpointNotFound, client.json_request, "GET",
                          "/")
        mock_request.assert_called_once_with('GET',
                                             'fake://example.com:6688/',
                                             allow_redirects=False,
                                             headers=headers)
Ejemplo n.º 8
0
    def test_token_or_credentials(self, mock_request):
        # Record a 200
        fake200 = fakes.FakeHTTPResponse(200, 'OK', {}, '')
        mock_request.side_effect = [fake200, fake200, fake200]

        # Replay, create client, assert
        client = http.HTTPClient('http://example.com:6688')
        resp, body = client.raw_request('GET', '')
        self.assertEqual(200, resp.status_code)

        client.username = osc_fakes.USERNAME
        client.password = osc_fakes.PASSWORD
        resp, body = client.raw_request('GET', '')
        self.assertEqual(200, resp.status_code)

        client.auth_token = osc_fakes.AUTH_TOKEN
        resp, body = client.raw_request('GET', '')
        self.assertEqual(200, resp.status_code)

        # no token or credentials
        mock_request.assert_has_calls([
            mock.call('GET',
                      'http://example.com:6688',
                      allow_redirects=False,
                      headers={
                          'User-Agent': 'python-pankoclient',
                          'Content-Type': 'application/octet-stream'
                      }),
            mock.call('GET',
                      'http://example.com:6688',
                      allow_redirects=False,
                      headers={
                          'User-Agent': 'python-pankoclient',
                          'X-Auth-Key': osc_fakes.PASSWORD,
                          'X-Auth-User': osc_fakes.USERNAME,
                          'Content-Type': 'application/octet-stream'
                      }),
            mock.call('GET',
                      'http://example.com:6688',
                      allow_redirects=False,
                      headers={
                          'User-Agent': 'python-pankoclient',
                          'X-Auth-Token': osc_fakes.AUTH_TOKEN,
                          'Content-Type': 'application/octet-stream'
                      })
        ])
Ejemplo n.º 9
0
 def test_http_json_request_redirect_error_without_location(
         self, mock_request):
     mock_request.return_value = fakes.FakeHTTPResponse(
         302, 'Found', {}, '')
     client = http.HTTPClient('http://example.com:6688/foo')
     self.assertRaises(exc.EndpointException, client.json_request, 'DELETE',
                       '')
     mock_request.assert_called_once_with('DELETE',
                                          'http://example.com:6688/foo',
                                          allow_redirects=False,
                                          headers={
                                              'Content-Type':
                                              'application/json',
                                              'Accept': 'application/json',
                                              'User-Agent':
                                              'python-pankoclient'
                                          })
Ejemplo n.º 10
0
    def test_http_request_specify_timeout(self, mock_request):
        mock_request.return_value = fakes.FakeHTTPResponse(
            200, 'OK', {'Content-Type': 'application/json'}, '{}')

        client = http.HTTPClient('http://example.com:6688', timeout='123')
        resp, body = client.json_request('GET', '')
        self.assertEqual(200, resp.status_code)
        self.assertEqual({}, body)
        mock_request.assert_called_once_with('GET',
                                             'http://example.com:6688',
                                             allow_redirects=False,
                                             headers={
                                                 'Content-Type':
                                                 'application/json',
                                                 'Accept': 'application/json',
                                                 'User-Agent':
                                                 'python-pankoclient'
                                             },
                                             timeout=float(123))
Ejemplo n.º 11
0
    def test_http_404_json_request(self, mock_request):
        mock_request.return_value = fakes.FakeHTTPResponse(
            404, 'Not Found', {'Content-Type': 'application/json'}, '')

        client = http.HTTPClient('http://example.com:6688')
        e = self.assertRaises(exc.NotFound, client.json_request, 'GET', '')
        # Assert that the raised exception can be converted to string
        self.assertIsNotNone(str(e))
        # Record a 404
        mock_request.assert_called_once_with('GET',
                                             'http://example.com:6688',
                                             allow_redirects=False,
                                             headers={
                                                 'Content-Type':
                                                 'application/json',
                                                 'Accept': 'application/json',
                                                 'User-Agent':
                                                 'python-pankoclient'
                                             })
Ejemplo n.º 12
0
    def test_http_json_request_invalid_json(self, mock_request):
        # Record a 200
        mock_request.return_value = fakes.FakeHTTPResponse(
            200, 'OK', {'Content-Type': 'application/json'}, 'invalid-json')

        client = http.HTTPClient('http://example.com:6688')
        resp, body = client.json_request('GET', '')
        self.assertEqual(200, resp.status_code)
        self.assertEqual('invalid-json', body)
        mock_request.assert_called_once_with('GET',
                                             'http://example.com:6688',
                                             allow_redirects=False,
                                             headers={
                                                 'Content-Type':
                                                 'application/json',
                                                 'Accept': 'application/json',
                                                 'User-Agent':
                                                 'python-pankoclient'
                                             })
Ejemplo n.º 13
0
    def test_http_json_request_non_json_resp_cont_type(self, mock_request):
        # Record a 200
        mock_request.return_value = fakes.FakeHTTPResponse(
            200, 'OK', {'Content-Type': 'not/json'}, '{}')

        client = http.HTTPClient('http://example.com:6688')
        resp, body = client.json_request('POST', '', data='test-data')
        self.assertEqual(200, resp.status_code)
        self.assertIsNone(body)
        mock_request.assert_called_once_with('POST',
                                             'http://example.com:6688',
                                             data='"test-data"',
                                             allow_redirects=False,
                                             headers={
                                                 'Content-Type':
                                                 'application/json',
                                                 'Accept': 'application/json',
                                                 'User-Agent':
                                                 'python-pankoclient'
                                             })
Ejemplo n.º 14
0
    def test_region_name(self, mock_request):
        # Record a 200
        fake200 = fakes.FakeHTTPResponse(200, 'OK', {}, '')
        mock_request.return_value = fake200

        client = http.HTTPClient('http://example.com:6688')
        client.region_name = osc_fakes.REGION_NAME
        resp, body = client.raw_request('GET', '')
        self.assertEqual(200, resp.status_code)

        mock_request.assert_called_once_with('GET',
                                             'http://example.com:6688',
                                             allow_redirects=False,
                                             headers={
                                                 'X-Region-Name':
                                                 osc_fakes.REGION_NAME,
                                                 'User-Agent':
                                                 'python-pankoclient',
                                                 'Content-Type':
                                                 'application/octet-stream'
                                             })
Ejemplo n.º 15
0
 def test_insecure_verify_cert_none(self, mock_request):
     client = http.HTTPClient('https://foo', insecure=True)
     self.assertFalse(client.verify_cert)