OLD | NEW |
(Empty) | |
| 1 # |
| 2 # Copyright 2015 Google Inc. |
| 3 # |
| 4 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 # you may not use this file except in compliance with the License. |
| 6 # You may obtain a copy of the License at |
| 7 # |
| 8 # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 # |
| 10 # Unless required by applicable law or agreed to in writing, software |
| 11 # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 # See the License for the specific language governing permissions and |
| 14 # limitations under the License. |
| 15 |
| 16 """Tests for http_wrapper.""" |
| 17 import socket |
| 18 |
| 19 import httplib2 |
| 20 import oauth2client |
| 21 from six.moves import http_client |
| 22 import unittest2 |
| 23 |
| 24 from mock import patch |
| 25 |
| 26 from apitools.base.py import exceptions |
| 27 from apitools.base.py import http_wrapper |
| 28 |
| 29 |
| 30 class _MockHttpRequest(object): |
| 31 |
| 32 url = None |
| 33 |
| 34 |
| 35 class _MockHttpResponse(object): |
| 36 |
| 37 def __init__(self, status_code): |
| 38 self.response = {'status': status_code} |
| 39 |
| 40 |
| 41 class RaisesExceptionOnLen(object): |
| 42 |
| 43 """Supports length property but raises if __len__ is used.""" |
| 44 |
| 45 def __len__(self): |
| 46 raise Exception('len() called unnecessarily') |
| 47 |
| 48 def length(self): |
| 49 return 1 |
| 50 |
| 51 |
| 52 class HttpWrapperTest(unittest2.TestCase): |
| 53 |
| 54 def testRequestBodyUsesLengthProperty(self): |
| 55 http_wrapper.Request(body=RaisesExceptionOnLen()) |
| 56 |
| 57 def testRequestBodyWithLen(self): |
| 58 http_wrapper.Request(body='burrito') |
| 59 |
| 60 def testDefaultExceptionHandler(self): |
| 61 """Ensures exception handles swallows (retries)""" |
| 62 mock_http_content = 'content'.encode('utf8') |
| 63 for exception_arg in ( |
| 64 http_client.BadStatusLine('line'), |
| 65 http_client.IncompleteRead('partial'), |
| 66 http_client.ResponseNotReady(), |
| 67 socket.error(), |
| 68 socket.gaierror(), |
| 69 httplib2.ServerNotFoundError(), |
| 70 ValueError(), |
| 71 oauth2client.client.HttpAccessTokenRefreshError(status=503), |
| 72 exceptions.RequestError(), |
| 73 exceptions.BadStatusCodeError( |
| 74 {'status': 503}, mock_http_content, 'url'), |
| 75 exceptions.RetryAfterError( |
| 76 {'status': 429}, mock_http_content, 'url', 0)): |
| 77 |
| 78 retry_args = http_wrapper.ExceptionRetryArgs( |
| 79 http={'connections': {}}, http_request=_MockHttpRequest(), |
| 80 exc=exception_arg, num_retries=0, max_retry_wait=0) |
| 81 |
| 82 # Disable time.sleep for this handler as it is called with |
| 83 # a minimum value of 1 second. |
| 84 with patch('time.sleep', return_value=None): |
| 85 http_wrapper.HandleExceptionsAndRebuildHttpConnections( |
| 86 retry_args) |
OLD | NEW |