test_requests: Add tests for morsel_to_cookie
authorMichael Becker <mike@beckerfuffle.com>
Tue, 3 Dec 2013 03:14:41 +0000 (22:14 -0500)
committerMichael Becker <mike@beckerfuffle.com>
Fri, 6 Dec 2013 13:56:30 +0000 (08:56 -0500)
These tests will ensure my changes to how we handle 'expires' don't cause any regressions.

test_requests.py

index a800ba7..4134153 100755 (executable)
@@ -6,15 +6,16 @@
 from __future__ import division
 import json
 import os
-import unittest
 import pickle
+import unittest
 
 import requests
 import pytest
-from requests.auth import HTTPDigestAuth
 from requests.adapters import HTTPAdapter
-from requests.compat import str, cookielib, getproxies, urljoin, urlparse
-from requests.cookies import cookiejar_from_dict
+from requests.auth import HTTPDigestAuth
+from requests.compat import (
+    Morsel, cookielib, getproxies, str, urljoin, urlparse)
+from requests.cookies import cookiejar_from_dict, morsel_to_cookie
 from requests.exceptions import InvalidURL, MissingSchema
 from requests.structures import CaseInsensitiveDict
 
@@ -759,6 +760,24 @@ class RequestsTestCase(unittest.TestCase):
             preq = req.prepare()
             assert test_url == preq.url
 
+    def test_morsel_to_cookie_expires_is_converted(self):
+        morsel = Morsel()
+
+        # Test case where we convert from string time
+        morsel['expires'] = 'Thu, 01-Jan-1970 00:00:01 GMT'
+        cookie = morsel_to_cookie(morsel)
+        self.assertEquals(cookie.expires, 18001)
+
+        # Test case where no conversion is required
+        morsel['expires'] = 100
+        cookie = morsel_to_cookie(morsel)
+        self.assertEquals(cookie.expires, 100)
+
+        # Test case where an invalid string is input
+        morsel['expires'] = 'woops'
+        with self.assertRaises(ValueError):
+            cookie = morsel_to_cookie(morsel)
+
 
 class TestContentEncodingDetection(unittest.TestCase):