Browse Source

100% coverage for celery.task.http

Ask Solem 15 years ago
parent
commit
15ba030784
3 changed files with 24 additions and 16 deletions
  1. 6 14
      celery/task/http.py
  2. 13 2
      celery/tests/test_task_http.py
  3. 5 0
      celery/utils/compat.py

+ 6 - 14
celery/task/http.py

@@ -1,9 +1,4 @@
 import urllib2
-import warnings
-try:
-    from urlparse import parse_qsl
-except ImportError:
-    from cgi import parse_qsl
 from urllib import urlencode
 from urlparse import urlparse
 
@@ -11,6 +6,9 @@ from anyjson import deserialize
 
 from celery import __version__ as celery_version
 from celery.task.base import Task as BaseTask
+from celery.utils.compat import parse_qsl
+
+GET_METHODS = frozenset(["GET", "HEAD"])
 
 
 class InvalidResponseError(Exception):
@@ -75,7 +73,7 @@ class MutableURL(object):
         return "".join(filter(None, components))
 
     def __repr__(self):
-        return "<%s %s>" % (self.__class__.__name__, str(self))
+        return "<%s: %s>" % (self.__class__.__name__, str(self))
 
     def _get_query(self):
         return self._query
@@ -116,18 +114,12 @@ class HttpDispatch(object):
         """Dispatches the callback and returns the raw response text."""
         url = MutableURL(self.url)
         params = None
-        if self.method == "GET":
+        if self.method in GET_METHODS:
             url.query.update(self.task_kwargs)
-        elif self.method == "POST":
+        else:
             params = urlencode(utf8dict(self.task_kwargs.items()))
         return self.make_request(str(url), self.method, params)
 
-    def execute(self):
-        warnings.warn(DeprecationWarning(
-            "execute() has been deprecated and is scheduled for removal in \
-            celery v1.2, please use dispatch() instead."))
-        return self.dispatch()
-
     def dispatch(self):
         """Dispatch callback and return result."""
         response = self._dispatch_raw()

+ 13 - 2
celery/tests/test_task_http.py

@@ -1,6 +1,7 @@
 # -*- coding: utf-8 -*-
 from __future__ import with_statement
 
+import sys
 import logging
 import unittest
 from urllib import addinfourl
@@ -54,8 +55,8 @@ def unknown_response():
 class TestEncodings(unittest.TestCase):
 
     def test_utf8dict(self):
-        d = {u"følelser ær langé": "ærbadægzaååÆØÅ",
-              "foobar": "xuzzybaz"}
+        d = {u"følelser ær langé": u"ærbadægzaååÆØÅ",
+              "foobar".encode("utf-8"): "xuzzybaz".encode("utf-8")}
 
         for key, value in http.utf8dict(d.items()).items():
             self.assertTrue(isinstance(key, str))
@@ -83,6 +84,16 @@ class TestMutableURL(unittest.TestCase):
         self.assertEquals(str(url).split("?")[0],
             "https://e.com:808/foo/bar#zeta")
 
+    def test___repr__(self):
+        url = http.MutableURL("http://e.com/foo/bar")
+        self.assertTrue(repr(url).startswith("<MutableURL: http://e.com"))
+
+    def test_set_query(self):
+        url = http.MutableURL("http://e.com/foo/bar/?x=10")
+        url.query = {"zzz": "xxx"}
+        url = http.MutableURL(str(url))
+        self.assertEquals(url.query, {"zzz": "xxx"})
+
 
 class TestHttpDispatch(unittest.TestCase):
 

+ 5 - 0
celery/utils/compat.py

@@ -1,3 +1,8 @@
+try:
+    from urlparse import parse_qsl
+except ImportError:
+    from cgi import parse_qsl
+
 try:
     from collections import defaultdict
 except ImportError: