Move HttpMock out of tests and into apiclient.http. Update tests that used HttpMock
diff --git a/apiclient/http.py b/apiclient/http.py
index 85ff93a..e9fd7e0 100644
--- a/apiclient/http.py
+++ b/apiclient/http.py
@@ -9,10 +9,12 @@
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
__all__ = [
- 'HttpRequest', 'RequestMockBuilder'
+ 'HttpRequest', 'RequestMockBuilder', 'HttpMock'
]
import httplib2
+import os
+
from model import JsonModel
@@ -147,3 +149,20 @@
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
diff --git a/docs/apiclient.http.html b/docs/apiclient.http.html
index a378677..b18e2de 100644
--- a/docs/apiclient.http.html
+++ b/docs/apiclient.http.html
@@ -22,7 +22,8 @@
<tr><td bgcolor="#aa55cc"><tt> </tt></td><td> </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> <br>
@@ -33,7 +34,8 @@
<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 @@
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#ffc8d8">
<td colspan=3 valign=bottom> <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> </tt></td>
+<td colspan=2><tt>Mock of httplib2.Http<br> </tt></td></tr>
+<tr><td> </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>
+ filename: string, absolute filename to read response from<br>
+ headers: dict, header to return with 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 for instance variables (if defined)</tt></dd>
+</dl>
+<dl><dt><strong>__weakref__</strong></dt>
+<dd><tt>list of weak references to the object (if 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> <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> </tt></td>
@@ -143,7 +169,7 @@
<font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr>
<tr><td bgcolor="#55aa55"><tt> </tt></td><td> </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">
diff --git a/tests/test_discovery.py b/tests/test_discovery.py
index 541210c..7c00bb9 100644
--- a/tests/test_discovery.py
+++ b/tests/test_discovery.py
@@ -32,7 +32,13 @@
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 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 @@
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 @@
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 @@
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 @@
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 @@
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 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 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 @@
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':
diff --git a/tests/test_mocks.py b/tests/test_mocks.py
index ee8ef26..2c66aab 100644
--- a/tests/test_mocks.py
+++ b/tests/test_mocks.py
@@ -24,15 +24,22 @@
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({})
diff --git a/tests/util.py b/tests/util.py
deleted file mode 100644
index 101079f..0000000
--- a/tests/util.py
+++ /dev/null
@@ -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