From 24025f4a61ff196d1fefe1a89640ff181acd3aae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20J=C3=BAnior?= Date: Fri, 8 Aug 2014 18:10:01 -0300 Subject: [PATCH] drip endpoint: adding custom status support --- README.md | 2 +- httpbin/core.py | 9 +++++++-- test_httpbin.py | 5 +++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cf153c0..5ca5569 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ Freely hosted in [HTTP](http://httpbin.org), [HTTPS](https://httpbin.org) & [EU] - [`/digest-auth/:qop/:user/:passwd`](http://httpbin.org/digest-auth/auth/user/passwd) Challenges HTTP Digest Auth. - [`/stream/:n`](http://httpbin.org/stream/20) Streams *n*–100 lines. - [`/delay/:n`](http://httpbin.org/delay/3) Delays responding for *n*–10 seconds. -- [`/drip?numbytes=n&duration=s&delay=s`](http://httpbin.org/drip?numbytes=5&duration=5) Drips data over a duration after an optional initial delay. +- [`/drip?numbytes=n&duration=s&delay=s&code=code`](http://httpbin.org/drip?numbytes=5&duration=5&code=200) Drips data over a duration after an optional initial delay, then (optionally) returns with the given status code. - [`/html`](http://httpbin.org/html) Renders an HTML Page. - [`/robots.txt`](http://httpbin.org/robots.txt) Returns some robots.txt rules. - [`/deny`](http://httpbin.org/deny) Denied by robots.txt file. diff --git a/httpbin/core.py b/httpbin/core.py index 4bd29a1..2e8774b 100644 --- a/httpbin/core.py +++ b/httpbin/core.py @@ -405,6 +405,7 @@ def drip(): args = CaseInsensitiveDict(request.args.items()) duration = float(args.get('duration', 2)) numbytes = int(args.get('numbytes', 10)) + code = int(args.get('code', 200)) pause = duration / numbytes delay = float(args.get('delay', 0)) @@ -416,9 +417,13 @@ def drip(): yield u"*".encode('utf-8') time.sleep(pause) - return Response(generate_bytes(), headers={ + response = Response(generate_bytes(), headers={ "Content-Type": "application/octet-stream", - }) + }) + + response.status_code = code + + return response @app.route('/base64/') def decode_base64(value): diff --git a/test_httpbin.py b/test_httpbin.py index ab3e9bb..8af40d8 100755 --- a/test_httpbin.py +++ b/test_httpbin.py @@ -180,6 +180,11 @@ class HttpbinTestCase(unittest.TestCase): self.assertEqual(len(response.get_data()), 400) self.assertEqual(response.status_code, 200) + def test_drip_with_custom_code(self): + response = self.app.get('/drip?numbytes=400&duration=2&code=500') + self.assertEqual(len(response.get_data()), 400) + self.assertEqual(response.status_code, 500) + def test_get_bytes(self): response = self.app.get('/bytes/1024') self.assertEqual(len(response.get_data()), 1024)