Skip to content

Commit c46e429

Browse files
Accept responses with just a status code (Fixes #263)
1 parent 4eac013 commit c46e429

File tree

3 files changed

+30
-22
lines changed

3 files changed

+30
-22
lines changed

src/microdot/microdot.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -520,10 +520,8 @@ class Response:
520520
:param body: The body of the response. If a dictionary or list is given,
521521
a JSON formatter is used to generate the body. If a file-like
522522
object or an async generator is given, a streaming response is
523-
used. If a string is given, it is encoded from UTF-8. If an
524-
integer is given and ``status_code`` isn't given, then the
525-
status code is assigned and the body is kept empty. Else, the
526-
body should be a byte sequence.
523+
used. If a string is given, it is encoded from UTF-8. Else,
524+
the body should be a byte sequence.
527525
:param status_code: The numeric HTTP status code of the response. The
528526
default is 200.
529527
:param headers: A dictionary of headers to include in the response.
@@ -556,14 +554,11 @@ class Response:
556554
#: written to the client. Used to exit WebSocket connections cleanly.
557555
already_handled = None
558556

559-
def __init__(self, body='', status_code=None, headers=None, reason=None):
560-
if body is None and status_code is None:
557+
def __init__(self, body='', status_code=200, headers=None, reason=None):
558+
if body is None and status_code == 200:
561559
body = ''
562560
status_code = 204
563-
elif isinstance(body, int) and status_code is None:
564-
status_code = int(body)
565-
body = ''
566-
self.status_code = status_code or 200
561+
self.status_code = status_code
567562
self.headers = NoCaseDict(headers or {})
568563
self.reason = reason
569564
if isinstance(body, (dict, list)):
@@ -1374,7 +1369,12 @@ async def dispatch_request(self, req):
13741369
if res is None:
13751370
res = await invoke_handler(
13761371
f, req, **req.url_args)
1372+
if isinstance(res, int):
1373+
res = '', res
13771374
if isinstance(res, tuple):
1375+
if isinstance(res[0], int):
1376+
res = ('', res[0],
1377+
res[1] if len(res) > 1 else {})
13781378
body = res[0]
13791379
if isinstance(res[1], int):
13801380
status_code = res[1]

tests/test_microdot.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,14 @@ def four(req):
274274
return '<p>four</p>', 202, \
275275
{'Content-Type': 'text/html; charset=UTF-8'}
276276

277+
@app.route('/status')
278+
def five(req):
279+
return 202
280+
281+
@app.route('/status-headers')
282+
def six(req):
283+
return 202, {'Content-Type': 'text/html; charset=UTF-8'}
284+
277285
client = TestClient(app)
278286

279287
res = self._run(client.get('/body'))
@@ -299,6 +307,18 @@ def four(req):
299307
'text/html; charset=UTF-8')
300308
self.assertEqual(res.text, '<p>four</p>')
301309

310+
res = self._run(client.get('/status'))
311+
self.assertEqual(res.text, '')
312+
self.assertEqual(res.status_code, 202)
313+
self.assertEqual(res.headers['Content-Type'],
314+
'text/plain; charset=UTF-8')
315+
316+
res = self._run(client.get('/status-headers'))
317+
self.assertEqual(res.text, '')
318+
self.assertEqual(res.status_code, 202)
319+
self.assertEqual(res.headers['Content-Type'],
320+
'text/html; charset=UTF-8')
321+
302322
def test_before_after_request(self):
303323
app = Microdot()
304324

tests/test_response.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,6 @@ def test_create_json(self):
109109
fd.response)
110110
self.assertTrue(fd.response.endswith(b'\r\n\r\n[1, "2"]'))
111111

112-
def test_create_status_code(self):
113-
res = Response(202)
114-
self.assertEqual(res.status_code, 202)
115-
self.assertEqual(res.body, b'')
116-
fd = FakeStreamAsync()
117-
self._run(res.write(fd))
118-
self.assertIn(b'HTTP/1.0 202 N/A\r\n', fd.response)
119-
self.assertIn(b'Content-Length: 0\r\n', fd.response)
120-
self.assertIn(b'Content-Type: text/plain; charset=UTF-8\r\n',
121-
fd.response)
122-
self.assertTrue(fd.response.endswith(b'\r\n\r\n'))
123-
124112
def test_create_from_none(self):
125113
res = Response(None)
126114
self.assertEqual(res.status_code, 204)

0 commit comments

Comments
 (0)