Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / connection / test.py @ e7e7dbbd

History | View | Annotate | Download (3 kB)

1
# Copyright 2013 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, self.list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, self.list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
from unittest import TestCase, TestSuite, TextTestRunner, TestLoader
35
from mock import Mock
36

    
37

    
38
class HTTPResponse(TestCase):
39

    
40
    def setUp(self):
41
        from kamaki.clients.connection import HTTPResponse as HTTPR
42
        self.resp = HTTPR('Abstract class, so test with fake request (str)')
43

    
44
    def _mock_get_response(foo):
45
        def mocker(self):
46
            self.resp._get_response = Mock()
47
            foo(self)
48
        return mocker
49

    
50
    def test_release(self):
51
        self.assertRaises(NotImplementedError, self.resp.release)
52

    
53
    def test_prefetched(self):
54
        self.assertFalse(self.resp.prefetched)
55
        self.resp.prefetched = True
56
        self.assertTrue(self.resp.prefetched)
57

    
58
    @_mock_get_response
59
    def test_content(self):
60
        rsp = self.resp
61
        for cont in ('Sample Content', u'\u03c7\u03cd\u03bd\u03c9\x00'):
62
            rsp.content = cont
63
            self.assertEquals(rsp.content, cont)
64

    
65
    (
66
        test_text,
67
        test_json,
68
        test_headers,
69
        test_status,
70
        test_status_code) = 5 * (test_content,)
71

    
72
    def test_request(self):
73
        from httplib import HTTPSConnection
74
        r = self.resp.request
75
        self.assertTrue(isinstance(r, HTTPSConnection))
76

    
77

    
78
def main(argv):
79
    if argv:
80
        suite = TestSuite()
81
        test_method = 'test_%s' % '_'.join(argv)
82
        suite.addTest(HTTPResponse(test_method))
83
    else:
84
        suite = TestLoader().loadTestsFromTestCase(HTTPResponse)
85
    TextTestRunner(verbosity=2).run(suite)
86

    
87

    
88
if __name__ == '__main__':
89
    from sys import argv
90
    main(argv[1:])