@@ -26,13 +26,15 @@ async def handler(request):
26
26
raise exc
27
27
28
28
logger = mock .Mock ()
29
- server = await aiohttp_raw_server (handler , logger = logger )
29
+ server = await aiohttp_raw_server (handler , logger = logger , debug = False )
30
30
cli = await aiohttp_client (server )
31
31
resp = await cli .get ('/path/to' )
32
32
assert resp .status == 500
33
+ assert resp .headers ['Content-Type' ].startswith ('text/plain' )
33
34
34
35
txt = await resp .text ()
35
- assert "<h1>500 Internal Server Error</h1>" in txt
36
+ assert txt .startswith ('500 Internal Server Error' )
37
+ assert 'Traceback' not in txt
36
38
37
39
logger .exception .assert_called_with (
38
40
"Error handling request" ,
@@ -102,10 +104,62 @@ async def handler(request):
102
104
cli = await aiohttp_client (server )
103
105
resp = await cli .get ('/path/to' )
104
106
assert resp .status == 500
107
+ assert resp .headers ['Content-Type' ].startswith ('text/plain' )
105
108
106
109
txt = await resp .text ()
107
- assert "<h2> Traceback:</h2>" in txt
110
+ assert ' Traceback (most recent call last): \n ' in txt
108
111
109
112
logger .exception .assert_called_with (
110
113
"Error handling request" ,
111
114
exc_info = exc )
115
+
116
+
117
+ async def test_raw_server_html_exception (aiohttp_raw_server , aiohttp_client ):
118
+ exc = RuntimeError ("custom runtime error" )
119
+
120
+ async def handler (request ):
121
+ raise exc
122
+
123
+ logger = mock .Mock ()
124
+ server = await aiohttp_raw_server (handler , logger = logger , debug = False )
125
+ cli = await aiohttp_client (server )
126
+ resp = await cli .get ('/path/to' , headers = {'Accept' : 'text/html' })
127
+ assert resp .status == 500
128
+ assert resp .headers ['Content-Type' ].startswith ('text/html' )
129
+
130
+ txt = await resp .text ()
131
+ assert txt == (
132
+ '<html><head><title>500 Internal Server Error</title></head><body>\n '
133
+ '<h1>500 Internal Server Error</h1>\n '
134
+ 'Server got itself in trouble\n '
135
+ '</body></html>\n '
136
+ )
137
+
138
+ logger .exception .assert_called_with (
139
+ "Error handling request" , exc_info = exc )
140
+
141
+
142
+ async def test_raw_server_html_exception_debug (aiohttp_raw_server ,
143
+ aiohttp_client ):
144
+ exc = RuntimeError ("custom runtime error" )
145
+
146
+ async def handler (request ):
147
+ raise exc
148
+
149
+ logger = mock .Mock ()
150
+ server = await aiohttp_raw_server (handler , logger = logger , debug = True )
151
+ cli = await aiohttp_client (server )
152
+ resp = await cli .get ('/path/to' , headers = {'Accept' : 'text/html' })
153
+ assert resp .status == 500
154
+ assert resp .headers ['Content-Type' ].startswith ('text/html' )
155
+
156
+ txt = await resp .text ()
157
+ assert txt .startswith (
158
+ '<html><head><title>500 Internal Server Error</title></head><body>\n '
159
+ '<h1>500 Internal Server Error</h1>\n '
160
+ '<h2>Traceback:</h2>\n '
161
+ '<pre>Traceback (most recent call last):\n '
162
+ )
163
+
164
+ logger .exception .assert_called_with (
165
+ "Error handling request" , exc_info = exc )
0 commit comments