r46976 - make tests a bit more compact

hawkowl-TA+aISz0psMTMxyoc4vAAJOcrHinNvQL0E9HWUfgJXw@public.gmane.org Thu, 10 Mar 2016 18:12:43 -0700 (MST)
Newsgroups gmane.comp.python.twisted.commits
Message-ID <[email protected]>
Author: hawkowl
Date: Thu Mar 10 18:12:39 2016
New Revision: 46976

Modified:
   branches/addCookie-str-8067/twisted/web/test/test_http.py

Log:
make tests a bit more compact

Modified: branches/addCookie-str-8067/twisted/web/test/test_http.py
==============================================================================
--- branches/addCookie-str-8067/twisted/web/test/test_http.py	(original)
+++ branches/addCookie-str-8067/twisted/web/test/test_http.py	Thu Mar 10 18:12:39 2016
@@ -1526,18 +1526,15 @@
         self.assertEqual(req.responseHeaders.getRawHeaders(b"test"), [b"lemur"])
 
 
-    def test_addCookieWithMinimumArgumentsUnicode(self):
+    def _checkCookie(self, expectedCookieValue, *args, **kwargs):
         """
-        L{http.Request.setCookie} adds a new cookie to be sent with the
-        response, and can be called with just a key and a value. L{unicode}
-        arguments are encoded using UTF-8.
+        Call L{http.Request.setCookie} with C{*args} and C{**kwargs}, and check
+        that the cookie value is equal to C{expectedCookieValue}.
         """
-        expectedCookieValue = b"foo=bar"
-
         channel = DummyChannel()
         req = http.Request(channel, False)
-        req.addCookie(u"foo", u"bar")
-        self.assertEqual(req.cookies[0], b"foo=bar")
+        req.addCookie(*args, **kwargs)
+        self.assertEqual(req.cookies[0], expectedCookieValue)
 
         # Write nothing to make it produce the headers
         req.write(b"")
@@ -1551,6 +1548,17 @@
                          b"Set-Cookie: " + expectedCookieValue)
 
 
+    def test_addCookieWithMinimumArgumentsUnicode(self):
+        """
+        L{http.Request.setCookie} adds a new cookie to be sent with the
+        response, and can be called with just a key and a value. L{unicode}
+        arguments are encoded using UTF-8.
+        """
+        expectedCookieValue = b"foo=bar"
+
+        self._checkCookie(expectedCookieValue, u"foo", u"bar")
+
+
     def test_addCookieWithAllArgumentsUnicode(self):
         """
         L{http.Request.setCookie} adds a new cookie to be sent with the
@@ -1561,24 +1569,10 @@
             b"Domain=.example.com; Path=/; Max-Age=31536000; "
             b"Comment=test; Secure; HttpOnly")
 
-        channel = DummyChannel()
-        req = http.Request(channel, False)
-        req.addCookie(
+        self._checkCookie(expectedCookieValue,
             u"foo", u"bar", expires=u"Fri, 31 Dec 9999 23:59:59 GMT",
             domain=u".example.com", path=u"/", max_age=u"31536000",
             comment=u"test", secure=True, httpOnly=True)
-        self.assertEqual(req.cookies[0], expectedCookieValue)
-
-        # Write nothing to make it produce the headers
-        req.write(b"")
-        writtenLines = channel.transport.written.getvalue().split(b"\r\n")
-
-        # There should be one Set-Cookie header
-        setCookieLines = [x for x in writtenLines
-                          if x.startswith(b"Set-Cookie")]
-        self.assertEqual(len(setCookieLines), 1)
-        self.assertEqual(setCookieLines[0],
-                         b"Set-Cookie: " + expectedCookieValue)
 
 
     def test_addCookieWithMinimumArgumentsBytes(self):
@@ -1589,21 +1583,7 @@
         """
         expectedCookieValue = b"foo=bar"
 
-        channel = DummyChannel()
-        req = http.Request(channel, False)
-        req.addCookie(b"foo", b"bar")
-        self.assertEqual(req.cookies[0], b"foo=bar")
-
-        # Write nothing to make it produce the headers
-        req.write(b"")
-        writtenLines = channel.transport.written.getvalue().split(b"\r\n")
-
-        # There should be one Set-Cookie header
-        setCookieLines = [x for x in writtenLines
-                          if x.startswith(b"Set-Cookie")]
-        self.assertEqual(len(setCookieLines), 1)
-        self.assertEqual(setCookieLines[0],
-                         b"Set-Cookie: " + expectedCookieValue)
+        self._checkCookie(expectedCookieValue, b"foo", b"bar")
 
 
     def test_addCookieWithAllArgumentsBytes(self):
@@ -1616,24 +1596,10 @@
             b"Domain=.example.com; Path=/; Max-Age=31536000; "
             b"Comment=test; Secure; HttpOnly")
 
-        channel = DummyChannel()
-        req = http.Request(channel, False)
-        req.addCookie(
+        self._checkCookie(expectedCookieValue,
             b"foo", b"bar", expires=b"Fri, 31 Dec 9999 23:59:59 GMT",
             domain=b".example.com", path=b"/", max_age=b"31536000",
             comment=b"test", secure=True, httpOnly=True)
-        self.assertEqual(req.cookies[0], expectedCookieValue)
-
-        # Write nothing to make it produce the headers
-        req.write(b"")
-        writtenLines = channel.transport.written.getvalue().split(b"\r\n")
-
-        # There should be one Set-Cookie header
-        setCookieLines = [x for x in writtenLines
-                          if x.startswith(b"Set-Cookie")]
-        self.assertEqual(len(setCookieLines), 1)
-        self.assertEqual(setCookieLines[0],
-                         b"Set-Cookie: " + expectedCookieValue)
 
 
     def test_addCookieNonStringArgument(self):
@@ -1644,23 +1610,9 @@
         """
         expectedCookieValue = b"foo=10"
 
-        channel = DummyChannel()
-        req = http.Request(channel, False)
-        req.addCookie(b"foo", 10)
-        self.assertEqual(req.cookies[0], expectedCookieValue)
-
-        # Write nothing to make it produce the headers
-        req.write(b"")
-        writtenLines = channel.transport.written.getvalue().split(b"\r\n")
-
-        # There should be one Set-Cookie header
-        setCookieLines = [x for x in writtenLines
-                          if x.startswith(b"Set-Cookie")]
-        self.assertEqual(len(setCookieLines), 1)
-        self.assertEqual(setCookieLines[0],
-                         b"Set-Cookie: " + expectedCookieValue)
+        self._checkCookie(expectedCookieValue, b"foo", 10)
 
-        warnings = self.flushWarnings([self.test_addCookieNonStringArgument])
+        warnings = self.flushWarnings([self._checkCookie])
         self.assertEqual(1, len(warnings))
         self.assertEqual(warnings[0]['category'], DeprecationWarning)
         self.assertEqual(