本文整理汇总了Python中werkzeug.create_environ函数的典型用法代码示例。如果您正苦于以下问题:Python create_environ函数的具体用法?Python create_environ怎么用?Python create_environ使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了create_environ函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_get_host
def test_get_host():
"""Host lookup"""
env = {'HTTP_X_FORWARDED_HOST': 'example.org',
'SERVER_NAME': 'bullshit', 'HOST_NAME': 'ignore me dammit'}
assert get_host(env) == 'example.org'
assert get_host(create_environ('/', 'http://example.org')) \
== 'example.org'
示例2: initdb
def initdb():
# A request_context is required to use these helper functions
with new_app(app).request_context(create_environ()):
db.drop_all()
db.create_all()
readaction = schema.Action("read")
insertaction = schema.Action("insert")
deleteaction = schema.Action("delete")
editaction = schema.Action("edit")
db.session.add(readaction)
db.session.add(insertaction)
db.session.add(deleteaction)
db.session.add(editaction)
administrator = schema.User(username="admin",
password="admin",
firstname="admin",
lastname="admin",
email="[email protected]")
administrator.actions.append(readaction)
administrator.actions.append(insertaction)
administrator.actions.append(deleteaction)
administrator.actions.append(editaction)
db.session.add(administrator)
db.session.commit()
示例3: __init__
def __init__(self, path="/", query_string=None, method='GET',
content_type=None, content_length=0, form_data=None,
environ_overrides=None):
"""
For parameter reference see the documentation of the werkzeug
package, especially the functions `url_encode` and `create_environ`.
"""
input_stream = None
if form_data is not None:
form_data = url_encode(form_data)
content_type = 'application/x-www-form-urlencoded'
content_length = len(form_data)
input_stream = StringIO(form_data)
environ = create_environ(path=path, query_string=query_string,
method=method, input_stream=input_stream,
content_type=content_type,
content_length=content_length)
environ['HTTP_USER_AGENT'] = 'MoinMoin/TestRequest'
# must have reverse lookup or tests will be extremely slow:
environ['REMOTE_ADDR'] = '127.0.0.1'
if environ_overrides:
environ.update(environ_overrides)
super(TestRequest, self).__init__(environ)
示例4: test_request_context
def test_request_context(self, *args, **kwargs):
"""Creates a WSGI environment from the given values (see
:func:`werkzeug.create_environ` for more information, this
function accepts the same arguments).
"""
from werkzeug import create_environ
return self.request_context(create_environ(*args, **kwargs))
示例5: test_path_info_extraction
def test_path_info_extraction():
"""PATH INFO extraction feature"""
x = extract_path_info('http://example.com/app', '/app/hello')
assert x == u'/hello'
x = extract_path_info('http://example.com/app',
'https://example.com/app/hello')
assert x == u'/hello'
x = extract_path_info('http://example.com/app/',
'https://example.com/app/hello')
assert x == u'/hello'
x = extract_path_info('http://example.com/app/',
'https://example.com/app')
assert x == u'/'
x = extract_path_info(u'http://☃.net/', u'/fööbär')
assert x == u'/fööbär'
x = extract_path_info(u'http://☃.net/x', u'http://☃.net/x/fööbär')
assert x == u'/fööbär'
env = create_environ(u'/fööbär', u'http://☃.net/x/')
x = extract_path_info(env, u'http://☃.net/x/fööbär')
assert x == u'/fööbär'
x = extract_path_info('http://example.com/app/',
'https://example.com/a/hello')
assert x is None
x = extract_path_info('http://example.com/app/',
'https://example.com/app/hello',
collapse_http_schemes=False)
assert x is None
示例6: test_path_info_from_request_uri_fix
def test_path_info_from_request_uri_fix():
"""Test the PathInfoFromRequestUriFix fixer"""
app = fixers.PathInfoFromRequestUriFix(path_check_app)
for key in 'REQUEST_URI', 'REQUEST_URL', 'UNENCODED_URL':
env = dict(create_environ(), SCRIPT_NAME='/test', PATH_INFO='/?????')
env[key] = '/test/foo%25bar?drop=this'
response = Response.from_app(app, env)
assert response.data == 'PATH_INFO: /foo%bar\nSCRIPT_NAME: /test'
示例7: test_lighttpd_cgi_root_fix
def test_lighttpd_cgi_root_fix():
"""Test the LighttpdCGIRootFix fixer"""
app = fixers.LighttpdCGIRootFix(path_check_app)
response = Response.from_app(app, dict(create_environ(),
SCRIPT_NAME='/foo',
PATH_INFO='/bar'
))
assert response.data == 'PATH_INFO: /foo/bar\nSCRIPT_NAME: '
示例8: test_broken_multipart
def test_broken_multipart():
"""Broken multipart does not break the applicaiton"""
data = (
'--foo\r\n'
'Content-Disposition: form-data; name="test"; filename="test.txt"\r\n'
'Content-Transfer-Encoding: base64\r\n'
'Content-Type: text/plain\r\n\r\n'
'broken base 64'
'--foo--'
)
_, form, files = parse_form_data(create_environ(data=data, method='POST',
content_type='multipart/form-data; boundary=foo'))
assert not files
assert not form
assert_raises(ValueError, parse_form_data, create_environ(data=data, method='POST',
content_type='multipart/form-data; boundary=foo'),
silent=False)
示例9: __init__
def __init__(self, url=None, pagename=''):
if url is None:
url = 'http://localhost:0/' # just some somehow valid dummy URL
environ = create_environ(base_url=url) # XXX not sure about base_url, but makes "make underlay" work
environ['HTTP_USER_AGENT'] = 'CLI/Script'
environ['wsgi.input'] = sys.stdin
request = Request(environ)
super(ScriptContext, self).__init__(request)
from MoinMoin import wsgiapp
wsgiapp.init(self)
示例10: test_parse_form_data_get_without_content
def test_parse_form_data_get_without_content():
"""GET requests without data, content type and length returns no data"""
env = create_environ('/foo', 'http://example.org/', method='GET')
del env['CONTENT_TYPE']
del env['CONTENT_LENGTH']
stream, form, files = parse_form_data(env)
assert stream.read() == ""
assert len(form) == 0
assert len(files) == 0
示例11: test_wrapper_internals
def test_wrapper_internals():
"""Test internals of the wrappers"""
from werkzeug import Request
req = Request.from_values(data={'foo': 'bar'}, method='POST')
req._load_form_data()
assert req.form.to_dict() == {'foo': 'bar'}
# second call does not break
req._load_form_data()
assert req.form.to_dict() == {'foo': 'bar'}
# check reprs
assert repr(req) == "<Request 'http://localhost/' [POST]>"
resp = Response()
assert repr(resp) == '<Response 0 bytes [200 OK]>'
resp.data = 'Hello World!'
assert repr(resp) == '<Response 12 bytes [200 OK]>'
resp.response = iter(['Test'])
assert repr(resp) == '<Response streamed [200 OK]>'
# unicode data does not set content length
response = Response([u'Hällo Wörld'])
headers = response.get_wsgi_headers(create_environ())
assert 'Content-Length' not in headers
response = Response(['Hällo Wörld'])
headers = response.get_wsgi_headers(create_environ())
assert 'Content-Length' in headers
# check for internal warnings
print 'start'
filterwarnings('error', category=Warning)
response = Response()
environ = create_environ()
response.response = 'What the...?'
assert_raises(Warning, lambda: list(response.iter_encoded()))
assert_raises(Warning, lambda: list(response.get_app_iter(environ)))
response.direct_passthrough = True
assert_raises(Warning, lambda: list(response.iter_encoded()))
assert_raises(Warning, lambda: list(response.get_app_iter(environ)))
resetwarnings()
示例12: test_header_rewriter_fix
def test_header_rewriter_fix():
"""Test the HeaderRewriterFix fixer"""
@Request.application
def application(request):
return Response("", headers=[
('X-Foo', 'bar')
])
application = fixers.HeaderRewriterFix(application, ('X-Foo',), (('X-Bar', '42'),))
response = Response.from_app(application, create_environ())
assert response.headers['Content-Type'] == 'text/plain; charset=utf-8'
assert 'X-Foo' not in response.headers
assert response.headers['X-Bar'] == '42'
示例13: _create_environ
def _create_environ(self, url, method, data, refer, content_type=None):
"""Return an environ to request *url*, including cookies."""
environ_args = dict(self._wsgi_server, method=method)
base_url = self._referrer if refer else self._base_url
environ_args.update(self._canonicalize_url(url, base_url))
environ_args.update(self._prep_input(method, data, content_type))
environ = create_environ(**environ_args)
if refer and self._referrer:
environ['HTTP_REFERER'] = self._referrer
environ.setdefault('REMOTE_ADDR', '127.0.0.1')
self._cookie_jar.export_to_environ(environ)
return environ
示例14: request_context
def request_context(self, *args, **kw):
"""Create a request context for use in tests. The arguments passed will
be used to create a WSGI environment to create a request instance (see
:func:`werkzeug.create_environ` for more information). This method must
be used with the ``with`` statement.
For example::
with self.request_context():
do_something_with(request)
"""
from werkzeug import create_environ
return self.test_app.request_context(create_environ(*args, **kw))
示例15: fetch_body
def fetch_body(app, path):
environ = create_environ(path=path)
def start_response(status, headers):
start_response.code = int(status.split()[0])
start_response.code = None
content = ''.join(list(app(environ, start_response)))
if start_response.code == 200:
return content
elif start_response.code // 100 == 3:
abort(404)
else:
abort(start_response.code)