Repository: django/asgiref
Pull request #5 (2016-09-05 21:11:42)

Channel and group name error enchancement
Fixes issue stated in [Django Channels ticket 336](https://github.com/andrewgodwin/channels/issues/336)


Patch:
diff --git a/.gitignore b/.gitignore
index 0c31ad1..8b601cd 100755
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,5 @@
 *.egg-info
 dist/
 build/
+__pycache__/
+__pycache__/*
diff --git a/asgiref/base_layer.py b/asgiref/base_layer.py
index c85cfd5..652b793 100755
--- a/asgiref/base_layer.py
+++ b/asgiref/base_layer.py
@@ -82,21 +82,25 @@ class BaseChannelLayer(object):
                 return capacity
         return self.capacity
 
+    def match_type_and_length(self, name):
+        if (len(name) < 100) and (isinstance(name, six.text_type)):
+            return True
+        return False
+
     ### Name validation functions
 
     channel_name_regex = re.compile(r"^[a-zA-Z\d\-_.]+((\?|\!)[\d\w\-_.]+)?$")
     group_name_regex = re.compile(r"^[a-zA-Z\d\-_.]+$")
+    invalid_name_error = "{} name must be a valid unicode string containing only alphanumerics, hyphens, or periods."
 
     def valid_channel_name(self, name):
-        if len(name) > 100:
-            return False
-        if not isinstance(name, six.text_type):
-            return False
-        return bool(self.channel_name_regex.match(name))
+        if self.match_type_and_length(name):
+            if bool(self.channel_name_regex.match(name)):
+                return True
+        raise TypeError("Channel name must be a valid unicode string containing only alphanumerics, hyphens, or periods.")
 
     def valid_group_name(self, name):
-        if len(name) > 100:
-            return False
-        if not isinstance(name, six.text_type):
-            return False
-        return bool(self.group_name_regex.match(name))
+        if self.match_type_and_length(name):
+            if bool(self.group_name_regex.match(name)):
+                return True
+        raise TypeError("Group name must be a valid unicode string containing only alphanumerics, hyphens, or periods.")


Test patch:
diff --git a/asgiref/tests/test_base_layer.py b/asgiref/tests/test_base_layer.py
index 35fd927..f6c5bc0 100755
--- a/asgiref/tests/test_base_layer.py
+++ b/asgiref/tests/test_base_layer.py
@@ -32,11 +32,20 @@ class BaseLayerTests(unittest.TestCase):
         self.assertTrue(layer.valid_channel_name("http.request.body?ab0def"))
         self.assertTrue(layer.valid_channel_name("0.a_b-c?d_e-f.1"))
 
-        self.assertFalse(layer.valid_channel_name("http.request\u00a3"))
-        self.assertFalse(layer.valid_channel_name("way.too.long" * 10))
-        self.assertFalse(layer.valid_channel_name("one?two?three"))
-        self.assertFalse(layer.valid_channel_name("four!five!six"))
-        self.assertFalse(layer.valid_channel_name("some+other!thing"))
+        with self.assertRaises(TypeError):
+            layer.valid_channel_name("http.request\u00a3")
+
+        with self.assertRaises(TypeError):
+            layer.valid_channel_name("way.too.long" * 10)
+
+        with self.assertRaises(TypeError):
+            layer.valid_channel_name("one?two?three")
+
+        with self.assertRaises(TypeError):
+            layer.valid_channel_name("four!five!six")
+
+        with self.assertRaises(TypeError):
+            layer.valid_channel_name("some+other!thing")
 
     def test_valid_group_name(self):
         """
@@ -46,8 +55,13 @@ class BaseLayerTests(unittest.TestCase):
         self.assertTrue(layer.valid_group_name("foo.bar"))
         self.assertTrue(layer.valid_group_name("0.a_b-c"))
 
-        self.assertFalse(layer.valid_group_name("foo.bar?baz"))
-        self.assertFalse(layer.valid_group_name("foo.bar!baz"))
-        self.assertFalse(layer.valid_group_name("foo.bar\u00a3"))
-        self.assertFalse(layer.valid_group_name("way.too.long" * 10))
-        self.assertFalse(layer.valid_group_name("some+other=thing"))
+        with self.assertRaises(TypeError):
+            layer.valid_group_name("foo.bar?baz")
+        with self.assertRaises(TypeError):
+            layer.valid_group_name("foo.bar!baz")
+        with self.assertRaises(TypeError):
+            layer.valid_group_name("foo.bar\u00a3")
+        with self.assertRaises(TypeError):
+            layer.valid_group_name("way.too.long" * 10)
+        with self.assertRaises(TypeError):
+            layer.valid_group_name("some+other=thing")