~ubuntu-branches/ubuntu/lucid/python-webob/lucid

« back to all changes in this revision

Viewing changes to tests/test_response.py

  • Committer: Bazaar Package Importer
  • Author(s): Piotr Ożarowski
  • Date: 2010-01-13 22:07:40 UTC
  • mfrom: (7.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100113220740-51qlp569n69lqn88
Tags: 0.9.7.1-1
* New upstream release
* 01-autoclass-autodata patch removed, no longer needed
* Add add_docs_conf.patch (missing in upstream tarball)
* Bump Standards-Version to 3.8.3 (no changes needed)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from StringIO import StringIO
2
 
 
 
2
from nose.tools import eq_, ok_, assert_raises
3
3
from webob import *
 
4
from webob import BaseRequest
4
5
 
5
6
def simple_app(environ, start_response):
6
7
    start_response('200 OK', [
9
10
    return ['OK']
10
11
 
11
12
def test_response():
12
 
    req = Request.blank('/')
 
13
    req = BaseRequest.blank('/')
13
14
    res = req.get_response(simple_app)
14
15
    assert res.status == '200 OK'
15
16
    assert res.status_int == 200
38
39
    assert res.content_encoding is None
39
40
    assert res.body == 'a body'
40
41
 
 
42
 
41
43
def test_HEAD_closes():
42
44
    req = Request.blank('/')
43
45
    req.method = 'HEAD'
44
46
    app_iter = StringIO('foo')
45
47
    res = req.get_response(Response(app_iter=app_iter))
46
 
    assert res.status_int == 200
47
 
    assert res.body == ''
48
 
    assert app_iter.closed
 
48
    eq_(res.status_int, 200)
 
49
    eq_(res.body, '')
 
50
    ok_(app_iter.closed)
 
51
 
 
52
def test_content_length():
 
53
    r0 = Response('x'*10, content_length=10)
 
54
 
 
55
    req_head = Request.blank('/', method='HEAD')
 
56
    r1 = req_head.get_response(r0)
 
57
    eq_(r1.status_int, 200)
 
58
    eq_(r1.body, '')
 
59
    eq_(r1.content_length, 10)
 
60
 
 
61
    req_get = Request.blank('/')
 
62
    r2 = req_get.get_response(r0)
 
63
    eq_(r2.status_int, 200)
 
64
    eq_(r2.body, 'x'*10)
 
65
    eq_(r2.content_length, 10)
 
66
 
 
67
    r3 = Response(app_iter=['x']*10)
 
68
    eq_(r3.content_length, None)
 
69
    eq_(r3.body, 'x'*10)
 
70
    eq_(r3.content_length, 10)
 
71
 
 
72
    r4 = Response(app_iter=['x']*10, content_length=20) # wrong content_length
 
73
    eq_(r4.content_length, 20)
 
74
    assert_raises(AssertionError, lambda: r4.body)
 
75
 
 
76
    req_range = Request.blank('/', range=(0,5))
 
77
    r0.conditional_response = True
 
78
    r5 = req_range.get_response(r0)
 
79
    eq_(r5.status_int, 206)
 
80
    eq_(r5.body, 'xxxxx')
 
81
    eq_(r5.content_length, 5)
 
82
 
 
83
 
 
84
def test_app_iter_range():
 
85
    req = Request.blank('/', range=(2,5))
 
86
    for app_iter in [
 
87
        ['012345'],
 
88
        ['0', '12345'],
 
89
        ['0', '1234', '5'],
 
90
        ['01', '2345'],
 
91
        ['01', '234', '5'],
 
92
        ['012', '34', '5'],
 
93
        ['012', '3', '4', '5'],
 
94
        ['012', '3', '45'],
 
95
        ['0', '12', '34', '5'],
 
96
        ['0', '12', '345'],
 
97
    ]:
 
98
        r = Response(
 
99
            app_iter=app_iter,
 
100
            content_length=6,
 
101
            conditional_response=True,
 
102
        )
 
103
        res = req.get_response(r)
 
104
        eq_(list(res.content_range), [2,5,6])
 
105
        eq_(res.body, '234', 'body=%r; app_iter=%r' % (res.body, app_iter))