Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(11)

Unified Diff: Lib/test/test_unicode.py

Issue 9682: socket.create_connection error message for domain subpart with invalid length is very confusing
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Lib/encodings/idna.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
--- a/Lib/test/test_unicode.py Sun Apr 14 19:22:47 2013 +0200
+++ b/Lib/test/test_unicode.py Sun May 05 15:22:56 2013 -0400
@@ -10,9 +10,12 @@
import itertools
import struct
import sys
+
import unittest
+from encodings.idna import ToASCII
import warnings
from test import support, string_tests
+from unittest import mock
# Error handling (bad decoder return)
def search_function(encoding):
@@ -1673,6 +1676,83 @@
# Test whether trailing dot is preserved
self.assertEqual("www.python.org.".encode("idna"), b"www.python.org.")
+ def test_min_codecs_error_messages(self):
+ # Test Empty
+ self.assertRaisesRegexp(
+ UnicodeError,
+ '1..com',
+ '1..com'.encode,
+ 'idna')
+
+ def test_max_codecs_errors_messages(self):
+ # Test label greater then 64
+ label = "A" * 64
+ value = "first." + label + ".last"
+ self.assertRaisesRegexp(
+ UnicodeError,
+ label,
+ value.encode,
+ 'idna')
+ # Test last label length 64
+ value = "first.second." + label
+ self.assertRaisesRegexp(
+ UnicodeError,
+ label,
+ value.encode,
+ 'idna')
+ # Test last label length greater then 64
+ label += "A"
+ value += "A"
+ self.assertRaisesRegexp(
+ UnicodeError,
+ label,
+ value.encode,
+ 'idna')
+
+ def test_min_label_encode_ascii(self):
+ with self.assertRaisesRegexp(UnicodeError, "empty label"):
+ ToASCII('')
+
+ def test_max_label_encode_ascii(self):
+ label = "Z" * 64
+ with self.assertRaisesRegexp(UnicodeError, label):
+ ToASCII(label)
+
+ @mock.patch('encodings.idna.nameprep')
+ def test_escaped_min_label(self, nameprep):
+ label = mock.Mock()
+ label.encode.side_effect = UnicodeError
+ nameprep.return_value = ''
+ with self.assertRaisesRegexp(UnicodeError, "empty label"):
+ ToASCII(label)
+
+ @mock.patch('encodings.idna.nameprep')
+ def test_escaped_max_label(self, nameprep):
+ mock_label = mock.Mock()
+ mock_label.encode.side_effect = UnicodeError
+ label = "Z" * 64
+ nameprep.return_value = label
+ with self.assertRaisesRegexp(UnicodeError, label):
+ ToASCII(mock_label)
+
+ @mock.patch('encodings.idna.nameprep')
+ def test_escape_max_punycode_ascii(self, nameprep):
+ mock_label = mock.Mock()
+ mock_label.encode.side_effect = UnicodeError
+ name_space_label = mock.Mock()
+ expected_label = "Z" * 64
+ expected_label = expected_label.encode('ascii')
+ def encode(value):
+ if value == "ascii":
+ raise UnicodeError
+ return expected_label
+
+ name_space_label.encode = encode
+ name_space_label.startswith.return_value = False
+ nameprep.return_value = name_space_label
+ with self.assertRaisesRegexp(UnicodeError, "Z" * 64):
+ ToASCII(mock_label)
+
def test_codecs_errors(self):
# Error handling (encoding)
self.assertRaises(UnicodeError, 'Andr\202 x'.encode, 'ascii')
« no previous file with comments | « Lib/encodings/idna.py ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+