File: test_txrequests.py

package info (click to toggle)
python-txrequests 0.9.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 108 kB
  • sloc: python: 153; sh: 12; makefile: 2
file content (88 lines) | stat: -rw-r--r-- 2,939 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Tests for Requests."""

from requests import Response
from os import environ
from txrequests import Session
from twisted.trial.unittest import TestCase
from twisted.internet import defer

HTTPBIN = environ.get('HTTPBIN_URL', 'http://httpbin.org/')

def httpbin(*suffix):
    """Returns url for HTTPBIN resource."""
    return HTTPBIN + '/'.join(suffix)


class RequestsTestCase(TestCase):

    @defer.inlineCallbacks
    def test_session(self):
        # basic futures get
        with Session() as sess:
            d = sess.get(httpbin('get'))
            self.assertIsInstance(d, defer.Deferred)
            resp = yield d
            self.assertIsInstance(resp, Response)
            self.assertEqual(200, resp.status_code)

            # non-200, 404
            d = sess.get(httpbin('status/404'))
            resp = yield d
            self.assertEqual(404, resp.status_code)

            def cb(s, r):
                self.assertIsInstance(s, Session)
                self.assertIsInstance(r, Response)
                # add the parsed json data to the response
                r.data = r.json()
                return r

            d = sess.get(httpbin('get'), background_callback=cb)
            # this should block until complete
            resp = yield d
            self.assertEqual(200, resp.status_code)
            # make sure the callback was invoked
            self.assertTrue(hasattr(resp, 'data'))

            def rasing_cb(s, r):
                raise Exception('boom')

            d = sess.get(httpbin('get'), background_callback=rasing_cb)
            raised = False
            try:
                resp = yield d
            except Exception as e:
                self.assertEqual('boom', e.args[0])
                raised = True
            self.assertTrue(raised)

    def test_max_workers(self):
        """ Tests the `max_workers` shortcut. """
        from twisted.python.threadpool import ThreadPool

        with Session() as session:
            self.assertEqual(session.pool.max, 4)
        with Session(maxthreads=5) as session:
            self.assertEqual(session.pool.max, 5)
        with Session(pool=ThreadPool(maxthreads=10)) as session:
            self.assertEqual(session.pool.max, 10)
        with Session(pool=ThreadPool(maxthreads=10),
                                     maxthreads=5) as session:
            self.assertEqual(session.pool.max, 10)

    @defer.inlineCallbacks
    def test_redirect(self):
        """ Tests for the ability to cleanly handle redirects. """
        with Session() as sess:
            d = sess.get(httpbin('redirect-to?url=get'))
            resp = yield d
            self.assertIsInstance(resp, Response)
            self.assertEqual(200, resp.status_code)

            d = sess.get(httpbin('redirect-to?url=status/404'))
            resp = yield d
            self.assertEqual(404, resp.status_code)