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, body = client.request('', 'GET', **kwargs)

        self.assertEqual(
            {
                'endpoint_override': 'http://no.where/',
                'json': {
                    'files': data
                },
                'user_agent': 'python-pankoclient',
                'raise_exc': False
            }, self.request.call_args[1])
        self.assertEqual(200, resp.status_code)
        self.assertEqual({}, body)
        self.assertEqual({}, utils.get_response_body(resp))
    def test_302_location_not_override(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, None), (fake2, {'Mount': 'Fuji'})]

        client = http.SessionClient(session=mock.ANY,
                                    auth=mock.ANY,
                                    endpoint_override='http://endpoint/')
        resp, body = client.request('', 'GET', redirect=True)

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

        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(
                {
                    'user_agent': 'python-pankoclient',
                    'raise_exc': False,
                    'redirect': True
                }, call[1])
    def test_300_error_response(self):
        fake = fakes.FakeHTTPResponse(
            300, 'FAIL', {'Content-Type': 'application/octet-stream'}, '')
        self.request.return_value = (fake, '')

        client = http.SessionClient(session=mock.ANY, auth=mock.ANY)
        e = self.assertRaises(exc.MultipleChoices, client.request, '', 'GET')
        # Assert that the raised exception can be converted to string
        self.assertIsNotNone(six.text_type(e))
    def test_404_error_response(self):
        fake = fakes.FakeHTTPResponse(404, 'Not Found',
                                      {'Content-Type': 'application/json'}, '')
        self.request.return_value = (fake, '')

        client = http.SessionClient(session=mock.ANY, auth=mock.ANY)
        e = self.assertRaises(exc.NotFound, client.request, '', 'GET')
        # Assert that the raised exception can be converted to string
        self.assertIsNotNone(six.text_type(e))
    def test_506_error_response(self):
        # for 506 we don't have specific exception type
        fake = fakes.FakeHTTPResponse(
            506, 'FAIL', {'Content-Type': 'application/octet-stream'}, '')
        self.request.return_value = (fake, '')

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

        self.assertEqual(506, e.status_code)
    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)
        resp, body = client.request('', 'GET')

        self.assertEqual(fake, resp)
    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)
        resp, body = client.request(method='GET', url='')
        self.assertEqual(200, resp.status_code)
        self.assertEqual('{}', ''.join([x for x in resp.content]))
        self.assertEqual({}, body)
    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.EndpointException,
                              client.request,
                              '',
                              'GET',
                              redirect=True)
        self.assertEqual("Location not returned with redirect",
                         six.text_type(e))
    def test_session_json_request(self):
        fake = fakes.FakeHTTPResponse(200, 'OK',
                                      {'Content-Type': 'application/json'},
                                      jsonutils.dumps({'some': 'body'}))
        self.request.return_value = (fake, {'some': 'body'})

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

        resp, body = client.request('', 'GET')
        self.assertEqual(200, resp.status_code)
        self.assertEqual({'some': 'body'}, resp.json())
        self.assertEqual({'some': 'body'}, body)
    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, body = 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, body = client.request('', 'GET', **kwargs)

        self.assertEqual(
            {
                'endpoint_override': 'http://no.where/',
                'json': 'some_data',
                'user_agent': 'python-pankoclient',
                'raise_exc': False
            }, self.request.call_args[1])
        self.assertEqual(200, resp.status_code)
        self.assertEqual({}, body)
        self.assertEqual({}, utils.get_response_body(resp))
 def test_credentials_headers(self):
     client = http.SessionClient(mock.ANY)
     self.assertEqual({}, client.credentials_headers())