From 28124e7bdf165689f92d667af1b77169ec75486c Mon Sep 17 00:00:00 2001 From: Petter Strandmark Date: Fri, 11 Dec 2020 14:05:36 +0100 Subject: [PATCH] Refs #4444 -- Added tests for handling broken pipe errors in WSGIServer. --- tests/servers/test_basehttp.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/tests/servers/test_basehttp.py b/tests/servers/test_basehttp.py index 32fdbf3c0e..4c685b282f 100644 --- a/tests/servers/test_basehttp.py +++ b/tests/servers/test_basehttp.py @@ -1,9 +1,10 @@ from io import BytesIO from django.core.handlers.wsgi import WSGIRequest -from django.core.servers.basehttp import WSGIRequestHandler +from django.core.servers.basehttp import WSGIRequestHandler, WSGIServer from django.test import SimpleTestCase from django.test.client import RequestFactory +from django.test.utils import captured_stderr class Stub: @@ -102,3 +103,25 @@ class WSGIRequestHandlerTestCase(SimpleTestCase): body = list(wfile.readlines())[-1] self.assertEqual(body, b'HTTP_SOME_HEADER:good') + + +class WSGIServerTestCase(SimpleTestCase): + request_factory = RequestFactory() + + def test_broken_pipe_errors(self): + """WSGIServer handles broken pipe errors.""" + request = WSGIRequest(self.request_factory.get('/').environ) + client_address = ('192.168.2.0', 8080) + msg = f'- Broken pipe from {client_address}\n' + try: + server = WSGIServer(('localhost', 0), WSGIRequestHandler) + try: + raise BrokenPipeError() + except Exception: + with captured_stderr() as err: + with self.assertLogs('django.server', 'INFO') as cm: + server.handle_error(request, client_address) + self.assertEqual(err.getvalue(), '') + self.assertEqual(cm.records[0].getMessage(), msg) + finally: + server.server_close()