test_basehttp.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from io import BytesIO
  2. from django.core.handlers.wsgi import WSGIRequest
  3. from django.core.servers.basehttp import WSGIRequestHandler
  4. from django.test import SimpleTestCase
  5. from django.test.client import RequestFactory
  6. from django.test.utils import captured_stderr
  7. class Stub(object):
  8. def __init__(self, **kwargs):
  9. self.__dict__.update(kwargs)
  10. class WSGIRequestHandlerTestCase(SimpleTestCase):
  11. def test_log_message(self):
  12. request = WSGIRequest(RequestFactory().get('/').environ)
  13. request.makefile = lambda *args, **kwargs: BytesIO()
  14. handler = WSGIRequestHandler(request, '192.168.0.2', None)
  15. with captured_stderr() as stderr:
  16. handler.log_message('GET %s %s', 'A', 'B')
  17. self.assertIn('] GET A B', stderr.getvalue())
  18. def test_https(self):
  19. request = WSGIRequest(RequestFactory().get('/').environ)
  20. request.makefile = lambda *args, **kwargs: BytesIO()
  21. handler = WSGIRequestHandler(request, '192.168.0.2', None)
  22. with captured_stderr() as stderr:
  23. handler.log_message("GET %s %s", str('\x16\x03'), "4")
  24. self.assertIn(
  25. "You're accessing the development server over HTTPS, "
  26. "but it only supports HTTP.",
  27. stderr.getvalue()
  28. )
  29. def test_strips_underscore_headers(self):
  30. """WSGIRequestHandler ignores headers containing underscores.
  31. This follows the lead of nginx and Apache 2.4, and is to avoid
  32. ambiguity between dashes and underscores in mapping to WSGI environ,
  33. which can have security implications.
  34. """
  35. def test_app(environ, start_response):
  36. """A WSGI app that just reflects its HTTP environ."""
  37. start_response('200 OK', [])
  38. http_environ_items = sorted(
  39. '%s:%s' % (k, v) for k, v in environ.items()
  40. if k.startswith('HTTP_')
  41. )
  42. yield (','.join(http_environ_items)).encode('utf-8')
  43. rfile = BytesIO()
  44. rfile.write(b"GET / HTTP/1.0\r\n")
  45. rfile.write(b"Some-Header: good\r\n")
  46. rfile.write(b"Some_Header: bad\r\n")
  47. rfile.write(b"Other_Header: bad\r\n")
  48. rfile.seek(0)
  49. # WSGIRequestHandler closes the output file; we need to make this a
  50. # no-op so we can still read its contents.
  51. class UnclosableBytesIO(BytesIO):
  52. def close(self):
  53. pass
  54. wfile = UnclosableBytesIO()
  55. def makefile(mode, *a, **kw):
  56. if mode == 'rb':
  57. return rfile
  58. elif mode == 'wb':
  59. return wfile
  60. request = Stub(makefile=makefile)
  61. server = Stub(base_environ={}, get_app=lambda: test_app)
  62. # We don't need to check stderr, but we don't want it in test output
  63. with captured_stderr():
  64. # instantiating a handler runs the request as side effect
  65. WSGIRequestHandler(request, '192.168.0.2', server)
  66. wfile.seek(0)
  67. body = list(wfile.readlines())[-1]
  68. self.assertEqual(body, b'HTTP_SOME_HEADER:good')