def test_http_404_json_request(self):
     # Record a 404
     mock_conn = http.requests.request(
         'GET', 'http://example.com:8004',
         allow_redirects=False,
         headers={'Content-Type': 'application/json',
                  'Accept': 'application/json',
                  'User-Agent': 'python-heatclient'})
     mock_conn.AndReturn(
         fakes.FakeHTTPResponse(
             404, 'OK', {'content-type': 'application/json'},
             '{}'))
     # Replay, create client, assert
     self.m.ReplayAll()
     client = http.HTTPClient('http://example.com:8004')
     e = self.assertRaises(exc.HTTPNotFound, client.json_request, 'GET', '')
     # Assert that the raised exception can be converted to string
     self.assertIsNotNone(str(e))
    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')

        # Replay, create client, assert
        client = http.HTTPClient('http://example.com:8004')
        resp, body = client.json_request('GET', '')
        self.assertEqual(200, resp.status_code)
        self.assertEqual('invalid-json', body)
        mock_request.assert_called_with('GET',
                                        'http://example.com:8004',
                                        allow_redirects=False,
                                        headers={
                                            'Content-Type': 'application/json',
                                            'Accept': 'application/json',
                                            'User-Agent': 'python-heatclient'
                                        })
    def test_http_raw_request(self, mock_request):
        headers = {
            'Content-Type': 'application/octet-stream',
            'User-Agent': 'python-heatclient'
        }

        # Record a 200
        mock_request.return_value = fakes.FakeHTTPResponse(
            200, 'OK', {'content-type': 'application/octet-stream'}, '')
        # Replay, create client, assert
        client = http.HTTPClient('http://example.com:8004')
        resp = client.raw_request('GET', '')
        self.assertEqual(200, resp.status_code)
        self.assertEqual('', ''.join([x for x in resp.content]))
        mock_request.assert_called_with('GET',
                                        'http://example.com:8004',
                                        allow_redirects=False,
                                        headers=headers)
示例#4
0
 def test_http_manual_redirect_prohibited(self):
     mock_conn = http.requests.request('DELETE',
                                       'http://example.com:8004/foo',
                                       allow_redirects=False,
                                       headers={
                                           'Content-Type':
                                           'application/json',
                                           'Accept': 'application/json',
                                           'User-Agent': 'python-heatclient'
                                       })
     mock_conn.AndReturn(
         fakes.FakeHTTPResponse(302, 'Found',
                                {'location': 'http://example.com:8004/'},
                                ''))
     self.m.ReplayAll()
     client = http.HTTPClient('http://example.com:8004/foo')
     self.assertRaises(exc.InvalidEndpoint, client.json_request, 'DELETE',
                       '')
示例#5
0
    def test_debug_curl_command(self, mock_request):
        with mock.patch('logging.Logger.debug') as mock_logging_debug:

            ssl_connection_params = {'ca_file': 'TEST_CA',
                                     'cert_file': 'TEST_CERT',
                                     'key_file': 'TEST_KEY',
                                     'insecure': 'TEST_NSA'}

            headers = {'key': 'value'}
            mock_logging_debug.return_value = None
            client = http.HTTPClient('http://foo')
            client.ssl_connection_params = ssl_connection_params
            client.log_curl_request('GET', '/bar', {'headers': headers,
                                                    'data': 'text'})
            mock_logging_debug.assert_called_with(
                "curl -g -i -X GET -H 'key: value' --key TEST_KEY "
                "--cert TEST_CERT --cacert TEST_CA "
                "-k -d 'text' http://foo/bar"
            )
 def test_http_request_specify_timeout(self):
     mock_conn = http.requests.request(
         'GET', 'http://example.com:8004',
         allow_redirects=False,
         headers={'Content-Type': 'application/json',
                  'Accept': 'application/json',
                  'User-Agent': 'python-heatclient'},
         timeout=float(123))
     mock_conn.AndReturn(
         fakes.FakeHTTPResponse(
             200, 'OK',
             {'content-type': 'application/json'},
             '{}'))
     # Replay, create client, assert
     self.m.ReplayAll()
     client = http.HTTPClient('http://example.com:8004', timeout='123')
     resp, body = client.json_request('GET', '')
     self.assertEqual(200, resp.status_code)
     self.assertEqual({}, body)
 def test_http_json_request_non_json_resp_cont_type(self):
     # Record a 200
     mock_conn = http.requests.request(
         'GET', 'http://example.com:8004', body='test-body',
         allow_redirects=False,
         headers={'Content-Type': 'application/json',
                  'Accept': 'application/json',
                  'User-Agent': 'python-heatclient'})
     mock_conn.AndReturn(
         fakes.FakeHTTPResponse(
             200, 'OK',
             {'content-type': 'not/json'},
             {}))
     # Replay, create client, assert
     self.m.ReplayAll()
     client = http.HTTPClient('http://example.com:8004')
     resp, body = client.json_request('GET', '', body='test-body')
     self.assertEqual(200, resp.status_code)
     self.assertIsNone(body)
    def test_http_raw_request(self):
        headers = {'Content-Type': 'application/octet-stream',
                   'User-Agent': 'python-heatclient'}

        # Record a 200
        mock_conn = http.requests.request('GET', 'http://example.com:8004',
                                          allow_redirects=False,
                                          headers=headers)
        mock_conn.AndReturn(
            fakes.FakeHTTPResponse(
                200, 'OK',
                {'content-type': 'application/octet-stream'},
                ''))
        # Replay, create client, assert
        self.m.ReplayAll()
        client = http.HTTPClient('http://example.com:8004')
        resp = client.raw_request('GET', '')
        self.assertEqual(200, resp.status_code)
        self.assertEqual('', ''.join([x for x in resp.content]))
        self.m.VerifyAll()
示例#9
0
    def test_not_include_pass(self, mock_request):
        # Record a 200
        fake500 = fakes.FakeHTTPResponse(
            500, 'ERROR',
            {'content-type': 'application/octet-stream'},
            b'(HTTP 401)')

        # no token or credentials
        mock_request.return_value = fake500

        # Replay, create client, assert
        client = http.HTTPClient('http://example.com:8004')
        e = self.assertRaises(exc.HTTPUnauthorized,
                              client.raw_request, 'GET', '')
        self.assertIn('Authentication failed', str(e))
        mock_request.assert_called_with(
            'GET', 'http://example.com:8004',
            allow_redirects=False,
            headers={'Content-Type': 'application/octet-stream',
                     'User-Agent': 'python-heatclient'})
    def test_not_include_pass(self):
        # Record a 200
        fake500 = fakes.FakeHTTPResponse(
            500, 'ERROR',
            {'content-type': 'application/octet-stream'},
            '(HTTP 401)')

        # no token or credentials
        mock_conn = http.requests.request(
            'GET', 'http://example.com:8004',
            allow_redirects=False,
            headers={'Content-Type': 'application/octet-stream',
                     'User-Agent': 'python-heatclient'})
        mock_conn.AndReturn(fake500)

        # Replay, create client, assert
        self.m.ReplayAll()
        client = http.HTTPClient('http://example.com:8004')
        e = self.assertRaises(exc.HTTPUnauthorized,
                              client.raw_request, 'GET', '')
        self.assertIn('include-password', str(e))
示例#11
0
    def test_region_name(self, mock_request):
        # Record a 200
        fake200 = fakes.FakeHTTPResponse(
            200, 'OK',
            {'content-type': 'application/octet-stream'},
            '')

        # Specify region name
        mock_request.return_value = fake200

        # Replay, create client, assert
        client = http.HTTPClient('http://example.com:8004')
        client.region_name = 'RegionOne'
        resp = client.raw_request('GET', '')
        self.assertEqual(200, resp.status_code)
        mock_request.assert_called_with(
            'GET', 'http://example.com:8004',
            allow_redirects=False,
            headers={'Content-Type': 'application/octet-stream',
                     'X-Region-Name': 'RegionOne',
                     'User-Agent': 'python-heatclient'})
示例#12
0
 def test_include_pass(self, mock_request):
     # Record a 200
     fake200 = fakes.FakeHTTPResponse(
         200, 'OK',
         {'content-type': 'application/octet-stream'},
         '')
     mock_request.return_value = fake200
     # no token or credentials
     client = http.HTTPClient('http://example.com:8004')
     resp = client.raw_request('GET', '')
     self.assertEqual(200, resp.status_code)
     # credentials
     client.username = '******'
     client.password = '******'
     client.include_pass = True
     resp = client.raw_request('GET', '')
     self.assertEqual(200, resp.status_code)
     # token suppresses credentials
     client.auth_token = 'abcd1234'
     resp = client.raw_request('GET', '')
     self.assertEqual(200, resp.status_code)
     mock_request.assert_has_calls([
         mock.call('GET', 'http://example.com:8004',
                   allow_redirects=False,
                   headers={'Content-Type': 'application/octet-stream',
                            'User-Agent': 'python-heatclient'}),
         mock.call('GET', 'http://example.com:8004',
                   allow_redirects=False,
                   headers={'Content-Type': 'application/octet-stream',
                            'User-Agent': 'python-heatclient',
                            'X-Auth-Key': 'pass',
                            'X-Auth-User': '******'}),
         mock.call('GET', 'http://example.com:8004',
                   allow_redirects=False,
                   headers={'Content-Type': 'application/octet-stream',
                            'User-Agent': 'python-heatclient',
                            'X-Auth-Token': 'abcd1234',
                            'X-Auth-Key': 'pass',
                            'X-Auth-User': '******'})
     ])
    def test_debug_curl_command(self):
        self.m.StubOutWithMock(logging.Logger, 'debug')

        ssl_connection_params = {'ca_file': 'TEST_CA',
                                 'cert_file': 'TEST_CERT',
                                 'key_file': 'TEST_KEY',
                                 'insecure': 'TEST_NSA'}

        headers = {'key': 'value'}

        mock_logging_debug = logging.Logger.debug(
            "curl -g -i -X GET -H 'key: value' --key TEST_KEY "
            "--cert TEST_CERT --cacert TEST_CA "
            "-k -d 'text' http://foo/bar"
        )
        mock_logging_debug.AndReturn(None)
        self.m.ReplayAll()

        client = http.HTTPClient('http://foo')
        client.ssl_connection_params = ssl_connection_params
        client.log_curl_request('GET', '/bar', {'headers': headers,
                                                'data': 'text'})
 def test_insecure_verify_cert_None(self):
     client = http.HTTPClient('https://foo', insecure=True)
     self.assertFalse(client.verify_cert)
    def test_include_pass(self):
        # Record a 200
        fake200 = fakes.FakeHTTPResponse(
            200, 'OK', {'content-type': 'application/octet-stream'}, '')

        # no token or credentials
        mock_conn = http.requests.request('GET',
                                          'http://example.com:8004',
                                          allow_redirects=False,
                                          headers={
                                              'Content-Type':
                                              'application/octet-stream',
                                              'User-Agent': 'python-heatclient'
                                          })
        mock_conn.AndReturn(fake200)

        # credentials
        mock_conn = http.requests.request('GET',
                                          'http://example.com:8004',
                                          allow_redirects=False,
                                          headers={
                                              'Content-Type':
                                              'application/octet-stream',
                                              'User-Agent':
                                              'python-heatclient',
                                              'X-Auth-Key': 'pass',
                                              'X-Auth-User': '******'
                                          })
        mock_conn.AndReturn(fake200)

        # token suppresses credentials
        mock_conn = http.requests.request('GET',
                                          'http://example.com:8004',
                                          allow_redirects=False,
                                          headers={
                                              'Content-Type':
                                              'application/octet-stream',
                                              'User-Agent':
                                              'python-heatclient',
                                              'X-Auth-Token': 'abcd1234',
                                              'X-Auth-Key': 'pass',
                                              'X-Auth-User': '******'
                                          })
        mock_conn.AndReturn(fake200)

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

        client.username = '******'
        client.password = '******'
        client.include_pass = True
        resp = client.raw_request('GET', '')
        self.assertEqual(200, resp.status_code)

        client.auth_token = 'abcd1234'
        resp = client.raw_request('GET', '')
        self.assertEqual(200, resp.status_code)
        self.m.VerifyAll()