Exemple #1
0
    def test_302_location_no_endpoint(self):
        fake1 = fakes.FakeHTTPResponse(302, 'OK',
                                       {'location': 'http://no.where/ishere'},
                                       '')
        fake2 = fakes.FakeHTTPResponse(200, 'OK',
                                       {'content-type': 'application/json'},
                                       jsonutils.dumps({'Mount': 'Fuji'}))
        self.request.side_effect = [(fake1, ''),
                                    (fake2, jsonutils.dumps({'Mount':
                                                             'Fuji'}))]

        client = http.SessionClient(session=mock.ANY, auth=mock.ANY)
        resp = client.request('', 'GET', redirect=True)

        self.assertEqual(200, resp.status_code)
        self.assertEqual({'Mount': 'Fuji'}, utils.get_response_body(resp))

        self.assertEqual(('', 'GET'), self.request.call_args_list[0][0])
        self.assertEqual(('http://no.where/ishere', 'GET'),
                         self.request.call_args_list[1][0])
        for call in self.request.call_args_list:
            self.assertEqual(
                {
                    'headers': {
                        'Content-Type': 'application/json'
                    },
                    'user_agent': 'python-heatclient',
                    'raise_exc': False,
                    'redirect': True
                }, call[1])
Exemple #2
0
    def test_kwargs_with_files(self, mock_dumps):
        fake = fakes.FakeHTTPResponse(200, 'OK',
                                      {'content-type': 'application/json'}, {})
        mock_dumps.return_value = "{'files': test}}"
        data = six.BytesIO(b'test')
        kwargs = {
            'endpoint_override': 'http://no.where/',
            'data': {
                'files': data
            }
        }
        client = http.SessionClient(mock.ANY)

        self.request.return_value = (fake, {})

        resp = client.request('', 'GET', **kwargs)

        self.assertEqual(
            {
                'endpoint_override': 'http://no.where/',
                'data': "{'files': test}}",
                'headers': {
                    'Content-Type': 'application/json'
                },
                'user_agent': 'python-heatclient',
                'raise_exc': False
            }, self.request.call_args[1])
        self.assertEqual(200, resp.status_code)
    def test_404_error_response(self):
        self.auth_session.request.return_value = fakes.FakeHTTPResponse(
            404, 'OK', {'content-type': 'application/octet-stream'}, '')

        client = http.SessionClient(session=self.auth_session,
                                    auth=self.auth_plugin)
        e = self.assertRaises(exc.HTTPNotFound, client.raw_request, 'GET', '')
        # Assert that the raised exception can be converted to string
        self.assertIsNotNone(str(e))
    def test_session_raw_request(self):
        self.auth_session.request.return_value = fakes.FakeHTTPResponse(
            200, 'OK', {'content-type': 'application/octet-stream'}, '')

        client = http.SessionClient(session=self.auth_session,
                                    auth=self.auth_plugin)
        resp = client.raw_request(method='GET', url='')
        self.assertEqual(200, resp.status_code)
        self.assertEqual('', ''.join([x for x in resp.content]))
Exemple #5
0
    def test_no_redirect_302_no_location(self):
        fake = fakes.FakeHTTPResponse(302, 'OK',
                                      {'location': 'http://no.where/ishere'},
                                      '')
        self.request.side_effect = [(fake, '')]

        client = http.SessionClient(session=mock.ANY, auth=mock.ANY)

        self.assertEqual(fake, client.request('', 'GET'))
Exemple #6
0
    def test_404_error_response(self):
        fake = fakes.FakeHTTPResponse(
            404, 'FAIL', {'content-type': 'application/octet-stream'}, '')
        self.request.return_value = (fake, '')

        client = http.SessionClient(session=mock.ANY, auth=mock.ANY)
        e = self.assertRaises(exc.HTTPNotFound, client.request, '', 'GET')
        # Assert that the raised exception can be converted to string
        self.assertIsNotNone(six.text_type(e))
Exemple #7
0
    def test_session_simple_request(self):
        resp = fakes.FakeHTTPResponse(
            200, 'OK', {'content-type': 'application/octet-stream'}, '')
        self.request.return_value = (resp, '')

        client = http.SessionClient(session=mock.ANY, auth=mock.ANY)
        response = client.request(method='GET', url='')
        self.assertEqual(200, response.status_code)
        self.assertEqual('', ''.join([x for x in response.content]))
    def test_session_json_request(self):
        self.auth_session.request.return_value = fakes.FakeHTTPResponse(
            200, 'OK', {'content-type': 'application/json'}, '{}')

        client = http.SessionClient(session=self.auth_session,
                                    auth=self.auth_plugin)

        resp, body = client.json_request('GET', '')
        self.assertEqual(200, resp.status_code)
        self.assertEqual({}, body)
Exemple #9
0
    def test_504_error_response(self):
        # for 504 we don't have specific exception type
        fake = fakes.FakeHTTPResponse(
            504, 'FAIL', {'content-type': 'application/octet-stream'}, '')
        self.request.return_value = (fake, '')

        client = http.SessionClient(session=mock.ANY, auth=mock.ANY)
        e = self.assertRaises(exc.HTTPException, client.request, '', 'GET')

        self.assertEqual(504, e.code)
Exemple #10
0
    def test_redirect_302_no_location(self):
        fake = fakes.FakeHTTPResponse(302, 'OK', {}, '')
        self.request.side_effect = [(fake, '')]

        client = http.SessionClient(session=mock.ANY, auth=mock.ANY)
        e = self.assertRaises(exc.InvalidEndpoint,
                              client.request,
                              '',
                              'GET',
                              redirect=True)
        self.assertEqual("Location not returned with 302", six.text_type(e))
Exemple #11
0
    def test_session_json_request(self):
        fake = fakes.FakeHTTPResponse(200, 'OK',
                                      {'content-type': 'application/json'},
                                      jsonutils.dumps({'some': 'body'}))
        self.request.return_value = (fake, {})

        client = http.SessionClient(session=mock.ANY, auth=mock.ANY)

        resp = client.request('', 'GET')
        self.assertEqual(200, resp.status_code)
        self.assertEqual({'some': 'body'}, resp.json())
Exemple #12
0
    def test_methods(self):
        fake = fakes.FakeHTTPResponse(200, 'OK',
                                      {'content-type': 'application/json'}, {})
        self.request.return_value = (fake, {})

        client = http.SessionClient(mock.ANY)
        methods = [
            client.get, client.put, client.post, client.patch, client.delete,
            client.head
        ]
        for method in methods:
            resp = method('')
            self.assertEqual(200, resp.status_code)
    def test_kwargs(self):
        fake = fakes.FakeHTTPResponse(200, 'OK',
                                      {'content-type': 'application/json'}, {})
        kwargs = dict(endpoint_override='http://no.where/', data='some_data')

        client = http.SessionClient(mock.ANY)

        self.request.return_value = (fake, {})

        resp = client.request('', 'GET', **kwargs)

        self.assertEqual(
            {
                'endpoint_override': 'http://no.where/',
                'data': '"some_data"',
                'user_agent': 'python-heatclient',
                'raise_exc': False
            }, self.request.call_args[1])
        self.assertEqual(200, resp.status_code)
Exemple #14
0
 def test_credentials_headers(self):
     client = http.SessionClient(mock.ANY)
     self.assertEqual({}, client.credentials_headers())