[2.7] bpo-37411: Rewrite test_wsgiref.testEnviron() (GH-14394) (GH-14404)
Fix test_wsgiref.testEnviron() to no longer depend on the environment
variables (don't fail if "X" variable is set).
testEnviron() now overrides os.environ to get a deterministic
environment. Test full TestHandler.environ content: not only a few
selected variables.
(cherry picked from commit 5150d32792
)
Co-authored-by: Victor Stinner <vstinner@redhat.com>
This commit is contained in:
parent
49032fa7e1
commit
dfa9499ccb
|
@ -13,7 +13,7 @@ import os
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from test import test_support
|
from test import support
|
||||||
|
|
||||||
class MockServer(WSGIServer):
|
class MockServer(WSGIServer):
|
||||||
"""Non-socket HTTP server"""
|
"""Non-socket HTTP server"""
|
||||||
|
@ -377,32 +377,62 @@ class TestHandler(ErrorHandler):
|
||||||
|
|
||||||
|
|
||||||
class HandlerTests(TestCase):
|
class HandlerTests(TestCase):
|
||||||
|
# testEnviron() can produce long error message
|
||||||
def checkEnvironAttrs(self, handler):
|
maxDiff = 80 * 50
|
||||||
env = handler.environ
|
|
||||||
for attr in [
|
|
||||||
'version','multithread','multiprocess','run_once','file_wrapper'
|
|
||||||
]:
|
|
||||||
if attr=='file_wrapper' and handler.wsgi_file_wrapper is None:
|
|
||||||
continue
|
|
||||||
self.assertEqual(getattr(handler,'wsgi_'+attr),env['wsgi.'+attr])
|
|
||||||
|
|
||||||
def checkOSEnviron(self,handler):
|
|
||||||
empty = {}; setup_testing_defaults(empty)
|
|
||||||
env = handler.environ
|
|
||||||
from os import environ
|
|
||||||
for k,v in environ.items():
|
|
||||||
if k not in empty:
|
|
||||||
self.assertEqual(env[k],v)
|
|
||||||
for k,v in empty.items():
|
|
||||||
self.assertIn(k, env)
|
|
||||||
|
|
||||||
def testEnviron(self):
|
def testEnviron(self):
|
||||||
h = TestHandler(X="Y")
|
os_environ = {
|
||||||
h.setup_environ()
|
# very basic environment
|
||||||
self.checkEnvironAttrs(h)
|
'HOME': '/my/home',
|
||||||
self.checkOSEnviron(h)
|
'PATH': '/my/path',
|
||||||
self.assertEqual(h.environ["X"],"Y")
|
'LANG': 'fr_FR.UTF-8',
|
||||||
|
|
||||||
|
# set some WSGI variables
|
||||||
|
'SCRIPT_NAME': 'test_script_name',
|
||||||
|
'SERVER_NAME': 'test_server_name',
|
||||||
|
}
|
||||||
|
|
||||||
|
with support.swap_attr(TestHandler, 'os_environ', os_environ):
|
||||||
|
# override X and HOME variables
|
||||||
|
handler = TestHandler(X="Y", HOME="/override/home")
|
||||||
|
handler.setup_environ()
|
||||||
|
|
||||||
|
# Check that wsgi_xxx attributes are copied to wsgi.xxx variables
|
||||||
|
# of handler.environ
|
||||||
|
for attr in ('version', 'multithread', 'multiprocess', 'run_once',
|
||||||
|
'file_wrapper'):
|
||||||
|
self.assertEqual(getattr(handler, 'wsgi_' + attr),
|
||||||
|
handler.environ['wsgi.' + attr])
|
||||||
|
|
||||||
|
# Test handler.environ as a dict
|
||||||
|
expected = {}
|
||||||
|
setup_testing_defaults(expected)
|
||||||
|
# Handler inherits os_environ variables which are not overriden
|
||||||
|
# by SimpleHandler.add_cgi_vars() (SimpleHandler.base_env)
|
||||||
|
for key, value in os_environ.items():
|
||||||
|
if key not in expected:
|
||||||
|
expected[key] = value
|
||||||
|
expected.update({
|
||||||
|
# X doesn't exist in os_environ
|
||||||
|
"X": "Y",
|
||||||
|
# HOME is overriden by TestHandler
|
||||||
|
'HOME': "/override/home",
|
||||||
|
|
||||||
|
# overriden by setup_testing_defaults()
|
||||||
|
"SCRIPT_NAME": "",
|
||||||
|
"SERVER_NAME": "127.0.0.1",
|
||||||
|
|
||||||
|
# set by BaseHandler.setup_environ()
|
||||||
|
'wsgi.input': handler.get_stdin(),
|
||||||
|
'wsgi.errors': handler.get_stderr(),
|
||||||
|
'wsgi.version': (1, 0),
|
||||||
|
'wsgi.run_once': False,
|
||||||
|
'wsgi.url_scheme': 'http',
|
||||||
|
'wsgi.multithread': True,
|
||||||
|
'wsgi.multiprocess': True,
|
||||||
|
'wsgi.file_wrapper': util.FileWrapper,
|
||||||
|
})
|
||||||
|
self.assertDictEqual(handler.environ, expected)
|
||||||
|
|
||||||
def testCGIEnviron(self):
|
def testCGIEnviron(self):
|
||||||
h = BaseCGIHandler(None,None,None,{})
|
h = BaseCGIHandler(None,None,None,{})
|
||||||
|
@ -565,7 +595,7 @@ class HandlerTests(TestCase):
|
||||||
|
|
||||||
|
|
||||||
def test_main():
|
def test_main():
|
||||||
test_support.run_unittest(__name__)
|
support.run_unittest(__name__)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
test_main()
|
test_main()
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix test_wsgiref.testEnviron() to no longer depend on the environment
|
||||||
|
variables (don't fail if "X" variable is set).
|
Loading…
Reference in New Issue