File: test_wsgi_application.py

package info (click to toggle)
circuits 3.2.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,980 kB
  • sloc: python: 17,583; javascript: 3,226; makefile: 100
file content (98 lines) | stat: -rw-r--r-- 2,768 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
#!/usr/bin/env python
import json

import pytest

from circuits.web import Controller
from circuits.web.wsgi import Application

from .helpers import HTTPError, urlencode, urlopen


class Root(Controller):
    def index(self):
        return 'Hello World!'

    def test_args(self, *args, **kwargs):
        self.response.headers['Content-Type'] = 'application/json'
        return json.dumps(
            {
                'args': args,
                'kwargs': kwargs,
                'path': self.request.path,
                'uri_path': self.request.uri._path.decode(),
                'base_path': self.request.base._path.decode(),
                'method': self.request.method,
                'scheme': self.request.scheme,
                'protocol': self.request.protocol,
                'qs': self.request.qs,
                'script_name': self.request.script_name,
                'content_type': self.request.headers['Content-Type'],
            }
        )

    def test_redirect(self):
        return self.redirect('/')

    def test_forbidden(self):
        return self.forbidden()

    def test_notfound(self):
        return self.notfound()


application = Application() + Root()


def test(webapp):
    f = urlopen(webapp.server.http.base)
    s = f.read()
    assert s == b'Hello World!'


def test_404(webapp):
    with pytest.raises(HTTPError) as exc:
        urlopen('%s/foo' % webapp.server.http.base)
    assert exc.value.code == 404
    assert exc.value.msg == 'Not Found'


def test_args(webapp):
    args = ['1', '2', '3']
    kwargs = {'1': 'one', '2': 'two', '3': 'three'}
    url = '%s/test_args/%s' % (webapp.server.http.base, '/'.join(args))
    data = urlencode(kwargs).encode()

    f = urlopen(url, data)
    data = json.load(f)
    assert data['args'] == args
    assert data['kwargs'] == kwargs
    assert data['path'] == 'test_args/1/2/3'
    assert data['uri_path'] == '/test_args/1/2/3'
    assert data['base_path'] == '/'
    assert data['method'] == 'POST'
    assert data['scheme'] == 'http'
    assert data['protocol'] == [1, 1]
    assert data['qs'] == ''
    assert data['script_name'] == '/'
    assert data['content_type'] == 'application/x-www-form-urlencoded'


def test_redirect(webapp):
    f = urlopen('%s/test_redirect' % webapp.server.http.base)
    s = f.read()
    assert s == b'Hello World!'


def test_forbidden(webapp):
    with pytest.raises(HTTPError) as exc:
        urlopen('%s/test_forbidden' % webapp.server.http.base)
    assert exc.value.code == 403
    assert exc.value.msg == 'Forbidden'


def test_notfound(webapp):
    with pytest.raises(HTTPError) as exc:
        urlopen('%s/test_notfound' % webapp.server.http.base)
    assert exc.value.code == 404
    assert exc.value.msg == 'Not Found'