aboutsummaryrefslogtreecommitdiff
path: root/test/test_exceptions.py
blob: b5bb93efb85a0cd8fadf304a7900a4c210520fb2 (plain)
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
import unittest
import pickle

from urllib3.exceptions import (HTTPError, MaxRetryError, LocationParseError,
                                ClosedPoolError, EmptyPoolError,
                                HostChangedError, ReadTimeoutError,
                                ConnectTimeoutError, HeaderParsingError)
from urllib3.connectionpool import HTTPConnectionPool



class TestPickle(unittest.TestCase):

    def verify_pickling(self, item):
        return pickle.loads(pickle.dumps(item))

    def test_exceptions(self):
        assert self.verify_pickling(HTTPError(None))
        assert self.verify_pickling(MaxRetryError(None, None, None))
        assert self.verify_pickling(LocationParseError(None))
        assert self.verify_pickling(ConnectTimeoutError(None))

    def test_exceptions_with_objects(self):
        assert self.verify_pickling(
            HTTPError('foo'))

        assert self.verify_pickling(
            HTTPError('foo', IOError('foo')))

        assert self.verify_pickling(
            MaxRetryError(HTTPConnectionPool('localhost'), '/', None))

        assert self.verify_pickling(
            LocationParseError('fake location'))

        assert self.verify_pickling(
            ClosedPoolError(HTTPConnectionPool('localhost'), None))

        assert self.verify_pickling(
            EmptyPoolError(HTTPConnectionPool('localhost'), None))

        assert self.verify_pickling(
            HostChangedError(HTTPConnectionPool('localhost'), '/', None))

        assert self.verify_pickling(
            ReadTimeoutError(HTTPConnectionPool('localhost'), '/', None))


class TestFormat(unittest.TestCase):
    def test_header_parsing_errors(self):
        hpe = HeaderParsingError('defects', 'unparsed_data')

        self.assertTrue('defects' in str(hpe))
        self.assertTrue('unparsed_data' in str(hpe))