Пример #1
0
    def test_server_exception_socket(self):
        client = http.HTTPClient('http://localhost/', token='foobar')
        client.get_connection = (
            lambda *a, **kw: utils.FakeConnection(exc=socket.error))

        self.assertRaises(exc.ConnectionRefused, client.json_request, 'GET',
                          '/v1/resources')
Пример #2
0
    def test_server_exception_msg_and_traceback(self):
        error_msg = 'another test error'
        error_trace = ("\"Traceback (most recent call last):\\n\\n  "
                       "File \\\"/usr/local/lib/python2.7/...")
        error_body = _get_error_body(error_msg, error_trace,
                                     ERROR_LIST_WITH_DESC)
        fake_resp = utils.FakeResponse({'content-type': 'application/json'},
                                       six.StringIO(error_body),
                                       version=1,
                                       status=500)
        client = http.HTTPClient('http://localhost/')
        client.get_connection = (
            lambda *a, **kw: utils.FakeConnection(fake_resp))

        error = self.assertRaises(exc.InternalServerError, client.json_request,
                                  'GET', '/v1/resources')

        self.assertEqual(
            '%(error)s (HTTP 500)\n%(trace)s' % {
                'error': error_msg,
                'trace': error_trace
            }, "%(error)s\n%(details)s" % {
                'error': str(error),
                'details': str(error.details)
            })
Пример #3
0
    def test_server_exception_endpoint(self):
        endpoint = 'https://magnum-host:6385'
        client = http.HTTPClient(endpoint, token='foobar', insecure=True,
                                 ca_file='/path/to/ca_file')
        client.get_connection = (
            lambda *a, **kw: utils.FakeConnection(exc=socket.gaierror))

        self.assertRaises(exc.EndpointNotFound, client.json_request,
                          'GET', '/v1/resources', body='farboo')
Пример #4
0
    def test_401_unauthorized_exception(self):
        error_body = _get_error_body(err_type=ERROR_LIST_WITH_DETAIL)
        fake_resp = utils.FakeResponse({'content-type': 'text/plain'},
                                       six.StringIO(error_body),
                                       version=1,
                                       status=401)
        client = http.HTTPClient('http://localhost/')
        client.get_connection = (
            lambda *a, **kw: utils.FakeConnection(fake_resp))

        self.assertRaises(exc.Unauthorized, client.json_request, 'GET',
                          '/v1/resources')
Пример #5
0
    def test_raw_request(self):
        fake_resp = utils.FakeResponse(
            {'content-type': 'application/octet-stream'},
            'bar', version=1, status=200)
        client = http.HTTPClient('http://localhost/')
        conn = utils.FakeConnection(fake_resp)
        client.get_connection = (lambda *a, **kw: conn)

        resp, body = client.raw_request('GET', '/v1/resources')

        self.assertEqual(resp, fake_resp)
        self.assertIsInstance(body, http.ResponseBodyIterator)
Пример #6
0
    def test_server_success_body_none(self):
        fake_resp = utils.FakeResponse(
            {'content-type': None},
            six.StringIO('bar'), version=1, status=200)
        client = http.HTTPClient('http://localhost/')
        conn = utils.FakeConnection(fake_resp)
        client.get_connection = (lambda *a, **kw: conn)

        resp, body = client.json_request('GET', '/v1/resources')

        self.assertEqual(resp, fake_resp)
        self.assertIsInstance(body, list)
Пример #7
0
    def test_server_success_body_app(self):
        fake_resp = utils.FakeResponse(
            {'content-type': 'application/octet-stream'},
            'bar', version=1, status=200)
        client = http.HTTPClient('http://localhost/')
        conn = utils.FakeConnection(fake_resp)
        client.get_connection = (lambda *a, **kw: conn)

        resp, body = client.json_request('GET', '/v1/resources')

        self.assertEqual(resp, fake_resp)
        self.assertIsNone(body)
Пример #8
0
    def test_server_exception_empty_body(self):
        error_body = _get_error_body()
        fake_resp = utils.FakeResponse({'content-type': 'application/json'},
                                       six.StringIO(error_body),
                                       version=1,
                                       status=500)
        client = http.HTTPClient('http://localhost/')
        client.get_connection = (
            lambda *a, **kw: utils.FakeConnection(fake_resp))

        error = self.assertRaises(exc.InternalServerError, client.json_request,
                                  'GET', '/v1/resources')
        self.assertEqual('Internal Server Error (HTTP 500)', str(error))
Пример #9
0
    def test_server_success_body_json(self):
        err = _get_error_body()
        fake_resp = utils.FakeResponse(
            {'content-type': 'application/json'},
            six.StringIO(err), version=1, status=200)
        client = http.HTTPClient('http://localhost/')
        conn = utils.FakeConnection(fake_resp)
        client.get_connection = (lambda *a, **kw: conn)

        resp, body = client.json_request('GET', '/v1/resources')

        self.assertEqual(resp, fake_resp)
        self.assertEqual(jsonutils.dumps(body), err)
Пример #10
0
    def test_server_redirect_exception(self):
        fake_redirect_resp = utils.FakeResponse(
            {'content-type': 'application/octet-stream'},
            'foo', version=1, status=301)
        fake_resp = utils.FakeResponse(
            {'content-type': 'application/octet-stream'},
            'bar', version=1, status=300)
        client = http.HTTPClient('http://localhost/')
        conn = utils.FakeConnection(fake_redirect_resp,
                                    redirect_resp=fake_resp)
        client.get_connection = (lambda *a, **kw: conn)

        self.assertRaises(MultipleChoices, client.json_request,
                          'GET', '/v1/resources')