1 import os, sys
2 import glob
3
4 from Globals import package_home
5
7 home = package_home(globals())
8 return [folder + '/' + os.path.basename(filename) for filename in
9 glob.glob(os.path.sep.join([home, folder + '/*.txt']))]
10
12 """
13 """
14 folder.manage_delObjects(folder.objectIds())
15
18
19 -def http(request_string, handle_errors=True):
20 """
21 MONKEYPATCH: Fix https://bugs.launchpad.net/zope3/+bug/98437
22 TODO: try to really fix this... ask Martin Aspeli,
23 talk in the zope channel, etc...
24
25 Execute an HTTP request string via the publisher
26
27 This is used for HTTP doc tests.
28 """
29 import urllib
30 import rfc822
31 from cStringIO import StringIO
32 from ZPublisher.Response import Response
33 from ZPublisher.Test import publish_module
34 from AccessControl.SecurityManagement import getSecurityManager
35 from AccessControl.SecurityManagement import setSecurityManager
36 import transaction
37 from Testing.ZopeTestCase.zopedoctest.functional import HTTPHeaderOutput, \
38 split_header, \
39 sync, \
40 DocResponseWrapper
41
42
43 old_sm = getSecurityManager()
44
45
46 transaction.commit()
47
48
49 request_string = request_string.lstrip()
50
51
52 l = request_string.find('\n')
53 command_line = request_string[:l].rstrip()
54 request_string = request_string[l+1:]
55 method, path, protocol = command_line.split()
56 path = urllib.unquote(path)
57
58 instream = StringIO(request_string)
59
60 env = {"HTTP_HOST": 'nohost',
61 "HTTP_REFERER": 'http://nohost/plone',
62 "REQUEST_METHOD": method,
63 "SERVER_PROTOCOL": protocol,
64 }
65
66 p = path.split('?')
67 if len(p) == 1:
68 env['PATH_INFO'] = p[0]
69 elif len(p) == 2:
70 [env['PATH_INFO'], env['QUERY_STRING']] = p
71 else:
72 raise TypeError, ''
73
74 header_output = HTTPHeaderOutput(
75 protocol, ('x-content-type-warning', 'x-powered-by',
76 'bobo-exception-type', 'bobo-exception-file',
77 'bobo-exception-value', 'bobo-exception-line'))
78
79 headers = [split_header(header)
80 for header in rfc822.Message(instream).headers]
81
82
83 instream = StringIO(instream.read())
84
85 for name, value in headers:
86 name = ('_'.join(name.upper().split('-')))
87 if name not in ('CONTENT_TYPE', 'CONTENT_LENGTH'):
88 name = 'HTTP_' + name
89 env[name] = value.rstrip()
90
91 if env.has_key('HTTP_AUTHORIZATION'):
92 env['HTTP_AUTHORIZATION'] = auth_header(env['HTTP_AUTHORIZATION'])
93
94 outstream = StringIO()
95 response = Response(stdout=outstream, stderr=sys.stderr)
96
97 publish_module('Zope2',
98 response=response,
99 stdin=instream,
100 environ=env,
101 debug=not handle_errors,
102 )
103 header_output.setResponseStatus(response.getStatus(), response.errmsg)
104 header_output.setResponseHeaders(response.headers)
105 header_output.appendResponseHeaders(response._cookie_list())
106 header_output.appendResponseHeaders(response.accumulated_headers.splitlines())
107
108
109
110 setSecurityManager(old_sm)
111
112
113 sync()
114
115 return DocResponseWrapper(response, outstream, path, header_output)
116