Some docs for the TestingMiddleware
[mediagoblin.git] / mediagoblin / middleware / testing.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 MediaGoblin contributors. See AUTHORS.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 class TestingMiddleware(object):
18 """
19 Middleware for the Unit tests
20
21 It might make sense to perform some tests on all
22 requests/responses. Or prepare them in a special
23 manner. For example all html responses could be tested
24 for being valid html *after* being rendered.
25
26 This module is getting inserted at the front of the
27 middleware list, which means: requests are handed here
28 first, responses last. So this wraps up the "normal"
29 app.
30
31 If you need to add a test, either add it directly to
32 the appropiate process_request or process_response, or
33 create a new method and call it from process_*.
34 """
35
36 def __init__(self, mg_app):
37 self.app = mg_app
38
39 def process_request(self, request):
40 pass
41
42 def process_response(self, request, response):
43 # All following tests should be for html only!
44 if response.content_type != "text/html":
45 # Get out early
46 return
47
48 # If the template contains a reference to
49 # /mgoblin_static/ instead of using
50 # /request.staticdirect(), error out here.
51 # This could probably be implemented as a grep on
52 # the shipped templates easier...
53 if response.text.find("/mgoblin_static/") >= 0:
54 raise AssertionError(
55 "Response HTML contains reference to /mgoblin_static/ "
56 "instead of staticdirect. Request was for: "
57 + request.full_path)
58
59 return