File: 010-test-proxies.py

package info (click to toggle)
python-restkit 4.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,908 kB
  • ctags: 841
  • sloc: python: 5,964; makefile: 84; xml: 8; sh: 5
file content (112 lines) | stat: -rw-r--r-- 3,207 bytes parent folder | download | duplicates (3)
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
# -*- coding: utf-8 -*-
#
# This file is part of restkit released under the MIT license. 
# See the NOTICE for more information.

import t
from _server_test import HOST, PORT
from restkit.contrib import wsgi_proxy

root_uri = "http://%s:%s" % (HOST, PORT)

def with_webob(func):
    def wrapper(*args, **kwargs):
        from webob import Request
        req = Request.blank('/')
        req.environ['SERVER_NAME'] = '%s:%s' % (HOST, PORT)
        return func(req)
    wrapper.func_name = func.func_name
    return wrapper

@with_webob
def test_001(req):
    req.path_info = '/query'
    proxy = wsgi_proxy.Proxy()
    resp = req.get_response(proxy)
    body = resp.body
    assert 'path: /query' in body, str(resp)

@with_webob
def test_002(req):
    req.path_info = '/json'
    req.environ['CONTENT_TYPE'] = 'application/json'
    req.method = 'POST'
    req.body = 'test post'
    proxy = wsgi_proxy.Proxy(allowed_methods=['POST'])
    resp = req.get_response(proxy)
    body = resp.body
    assert resp.content_length == 9, str(resp)

    proxy = wsgi_proxy.Proxy(allowed_methods=['GET'])
    resp = req.get_response(proxy)
    assert resp.status.startswith('403'), resp.status

@with_webob
def test_003(req):
    req.path_info = '/json'
    req.environ['CONTENT_TYPE'] = 'application/json'
    req.method = 'PUT'
    req.body = 'test post'
    proxy = wsgi_proxy.Proxy(allowed_methods=['PUT'])
    resp = req.get_response(proxy)
    body = resp.body
    assert resp.content_length == 9, str(resp)

    proxy = wsgi_proxy.Proxy(allowed_methods=['GET'])
    resp = req.get_response(proxy)
    assert resp.status.startswith('403'), resp.status

@with_webob
def test_004(req):
    req.path_info = '/ok'
    req.method = 'HEAD'
    proxy = wsgi_proxy.Proxy(allowed_methods=['HEAD'])
    resp = req.get_response(proxy)
    body = resp.body
    assert resp.content_type == 'text/plain', str(resp)

@with_webob
def test_005(req):
    req.path_info = '/delete'
    req.method = 'DELETE'
    proxy = wsgi_proxy.Proxy(allowed_methods=['DELETE'])
    resp = req.get_response(proxy)
    body = resp.body
    assert resp.content_type == 'text/plain', str(resp)

    proxy = wsgi_proxy.Proxy(allowed_methods=['GET'])
    resp = req.get_response(proxy)
    assert resp.status.startswith('403'), resp.status

@with_webob
def test_006(req):
    req.path_info = '/redirect'
    req.method = 'GET'
    proxy = wsgi_proxy.Proxy(allowed_methods=['GET'])
    resp = req.get_response(proxy)
    body = resp.body
    assert resp.location == '%s/complete_redirect' % root_uri, str(resp)

@with_webob
def test_007(req):
    req.path_info = '/redirect_to_url'
    req.method = 'GET'
    proxy = wsgi_proxy.Proxy(allowed_methods=['GET'])
    resp = req.get_response(proxy)
    body = resp.body

    print resp.location
    assert resp.location == '%s/complete_redirect' % root_uri, str(resp)

@with_webob
def test_008(req):
    req.path_info = '/redirect_to_url'
    req.script_name = '/name'
    req.method = 'GET'
    proxy = wsgi_proxy.Proxy(allowed_methods=['GET'], strip_script_name=True)
    resp = req.get_response(proxy)
    body = resp.body
    assert resp.location == '%s/name/complete_redirect' % root_uri, str(resp)