Files
deb-python-oauth2client/tests/http_mock.py
Craig Citro a97121f0d6 Drop the googleapiclient copy, and cleanup tests.
This drops the duplication of `googleapiclient` inside the `oauth2client` repo,
which involved several steps:

* Delete the actual `googleapiclient` files.
* Remove the duplicated tests, since they're also hosted in the
  `googleapiclient` repo.
* Add a copy of three testing-related functions back in, since they're also
  used in the tests here. This was `HttpMock` and `HttpMockSequence` in
  `tests/http_mock.py`, and `assertUrisEqual` in `tests/test_oauth2client.py`.

Once that was done, I did just the tiniest bit of cleanup in the tests:

* Remove the now-superfluous `_oauth2client_` in the filenames for all the
  tests.
* Drop unneeded files from `data/`.
* Add `py26` back as a `tox` testing environment.
* Temporarily disable the `appengine` test for both environments.

In a coming PR, I'll actually clean up the GAE test to work correctly in the
presence or absence of `dev_appserver`, but this isn't a bad first step. (In
particular, my next PR will just add a `.travis.yml`.)
2014-05-05 12:09:05 -07:00

113 lines
3.4 KiB
Python

# Copyright (C) 2012 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Copy of googleapiclient.http's mock functionality."""
import httplib2
from oauth2client.anyjson import simplejson
# TODO(craigcitro): Find a cleaner way to share this code with googleapiclient.
class HttpMock(object):
"""Mock of httplib2.Http"""
def __init__(self, filename=None, headers=None):
"""
Args:
filename: string, absolute filename to read response from
headers: dict, header to return with response
"""
if headers is None:
headers = {'status': '200 OK'}
if filename:
f = file(filename, 'r')
self.data = f.read()
f.close()
else:
self.data = None
self.response_headers = headers
self.headers = None
self.uri = None
self.method = None
self.body = None
self.headers = None
def request(self, uri,
method='GET',
body=None,
headers=None,
redirections=1,
connection_type=None):
self.uri = uri
self.method = method
self.body = body
self.headers = headers
return httplib2.Response(self.response_headers), self.data
class HttpMockSequence(object):
"""Mock of httplib2.Http
Mocks a sequence of calls to request returning different responses for each
call. Create an instance initialized with the desired response headers
and content and then use as if an httplib2.Http instance.
http = HttpMockSequence([
({'status': '401'}, ''),
({'status': '200'}, '{"access_token":"1/3w","expires_in":3600}'),
({'status': '200'}, 'echo_request_headers'),
])
resp, content = http.request("http://examples.com")
There are special values you can pass in for content to trigger
behavours that are helpful in testing.
'echo_request_headers' means return the request headers in the response body
'echo_request_headers_as_json' means return the request headers in
the response body
'echo_request_body' means return the request body in the response body
'echo_request_uri' means return the request uri in the response body
"""
def __init__(self, iterable):
"""
Args:
iterable: iterable, a sequence of pairs of (headers, body)
"""
self._iterable = iterable
self.follow_redirects = True
def request(self, uri,
method='GET',
body=None,
headers=None,
redirections=1,
connection_type=None):
resp, content = self._iterable.pop(0)
if content == 'echo_request_headers':
content = headers
elif content == 'echo_request_headers_as_json':
content = simplejson.dumps(headers)
elif content == 'echo_request_body':
if hasattr(body, 'read'):
content = body.read()
else:
content = body
elif content == 'echo_request_uri':
content = uri
return httplib2.Response(resp), content