← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~allenap/maas/apiclient-httplib2 into lp:maas

 

Gavin Panella has proposed merging lp:~allenap/maas/apiclient-httplib2 into lp:maas.

Commit message:
Use httplib2 in apiclient.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~allenap/maas/apiclient-httplib2/+merge/128585
-- 
https://code.launchpad.net/~allenap/maas/apiclient-httplib2/+merge/128585
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~allenap/maas/apiclient-httplib2 into lp:maas.
=== modified file 'src/apiclient/maas_client.py'
--- src/apiclient/maas_client.py	2012-10-05 07:38:54 +0000
+++ src/apiclient/maas_client.py	2012-10-08 20:59:22 +0000
@@ -17,9 +17,9 @@
     ]
 
 from urllib import urlencode
-import urllib2
 
 from apiclient.multipart import encode_multipart_data
+import httplib2
 import oauth.oauth as oauth
 
 
@@ -68,6 +68,11 @@
     provider in Juju for the code this would require.
     """
 
+    def __init__(self, insecure=False):
+        super(MAASDispatcher, self).__init__()
+        self.http = httplib2.Http(
+            disable_ssl_certificate_validation=insecure)
+
     def dispatch_query(self, request_url, headers, method="GET", data=None):
         """Synchronously dispatch an OAuth-signed request to L{request_url}.
 
@@ -81,8 +86,8 @@
 
         :return: A open file-like object that contains the response.
         """
-        req = urllib2.Request(request_url, data, headers)
-        return urllib2.urlopen(req)
+        return self.http.request(
+            request_url, method, body=data, headers=headers)
 
 
 class MAASClient:

=== modified file 'src/apiclient/tests/test_maas_client.py'
--- src/apiclient/tests/test_maas_client.py	2012-10-05 07:38:54 +0000
+++ src/apiclient/tests/test_maas_client.py	2012-10-08 20:59:22 +0000
@@ -12,6 +12,7 @@
 __metaclass__ = type
 __all__ = []
 
+from os.path import relpath
 from random import randint
 from urlparse import (
     parse_qs,
@@ -26,7 +27,10 @@
     )
 from apiclient.testing.django import parse_headers_and_body_with_django
 from maastesting.factory import factory
+from maastesting.fixtures import ProxiesDisabledFixture
+from maastesting.httpd import HTTPServerFixture
 from maastesting.testcase import TestCase
+from mock import sentinel
 
 
 class TestMAASOAuth(TestCase):
@@ -40,11 +44,37 @@
 
 class TestMAASDispatcher(TestCase):
 
+    def setUp(self):
+        super(TestMAASDispatcher, self).setUp()
+        self.useFixture(ProxiesDisabledFixture())
+
     def test_dispatch_query_makes_direct_call(self):
-        contents = factory.getRandomString()
-        url = "file://%s" % self.make_file(contents=contents)
-        self.assertEqual(
-            contents, MAASDispatcher().dispatch_query(url, {}).read())
+        dispatch_query = MAASDispatcher().dispatch_query
+        filename = relpath(__file__)
+        with HTTPServerFixture() as httpd:
+            url = urljoin(httpd.url, filename)
+            body_from_dispatcher = dispatch_query(url, {})[1]
+        with open(filename, "rb") as file_in:
+            body_from_file = file_in.read()
+        self.assertEqual(
+            body_from_file, body_from_dispatcher,
+            "The content of %s differs from %s." % (url, filename))
+
+    def test_insecure_argument_passed_to_httplib2(self):
+        dispatcher = MAASDispatcher(sentinel.insecure)
+        self.assertEqual(
+            sentinel.insecure,
+            dispatcher.http.disable_ssl_certificate_validation)
+
+    def test_dispatch_query_passes_args_to_httplib2(self):
+        dispatcher = MAASDispatcher()
+        request = self.patch(dispatcher.http, "request")
+        dispatcher.dispatch_query(
+            request_url=sentinel.request_url, headers=sentinel.headers,
+            method=sentinel.method, data=sentinel.data)
+        request.assert_called_once_with(
+            sentinel.request_url, sentinel.method, headers=sentinel.headers,
+            body=sentinel.data)
 
 
 def make_url():