~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/tests/test_http_head.py

  • Committer: Andrew Bennetts
  • Date: 2011-06-28 13:13:05 UTC
  • Revision ID: andrew.bennetts@canonical.com-20110628131305-skv4h2z02z3iw8wc
Extract some refactoring of TemplatedBranchView and BranchWSGIApp from the jsonify branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2011 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Tests for the HeadMiddleware app."""
 
18
 
 
19
from cStringIO import StringIO
 
20
 
 
21
from bzrlib import tests
 
22
 
 
23
from loggerhead.apps import http_head
 
24
 
 
25
 
 
26
content = ["<html>",
 
27
           "<head><title>Listed</title></head>",
 
28
           "<body>Content</body>",
 
29
           "</html>",
 
30
          ]
 
31
headers = {'X-Ignored-Header': 'Value'}
 
32
 
 
33
def yielding_app(environ, start_response):
 
34
    writer = start_response('200 OK', headers)
 
35
    for chunk in content:
 
36
        yield chunk
 
37
 
 
38
 
 
39
def list_app(environ, start_response):
 
40
    writer = start_response('200 OK', headers)
 
41
    return content
 
42
 
 
43
 
 
44
def writer_app(environ, start_response):
 
45
    writer = start_response('200 OK', headers)
 
46
    for chunk in content:
 
47
        writer(chunk)
 
48
    return []
 
49
 
 
50
 
 
51
class TestHeadMiddleware(tests.TestCase):
 
52
 
 
53
    def _trap_start_response(self, status, response_headers, exc_info=None):
 
54
        self._write_buffer = StringIO()
 
55
        self._start_response_passed = (status, response_headers, exc_info)
 
56
        return self._write_buffer.write
 
57
 
 
58
    def _consume_app(self, app, request_method):
 
59
        environ = {'REQUEST_METHOD': request_method}
 
60
        value = list(app(environ, self._trap_start_response))
 
61
        self._write_buffer.writelines(value)
 
62
 
 
63
    def _verify_get_passthrough(self, app):
 
64
        app = http_head.HeadMiddleware(app)
 
65
        self._consume_app(app, 'GET')
 
66
        self.assertEqual(('200 OK', headers, None), self._start_response_passed)
 
67
        self.assertEqualDiff(''.join(content), self._write_buffer.getvalue())
 
68
 
 
69
    def _verify_head_no_body(self, app):
 
70
        app = http_head.HeadMiddleware(app)
 
71
        self._consume_app(app, 'HEAD')
 
72
        self.assertEqual(('200 OK', headers, None), self._start_response_passed)
 
73
        self.assertEqualDiff('', self._write_buffer.getvalue())
 
74
 
 
75
    def test_get_passthrough_yielding(self):
 
76
        self._verify_get_passthrough(yielding_app)
 
77
 
 
78
    def test_head_passthrough_yielding(self):
 
79
        self._verify_head_no_body(yielding_app)
 
80
 
 
81
    def test_get_passthrough_list(self):
 
82
        self._verify_get_passthrough(list_app)
 
83
 
 
84
    def test_head_passthrough_list(self):
 
85
        self._verify_head_no_body(list_app)
 
86
 
 
87
    def test_get_passthrough_writer(self):
 
88
        self._verify_get_passthrough(writer_app)
 
89
 
 
90
    def test_head_passthrough_writer(self):
 
91
        self._verify_head_no_body(writer_app)
 
92