~ubuntu-branches/ubuntu/trusty/requests/trusty-proposed

« back to all changes in this revision

Viewing changes to tests/test_requests.py

  • Committer: Package Import Robot
  • Author(s): Daniele Tricoli
  • Date: 2012-05-04 14:34:47 UTC
  • mfrom: (1.1.9)
  • Revision ID: package-import@ubuntu.com-20120504143447-3psyffw2slct0a1j
Tags: 0.12.1-1
* New upstream release
* debian/control
  - Added python-oauthlib to python-requests' Recommends field
* debian/patches/01_do-not-use-python-certifi.patch
  - Refreshed

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
 
11
11
import json
12
12
import os
13
 
import sys
14
13
import unittest
15
14
import pickle
16
15
 
52
51
            # time.sleep(1)
53
52
            _httpbin = True
54
53
 
55
 
 
56
 
class RequestsTestSuite(TestSetup, unittest.TestCase):
 
54
class TestBaseMixin(object):
 
55
 
 
56
    def assertCookieHas(self, cookie, **kwargs):
 
57
        """Assert that a cookie has various specified properties."""
 
58
        for attr, expected_value in kwargs.items():
 
59
            cookie_attr = getattr(cookie, attr)
 
60
            message = 'Failed comparison for %s: %s != %s' % (attr, cookie_attr, expected_value)
 
61
            self.assertEqual(cookie_attr, expected_value, message)
 
62
 
 
63
class RequestsTestSuite(TestSetup, TestBaseMixin, unittest.TestCase):
57
64
    """Requests test cases."""
58
65
 
59
66
    def test_entry_points(self):
322
329
 
323
330
            self.assertEqual(post2.status_code, 200)
324
331
 
 
332
    def test_POSTBIN_GET_POST_FILES_STRINGS(self):
 
333
 
 
334
        for service in SERVICES:
 
335
 
 
336
            url = service('post')
 
337
 
 
338
            post1 = post(url, files={'fname.txt': 'fdata'})
 
339
            self.assertEqual(post1.status_code, 200)
 
340
 
 
341
            post2 = post(url, files={'fname.txt': 'fdata', 'fname2.txt':'more fdata'})
 
342
            self.assertEqual(post2.status_code, 200)
 
343
 
 
344
            post3 = post(url, files={'fname.txt': 'fdata', 'fname2.txt':open(__file__,'rb')})
 
345
            self.assertEqual(post3.status_code, 200)
 
346
 
 
347
            post4 = post(url, files={'fname.txt': 'fdata'})
 
348
            self.assertEqual(post4.status_code, 200)
 
349
 
 
350
            post5 = post(url, files={'file': ('file.txt', 'more fdata')})
 
351
            self.assertEqual(post5.status_code, 200)
 
352
 
 
353
            post6 = post(url, files={'fname.txt': '\xe9'})
 
354
            self.assertEqual(post6.status_code, 200)
 
355
 
 
356
            post7 = post(url, files={'fname.txt': 'fdata to verify'})
 
357
            rbody = json.loads(post7.text)
 
358
            self.assertTrue(rbody.get('files', None))
 
359
            self.assertTrue(rbody['files'].get('fname.txt'), None)
 
360
            self.assertEqual(rbody['files']['fname.txt'], 'fdata to verify')
 
361
 
 
362
 
325
363
    def test_nonzero_evaluation(self):
326
364
 
327
365
        for service in SERVICES:
632
670
 
633
671
        # Those cookies persist transparently.
634
672
        c = json.loads(r.text).get('cookies')
635
 
        assert c == _c
 
673
        self.assertEqual(c, _c)
636
674
 
637
675
        # Double check.
638
676
        r = get(httpbin('cookies'), cookies={}, session=s)
639
677
        c = json.loads(r.text).get('cookies')
640
 
        assert c == _c
 
678
        self.assertEqual(c, _c)
641
679
 
642
680
        # Remove a cookie by setting it's value to None.
643
681
        r = get(httpbin('cookies'), cookies={'bessie': None}, session=s)
644
682
        c = json.loads(r.text).get('cookies')
645
683
        del _c['bessie']
646
 
        assert c == _c
 
684
        self.assertEqual(c, _c)
647
685
 
648
686
        # Test session-level cookies.
649
687
        s = requests.session(cookies=_c)
650
688
        r = get(httpbin('cookies'), session=s)
651
689
        c = json.loads(r.text).get('cookies')
652
 
        assert c == _c
 
690
        self.assertEqual(c, _c)
653
691
 
654
692
        # Have the server set a cookie.
655
693
        r = get(httpbin('cookies', 'set', 'k', 'v'), allow_redirects=True, session=s)
698
736
        ds = pickle.loads(pickle.dumps(s))
699
737
 
700
738
        self.assertEqual(s.headers, ds.headers)
701
 
        self.assertEqual(s.cookies, ds.cookies)
702
739
        self.assertEqual(s.auth, ds.auth)
703
740
 
 
741
        # Cookie doesn't have a good __eq__, so verify manually:
 
742
        self.assertEqual(len(ds.cookies), 1)
 
743
        for cookie in ds.cookies:
 
744
            self.assertCookieHas(cookie, name='a-cookie', value='cookie-value')
 
745
 
704
746
    def test_unpickled_session_requests(self):
705
747
        s = requests.session()
706
748
        r = get(httpbin('cookies', 'set', 'k', 'v'), allow_redirects=True, session=s)
837
879
        r = requests.get(httpbin('status', '404'))
838
880
        r.text
839
881
 
840
 
    def test_no_content(self):
841
 
        r = requests.get(httpbin('status', "0"), config={"safe_mode":True})
842
 
        r.content
843
 
        r.content
 
882
    def test_max_redirects(self):
 
883
        """Test the max_redirects config variable, normally and under safe_mode."""
 
884
        def unsafe_callable():
 
885
            requests.get(httpbin('redirect', '3'), config=dict(max_redirects=2))
 
886
        self.assertRaises(requests.exceptions.TooManyRedirects, unsafe_callable)
 
887
 
 
888
        # add safe mode
 
889
        response = requests.get(httpbin('redirect', '3'), config=dict(safe_mode=True, max_redirects=2))
 
890
        self.assertTrue(response.content is None)
 
891
        self.assertTrue(isinstance(response.error, requests.exceptions.TooManyRedirects))
 
892
 
 
893
    def test_connection_keepalive_and_close(self):
 
894
        """Test that we send 'Connection: close' when keep_alive is disabled."""
 
895
        # keep-alive should be on by default
 
896
        r1 = requests.get(httpbin('get'))
 
897
        # XXX due to proxying issues, test the header sent back by httpbin, rather than
 
898
        # the header reported in its message body. See kennethreitz/httpbin#46
 
899
        self.assertEqual(r1.headers['Connection'].lower(), 'keep-alive')
 
900
 
 
901
        # but when we disable it, we should send a 'Connection: close'
 
902
        # and get the same back:
 
903
        r2 = requests.get(httpbin('get'), config=dict(keep_alive=False))
 
904
        self.assertEqual(r2.headers['Connection'].lower(), 'close')
844
905
 
845
906
if __name__ == '__main__':
846
907
    unittest.main()