1
# Copyright 2011 Canonical Ltd
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.
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.
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
17
"""Tests for the HeadMiddleware app."""
19
from cStringIO import StringIO
21
from bzrlib import tests
23
from loggerhead.apps import http_head
27
"<head><title>Listed</title></head>",
28
"<body>Content</body>",
31
headers = {'X-Ignored-Header': 'Value'}
33
def yielding_app(environ, start_response):
34
writer = start_response('200 OK', headers)
39
def list_app(environ, start_response):
40
writer = start_response('200 OK', headers)
44
def writer_app(environ, start_response):
45
writer = start_response('200 OK', headers)
51
class TestHeadMiddleware(tests.TestCase):
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
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)
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())
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())
75
def test_get_passthrough_yielding(self):
76
self._verify_get_passthrough(yielding_app)
78
def test_head_passthrough_yielding(self):
79
self._verify_head_no_body(yielding_app)
81
def test_get_passthrough_list(self):
82
self._verify_get_passthrough(list_app)
84
def test_head_passthrough_list(self):
85
self._verify_head_no_body(list_app)
87
def test_get_passthrough_writer(self):
88
self._verify_get_passthrough(writer_app)
90
def test_head_passthrough_writer(self):
91
self._verify_head_no_body(writer_app)