File: test_root.py

package info (click to toggle)
python-wsme 0.12.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 616 kB
  • sloc: python: 6,064; makefile: 27; javascript: 8
file content (121 lines) | stat: -rw-r--r-- 3,792 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# encoding=utf8

import unittest

from wsme import WSRoot
import wsme.protocol
import wsme.rest.protocol
from wsme.root import default_prepare_response_body

from webob import Request


class TestRoot(unittest.TestCase):
    def test_default_transaction(self):
        import transaction
        root = WSRoot(transaction=True)
        assert root._transaction is transaction

        txn = root.begin()
        txn.abort()

    def test_default_prepare_response_body(self):
        default_prepare_response_body(None, [b'a']) == b'a'
        default_prepare_response_body(None, [b'a', b'b']) == b'a\nb'
        default_prepare_response_body(None, ['a']) == 'a'
        default_prepare_response_body(None, ['a', 'b']) == 'a\nb'

    def test_protocol_selection_error(self):
        class P(wsme.protocol.Protocol):
            name = "test"

            def accept(self, r):
                raise Exception('test')

        root = WSRoot()
        root.addprotocol(P())

        from webob import Request
        req = Request.blank('/test?check=a&check=b&name=Bob')
        res = root._handle_request(req)
        assert res.status_int == 500
        assert res.content_type == 'text/plain'
        assert (res.text ==
                'Unexpected error while selecting protocol: test'), req.text

    def test_protocol_selection_accept_mismatch(self):
        """Verify that we get a 406 error on wrong Accept header."""
        class P(wsme.protocol.Protocol):
            name = "test"

            def accept(self, r):
                return False

        root = WSRoot()
        root.addprotocol(wsme.rest.protocol.RestProtocol())
        root.addprotocol(P())

        req = Request.blank('/test?check=a&check=b&name=Bob')
        req.method = 'GET'
        res = root._handle_request(req)
        assert res.status_int == 406
        assert res.content_type == 'text/plain'
        assert res.text.startswith(
            'None of the following protocols can handle this request'
        ), req.text

    def test_protocol_selection_content_type_mismatch(self):
        """Verify that we get a 415 error on wrong Content-Type header."""
        class P(wsme.protocol.Protocol):
            name = "test"

            def accept(self, r):
                return False

        root = WSRoot()
        root.addprotocol(wsme.rest.protocol.RestProtocol())
        root.addprotocol(P())

        req = Request.blank('/test?check=a&check=b&name=Bob')
        req.method = 'POST'
        req.headers['Content-Type'] = "test/unsupported"
        res = root._handle_request(req)
        assert res.status_int == 415
        assert res.content_type == 'text/plain'
        assert res.text.startswith(
            'Unacceptable Content-Type: test/unsupported not in'
        ), req.text

    def test_protocol_selection_get_method(self):
        class P(wsme.protocol.Protocol):
            name = "test"

            def accept(self, r):
                return True

        root = WSRoot()
        root.addprotocol(wsme.rest.protocol.RestProtocol())
        root.addprotocol(P())

        req = Request.blank('/test?check=a&check=b&name=Bob')
        req.method = 'GET'
        req.headers['Accept'] = 'test/fake'
        p = root._select_protocol(req)
        assert p.name == "test"

    def test_protocol_selection_post_method(self):
        class P(wsme.protocol.Protocol):
            name = "test"

            def accept(self, r):
                return True

        root = WSRoot()
        root.addprotocol(wsme.rest.protocol.RestProtocol())
        root.addprotocol(P())

        req = Request.blank('/test?check=a&check=b&name=Bob')
        req.headers['Content-Type'] = 'test/fake'
        req.method = 'POST'
        p = root._select_protocol(req)
        assert p.name == "test"