~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/tests/test_simple.py

  • Committer: John Arbash Meinel
  • Date: 2011-03-19 08:35:57 UTC
  • mfrom: (422.3.2 head_middleware)
  • Revision ID: john@arbash-meinel.com-20110319083557-k8mbbkr3bzisz3ob
include HeadMiddleware so that we can be sure HEAD requests never return BODY content.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008, 2009 Canonical Ltd.
 
1
# Copyright (C) 2007, 2008, 2009, 2011 Canonical Ltd.
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
17
17
 
18
18
import cgi
19
19
import logging
 
20
import re
20
21
 
21
22
from bzrlib.tests import TestCaseWithTransport
22
 
from bzrlib.util.configobj.configobj import ConfigObj
 
23
try:
 
24
    from bzrlib.util.configobj.configobj import ConfigObj
 
25
except ImportError:
 
26
    from configobj import ConfigObj
23
27
from bzrlib import config
24
28
 
25
29
from loggerhead.apps.branch import BranchWSGIApp
 
30
from loggerhead.apps.http_head import HeadMiddleware
26
31
from paste.fixture import TestApp
27
 
from paste.httpexceptions import HTTPExceptionHandler
28
 
 
29
 
 
30
 
 
31
 
def test_config_root():
32
 
    from loggerhead.apps.config import Root
33
 
    config = ConfigObj()
34
 
    app = TestApp(HTTPExceptionHandler(Root(config)))
35
 
    res = app.get('/')
36
 
    res.mustcontain('loggerhead branches')
 
32
from paste.httpexceptions import HTTPExceptionHandler, HTTPMovedPermanently
 
33
 
37
34
 
38
35
 
39
36
class BasicTests(TestCaseWithTransport):
61
58
                             'with<htmlspecialchars\n')
62
59
        self.build_tree_contents(
63
60
            [('myfilename', self.filecontents)])
64
 
        self.tree.add('myfilename')
 
61
        self.tree.add('myfilename', 'myfile-id')
65
62
        self.fileid = self.tree.path2id('myfilename')
66
63
        self.msg = 'a very exciting commit message <'
67
64
        self.revid = self.tree.commit(message=self.msg)
71
68
        res = app.get('/changes')
72
69
        res.mustcontain(cgi.escape(self.msg))
73
70
 
 
71
    def test_changes_for_file(self):
 
72
        app = self.setUpLoggerhead()
 
73
        res = app.get('/changes?filter_file_id=myfile-id')
 
74
        res.mustcontain(cgi.escape(self.msg))
 
75
 
74
76
    def test_changes_branch_from(self):
75
77
        app = self.setUpLoggerhead(served_url="lp:loggerhead")
76
78
        res = app.get('/changes')
88
90
    def test_annotate(self):
89
91
        app = self.setUpLoggerhead()
90
92
        res = app.get('/annotate', params={'file_id': self.fileid})
 
93
        # If pygments is installed, it inserts <span class="pyg" content into
 
94
        # the output, to trigger highlighting. And it specifically highlights
 
95
        # the &lt; that we are interested in seeing in the output.
 
96
        # Without pygments we have a simple: 'with&lt;htmlspecialchars'
 
97
        # With it, we have
 
98
        # '<span class='pyg-n'>with</span><span class='pyg-o'>&lt;</span>'
 
99
        # '<span class='pyg-n'>htmlspecialchars</span>
 
100
        # So we pre-filter the body, to make sure remove spans of that type.
 
101
        body_no_span = re.sub(r'<span class="pyg-.">', '', res.body)
 
102
        body_no_span = body_no_span.replace('</span>', '')
91
103
        for line in self.filecontents.splitlines():
92
 
            res.mustcontain(cgi.escape(line))
 
104
            escaped = cgi.escape(line)
 
105
            self.assertTrue(escaped in body_no_span,
 
106
                            "did not find %r in %r" % (escaped, body_no_span))
93
107
 
94
108
    def test_inventory(self):
95
109
        app = self.setUpLoggerhead()
157
171
        res = app.get('/changes', status=404)
158
172
 
159
173
 
 
174
class TestControllerRedirects(BasicTests):
 
175
    """
 
176
    Test that a file under /files redirects to /view,
 
177
    and a directory under /view redirects to /files.
 
178
    """
 
179
 
 
180
    def setUp(self):
 
181
        BasicTests.setUp(self)
 
182
        self.createBranch()
 
183
        self.build_tree(('file', 'folder/', 'folder/file'))
 
184
        self.tree.smart_add([])
 
185
        self.tree.commit('')
 
186
 
 
187
    def test_view_folder(self):
 
188
        app = TestApp(BranchWSGIApp(self.tree.branch, '').app)
 
189
 
 
190
        e = self.assertRaises(HTTPMovedPermanently, app.get, '/view/head:/folder')
 
191
        self.assertEqual(e.location(), '/files/head:/folder')
 
192
 
 
193
    def test_files_file(self):
 
194
        app = TestApp(BranchWSGIApp(self.tree.branch, '').app)
 
195
 
 
196
        e = self.assertRaises(HTTPMovedPermanently, app.get, '/files/head:/folder/file')
 
197
        self.assertEqual(e.location(), '/view/head:/folder/file')
 
198
        e = self.assertRaises(HTTPMovedPermanently, app.get, '/files/head:/file')
 
199
        self.assertEqual(e.location(), '/view/head:/file')
 
200
 
 
201
 
 
202
class TestHeadMiddleware(BasicTests):
 
203
 
 
204
    def setUp(self):
 
205
        BasicTests.setUp(self)
 
206
        self.createBranch()
 
207
        self.msg = 'trivial commit message'
 
208
        self.revid = self.tree.commit(message=self.msg)
 
209
 
 
210
    def setUpLoggerhead(self, **kw):
 
211
        branch_app = BranchWSGIApp(self.tree.branch, '', **kw).app
 
212
        return TestApp(HTTPExceptionHandler(HeadMiddleware(branch_app)))
 
213
 
 
214
    def test_get(self):
 
215
        app = self.setUpLoggerhead()
 
216
        res = app.get('/changes')
 
217
        res.mustcontain(self.msg)
 
218
        self.assertEqual('text/html', res.header('Content-Type'))
 
219
 
 
220
    def test_head(self):
 
221
        app = self.setUpLoggerhead()
 
222
        res = app.get('/changes', extra_environ={'REQUEST_METHOD': 'HEAD'})
 
223
        self.assertEqual('text/html', res.header('Content-Type'))
 
224
        self.assertEqualDiff('', res.body)
 
225
 
 
226
 
160
227
#class TestGlobalConfig(BasicTests):
161
228
#    """
162
229
#    Test that global config settings are respected