|
| 1 | +# Copyright 2017 Google Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import unittest2 |
| 16 | + |
| 17 | +from apitools.base.py import exceptions |
| 18 | +from apitools.base.py import http_wrapper |
| 19 | + |
| 20 | + |
| 21 | +def _MakeResponse(status_code): |
| 22 | + return http_wrapper.Response( |
| 23 | + info={'status': status_code}, content='{"field": "abc"}', |
| 24 | + request_url='http://www.google.com') |
| 25 | + |
| 26 | + |
| 27 | +class HttpErrorFromResponseTest(unittest2.TestCase): |
| 28 | + |
| 29 | + """Tests for exceptions.HttpError.FromResponse.""" |
| 30 | + |
| 31 | + def testBadRequest(self): |
| 32 | + err = exceptions.HttpError.FromResponse(_MakeResponse(400)) |
| 33 | + self.assertIsInstance(err, exceptions.HttpError) |
| 34 | + self.assertIsInstance(err, exceptions.HttpBadRequestError) |
| 35 | + self.assertEquals(err.status_code, 400) |
| 36 | + |
| 37 | + def testUnauthorized(self): |
| 38 | + err = exceptions.HttpError.FromResponse(_MakeResponse(401)) |
| 39 | + self.assertIsInstance(err, exceptions.HttpError) |
| 40 | + self.assertIsInstance(err, exceptions.HttpUnauthorizedError) |
| 41 | + self.assertEquals(err.status_code, 401) |
| 42 | + |
| 43 | + def testForbidden(self): |
| 44 | + err = exceptions.HttpError.FromResponse(_MakeResponse(403)) |
| 45 | + self.assertIsInstance(err, exceptions.HttpError) |
| 46 | + self.assertIsInstance(err, exceptions.HttpForbiddenError) |
| 47 | + self.assertEquals(err.status_code, 403) |
| 48 | + |
| 49 | + def testNotFound(self): |
| 50 | + err = exceptions.HttpError.FromResponse(_MakeResponse(404)) |
| 51 | + self.assertIsInstance(err, exceptions.HttpError) |
| 52 | + self.assertIsInstance(err, exceptions.HttpNotFoundError) |
| 53 | + self.assertEquals(err.status_code, 404) |
| 54 | + |
| 55 | + def testConflict(self): |
| 56 | + err = exceptions.HttpError.FromResponse(_MakeResponse(409)) |
| 57 | + self.assertIsInstance(err, exceptions.HttpError) |
| 58 | + self.assertIsInstance(err, exceptions.HttpConflictError) |
| 59 | + self.assertEquals(err.status_code, 409) |
| 60 | + |
| 61 | + def testUnknownStatus(self): |
| 62 | + err = exceptions.HttpError.FromResponse(_MakeResponse(499)) |
| 63 | + self.assertIsInstance(err, exceptions.HttpError) |
| 64 | + self.assertEquals(err.status_code, 499) |
| 65 | + |
| 66 | + def testMalformedStatus(self): |
| 67 | + err = exceptions.HttpError.FromResponse(_MakeResponse('BAD')) |
| 68 | + self.assertIsInstance(err, exceptions.HttpError) |
0 commit comments