Move HttpMock out of tests and into apiclient.http. Update tests that used HttpMock

This commit is contained in:
Joe Gregorio
2011-02-11 23:20:52 -05:00
parent ded54c5fe5
commit cb8103dbd3
5 changed files with 75 additions and 44 deletions

View File

@@ -9,10 +9,12 @@ actuall HTTP request.
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
__all__ = [
'HttpRequest', 'RequestMockBuilder'
'HttpRequest', 'RequestMockBuilder', 'HttpMock'
]
import httplib2
import os
from model import JsonModel
@@ -147,3 +149,20 @@ class RequestMockBuilder(object):
else:
model = JsonModel()
return HttpRequestMock(None, '{}', model.response)
class HttpMock(object):
"""Mock of httplib2.Http"""
def __init__(self, filename, headers):
"""
Args:
filename: string, absolute filename to read response from
headers: dict, header to return with response
"""
f = file(filename, 'r')
self.data = f.read()
f.close()
self.headers = headers
def request(self, uri, method="GET", body=None, headers=None, redirections=1, connection_type=None):
return httplib2.Response(self.headers), self.data

View File

@@ -22,7 +22,8 @@ actuall&nbsp;HTTP&nbsp;request.</tt></p>
<tr><td bgcolor="#aa55cc"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
<td width="100%"><table width="100%" summary="list"><tr><td width="25%" valign=top><a href="httplib2.html">httplib2</a><br>
</td><td width="25%" valign=top></td><td width="25%" valign=top></td><td width="25%" valign=top></td></tr></table></td></tr></table><p>
</td><td width="25%" valign=top><a href="os.html">os</a><br>
</td><td width="25%" valign=top></td><td width="25%" valign=top></td></tr></table></td></tr></table><p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#ee77aa">
<td colspan=3 valign=bottom>&nbsp;<br>
@@ -33,7 +34,8 @@ actuall&nbsp;HTTP&nbsp;request.</tt></p>
<dt><font face="helvetica, arial"><a href="__builtin__.html#object">__builtin__.object</a>
</font></dt><dd>
<dl>
<dt><font face="helvetica, arial"><a href="apiclient.http.html#HttpRequest">HttpRequest</a>
<dt><font face="helvetica, arial"><a href="apiclient.http.html#HttpMock">HttpMock</a>
</font></dt><dt><font face="helvetica, arial"><a href="apiclient.http.html#HttpRequest">HttpRequest</a>
</font></dt><dt><font face="helvetica, arial"><a href="apiclient.http.html#RequestMockBuilder">RequestMockBuilder</a>
</font></dt></dl>
</dd>
@@ -42,6 +44,30 @@ actuall&nbsp;HTTP&nbsp;request.</tt></p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#ffc8d8">
<td colspan=3 valign=bottom>&nbsp;<br>
<font color="#000000" face="helvetica, arial"><a name="HttpMock">class <strong>HttpMock</strong></a>(<a href="__builtin__.html#object">__builtin__.object</a>)</font></td></tr>
<tr bgcolor="#ffc8d8"><td rowspan=2><tt>&nbsp;&nbsp;&nbsp;</tt></td>
<td colspan=2><tt>Mock&nbsp;of&nbsp;httplib2.Http<br>&nbsp;</tt></td></tr>
<tr><td>&nbsp;</td>
<td width="100%">Methods defined here:<br>
<dl><dt><a name="HttpMock-__init__"><strong>__init__</strong></a>(self, filename, headers)</dt><dd><tt>Args:<br>
&nbsp;&nbsp;filename:&nbsp;string,&nbsp;absolute&nbsp;filename&nbsp;to&nbsp;read&nbsp;response&nbsp;from<br>
&nbsp;&nbsp;headers:&nbsp;dict,&nbsp;header&nbsp;to&nbsp;return&nbsp;with&nbsp;response</tt></dd></dl>
<dl><dt><a name="HttpMock-request"><strong>request</strong></a>(self, uri, method<font color="#909090">='GET'</font>, body<font color="#909090">=None</font>, headers<font color="#909090">=None</font>, redirections<font color="#909090">=1</font>, connection_type<font color="#909090">=None</font>)</dt></dl>
<hr>
Data descriptors defined here:<br>
<dl><dt><strong>__dict__</strong></dt>
<dd><tt>dictionary&nbsp;for&nbsp;instance&nbsp;variables&nbsp;(if&nbsp;defined)</tt></dd>
</dl>
<dl><dt><strong>__weakref__</strong></dt>
<dd><tt>list&nbsp;of&nbsp;weak&nbsp;references&nbsp;to&nbsp;the&nbsp;object&nbsp;(if&nbsp;defined)</tt></dd>
</dl>
</td></tr></table> <p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#ffc8d8">
<td colspan=3 valign=bottom>&nbsp;<br>
<font color="#000000" face="helvetica, arial"><a name="HttpRequest">class <strong>HttpRequest</strong></a>(<a href="__builtin__.html#object">__builtin__.object</a>)</font></td></tr>
<tr bgcolor="#ffc8d8"><td rowspan=2><tt>&nbsp;&nbsp;&nbsp;</tt></td>
@@ -143,7 +169,7 @@ Data descriptors defined here:<br>
<font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr>
<tr><td bgcolor="#55aa55"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
<td width="100%"><strong>__all__</strong> = ['HttpRequest', 'RequestMockBuilder']<br>
<td width="100%"><strong>__all__</strong> = ['HttpRequest', 'RequestMockBuilder', 'HttpMock']<br>
<strong>__author__</strong> = 'jcgregorio@google.com (Joe Gregorio)'</td></tr></table><p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#7799ee">

View File

@@ -32,7 +32,13 @@ except ImportError:
from cgi import parse_qs
from apiclient.discovery import build, key2param
from tests.util import HttpMock
from apiclient.http import HttpMock
DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
def datafile(filename):
return os.path.join(DATA_DIR, filename)
class Utilities(unittest.TestCase):
@@ -44,7 +50,7 @@ class Utilities(unittest.TestCase):
class Discovery(unittest.TestCase):
def test_method_error_checking(self):
self.http = HttpMock('buzz.json', {'status': '200'})
self.http = HttpMock(datafile('buzz.json'), {'status': '200'})
buzz = build('buzz', 'v1', self.http)
# Missing required parameters
@@ -76,7 +82,7 @@ class Discovery(unittest.TestCase):
self.assertTrue('unexpected' in str(e))
def test_buzz_resources(self):
self.http = HttpMock('buzz.json', {'status': '200'})
self.http = HttpMock(datafile('buzz.json'), {'status': '200'})
buzz = build('buzz', 'v1', self.http)
self.assertTrue(getattr(buzz, 'activities'))
self.assertTrue(getattr(buzz, 'feeds'))
@@ -87,7 +93,7 @@ class Discovery(unittest.TestCase):
self.assertTrue(getattr(buzz, 'related'))
def test_auth(self):
self.http = HttpMock('buzz.json', {'status': '200'})
self.http = HttpMock(datafile('buzz.json'), {'status': '200'})
buzz = build('buzz', 'v1', self.http)
auth = buzz.auth_discovery()
self.assertTrue('request' in auth)
@@ -95,7 +101,7 @@ class Discovery(unittest.TestCase):
def test_full_featured(self):
# Zoo should exercise all discovery facets
# and should also have no future.json file.
self.http = HttpMock('zoo.json', {'status': '200'})
self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
zoo = build('zoo', 'v1', self.http)
self.assertTrue(getattr(zoo, 'animals'))
request = zoo.animals().list(name="bat", projection="size")
@@ -105,7 +111,7 @@ class Discovery(unittest.TestCase):
self.assertEqual(q['projection'], ['size'])
def test_nested_resources(self):
self.http = HttpMock('zoo.json', {'status': '200'})
self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
zoo = build('zoo', 'v1', self.http)
self.assertTrue(getattr(zoo, 'animals'))
request = zoo.my().favorites().list(max_results="5")
@@ -114,7 +120,7 @@ class Discovery(unittest.TestCase):
self.assertEqual(q['max-results'], ['5'])
def test_top_level_functions(self):
self.http = HttpMock('zoo.json', {'status': '200'})
self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
zoo = build('zoo', 'v1', self.http)
self.assertTrue(getattr(zoo, 'query'))
request = zoo.query(q="foo")
@@ -125,7 +131,7 @@ class Discovery(unittest.TestCase):
class Next(unittest.TestCase):
def test_next_for_people_liked(self):
self.http = HttpMock('buzz.json', {'status': '200'})
self.http = HttpMock(datafile('buzz.json'), {'status': '200'})
buzz = build('buzz', 'v1', self.http)
people = {'links':
{'next':
@@ -136,7 +142,7 @@ class Next(unittest.TestCase):
class DeveloperKey(unittest.TestCase):
def test_param(self):
self.http = HttpMock('buzz.json', {'status': '200'})
self.http = HttpMock(datafile('buzz.json'), {'status': '200'})
buzz = build('buzz', 'v1', self.http, developerKey='foobie_bletch')
activities = {'links':
{'next':
@@ -147,7 +153,7 @@ class DeveloperKey(unittest.TestCase):
self.assertEqual(q['key'], ['foobie_bletch'])
def test_next_for_activities_list(self):
self.http = HttpMock('buzz.json', {'status': '200'})
self.http = HttpMock(datafile('buzz.json'), {'status': '200'})
buzz = build('buzz', 'v1', self.http, developerKey='foobie_bletch')
activities = {'links':
{'next':

View File

@@ -24,15 +24,22 @@ __author__ = 'jcgregorio@google.com (Joe Gregorio)'
from apiclient.errors import HttpError
from apiclient.discovery import build
from apiclient.http import RequestMockBuilder
from tests.util import HttpMock
from apiclient.http import HttpMock
import unittest
import httplib2
import os
import unittest
DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
def datafile(filename):
return os.path.join(DATA_DIR, filename)
class Mocks(unittest.TestCase):
def setUp(self):
self.http = HttpMock('buzz.json', {'status': '200'})
self.http = HttpMock(datafile('buzz.json'), {'status': '200'})
def test_default_response(self):
requestBuilder = RequestMockBuilder({})

View File

@@ -1,27 +0,0 @@
#!/usr/bin/python2.4
#
# Copyright 2010 Google Inc. All Rights Reserved.
"""One-line documentation for util module.
A detailed description of util.
"""
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
import httplib2
import os
DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
class HttpMock(object):
def __init__(self, filename, headers):
f = file(os.path.join(DATA_DIR, filename), 'r')
self.data = f.read()
f.close()
self.headers = headers
def request(self, uri, method="GET", body=None, headers=None, redirections=1, connection_type=None):
return httplib2.Response(self.headers), self.data