← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~rvb/maas/maas-add-proxy-test into lp:maas

 

Raphaël Badin has proposed merging lp:~rvb/maas/maas-add-proxy-test into lp:maas.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~rvb/maas/maas-add-proxy-test/+merge/97593

Test views.proxy_to_longpoll.
-- 
https://code.launchpad.net/~rvb/maas/maas-add-proxy-test/+merge/97593
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~rvb/maas/maas-add-proxy-test into lp:maas.
=== modified file 'src/maasserver/testing/factory.py'
--- src/maasserver/testing/factory.py	2012-03-12 07:25:31 +0000
+++ src/maasserver/testing/factory.py	2012-03-15 09:12:25 +0000
@@ -13,6 +13,7 @@
     "factory",
     ]
 
+import httplib
 from io import BytesIO
 import random
 import string
@@ -38,6 +39,9 @@
     def getRandomEmail(self, login_size=10):
         return "%s@xxxxxxxxxxx" % self.getRandomString(size=login_size)
 
+    def getRandomStatusCode(self):
+        return random.choice(list(httplib.responses))
+
     def getRandomBoolean(self):
         return random.choice((True, False))
 

=== modified file 'src/maasserver/tests/test_views.py'
--- src/maasserver/tests/test_views.py	2012-03-14 15:56:49 +0000
+++ src/maasserver/tests/test_views.py	2012-03-15 09:12:25 +0000
@@ -11,7 +11,9 @@
 __metaclass__ = type
 __all__ = []
 
+from collections import namedtuple
 import httplib
+import urllib2
 
 from django.conf import settings
 from django.conf.urls.defaults import patterns
@@ -31,7 +33,10 @@
     LoggedInTestCase,
     TestCase,
     )
-from maasserver.views import get_longpoll_context
+from maasserver.views import (
+    get_longpoll_context,
+    proxy_to_longpoll,
+    )
 
 
 def get_prefixed_form_data(prefix, data):
@@ -103,6 +108,45 @@
             'select#id_after_commissioning_action')
 
 
+class TestProxyView(LoggedInTestCase):
+    """Test the (dev) view used to proxy request to a txlongpoll server."""
+
+    def test_proxy_to_longpoll(self):
+        # Set LONGPOLL_SERVER_URL (to a random string).
+        longpoll_server_url = factory.getRandomString()
+        self.patch(settings, 'LONGPOLL_SERVER_URL', longpoll_server_url)
+
+        # Create content of the fake reponse.
+        query_string = factory.getRandomString()
+        mimetype = factory.getRandomString()
+        content = factory.getRandomString()
+        status_code = factory.getRandomStatusCode()
+
+        # Monkey patch urllib2.urlopen to make it return a (fake) response
+        # with status_code=code, headers.typeheader=mimetype and a
+        # 'read' method that will return 'content'.
+        def urlopen(url):
+            # Assert that urlopen is called on the longpoll url (plus
+            # additional parameters taken from the original request's
+            # query string).
+            self.assertEqual(
+                '%s?%s' % (longpoll_server_url, query_string), url)
+            FakeProxiedResponse = namedtuple(
+                'FakeProxiedResponse', 'code headers read')
+            headers = namedtuple('Headers', 'typeheader')(mimetype)
+            return FakeProxiedResponse(status_code, headers, lambda: content)
+        self.patch(urllib2, 'urlopen', urlopen)
+
+        # Create a fake request.
+        request = namedtuple(
+            'FakeRequest', ['META'])({'QUERY_STRING': query_string})
+        response = proxy_to_longpoll(request)
+
+        self.assertEqual(content, response.content)
+        self.assertEqual(mimetype, response['Content-Type'])
+        self.assertEqual(status_code, response.status_code)
+
+
 class TestComboLoaderView(TestCase):
     """Test combo loader view."""
 

=== modified file 'src/maasserver/views.py'
--- src/maasserver/views.py	2012-03-14 14:03:33 +0000
+++ src/maasserver/views.py	2012-03-15 09:12:25 +0000
@@ -247,10 +247,11 @@
 
     if 'QUERY_STRING' in request.META:
         url += '?' + request.META['QUERY_STRING']
-    proxied_request = urllib2.urlopen(url)
-    status_code = proxied_request.code
-    mimetype = proxied_request.headers.typeheader or mimetypes.guess_type(url)
-    content = proxied_request.read()
+    proxied_response = urllib2.urlopen(url)
+    status_code = proxied_response.code
+    mimetype = (
+        proxied_response.headers.typeheader or mimetypes.guess_type(url))
+    content = proxied_response.read()
     return HttpResponse(content, status=status_code, mimetype=mimetype)