mirror of
https://github.com/kennethreitz/httpbin.git
synced 2026-06-05 06:46:16 +00:00
Add /deflate endpoint
returns response with deflate encoded body(Content-Encoding='deflate').
This commit is contained in:
@@ -16,6 +16,7 @@ Freely hosted in [HTTP](http://httpbin.org) &
|
||||
- [`/put`](http://hurl.it/hurls/18016368fa5e5eef80db935f5dae615d5858a4a4/a512d904f5da64df9627ee998c040f7874d6436a) Returns PUT data.
|
||||
- [`/delete`](http://hurl.it/hurls/6faafea5191f77172fca4cfe1505739230d5f769/bc255ffc69e04c2c8b968822c59544746bbb872c) Returns DELETE data
|
||||
- [`/gzip`](http://httpbin.org/gzip) Returns gzip-encoded data.
|
||||
- [`/deflate`](http://httpbin.org/deflate) Returns deflate-encoded data.
|
||||
- [`/status/:code`](http://httpbin.org/status/418) Returns given HTTP Status code.
|
||||
- [`/response-headers?key=val`](http://httpbin.org/response-headers?Content-Type=text/plain;%20charset=UTF-8&Server=httpbin) Returns given response headers.
|
||||
- [`/redirect/:n`](http://httpbin.org/redirect/6) 302 Redirects *n* times.
|
||||
@@ -43,7 +44,7 @@ Freely hosted in [HTTP](http://httpbin.org) &
|
||||
|
||||
Testing an HTTP Library can become difficult sometimes. PostBin.org is fantastic
|
||||
for testing POST requests, but not much else. This exists to cover all kinds of HTTP
|
||||
scenarios. Additional endpoints are being considered (e.g. `/deflate`).
|
||||
scenarios. Additional endpoints are being considered.
|
||||
|
||||
All endpoint responses are JSON-encoded.
|
||||
|
||||
|
||||
+10
-1
@@ -168,6 +168,15 @@ def view_gzip_encoded_content():
|
||||
'origin', 'headers', method=request.method, gzipped=True))
|
||||
|
||||
|
||||
@app.route('/deflate')
|
||||
@filters.deflate
|
||||
def view_deflate_encoded_content():
|
||||
"""Returns Deflate-Encoded Data."""
|
||||
|
||||
return jsonify(get_dict(
|
||||
'origin', 'headers', method=request.method, deflated=True))
|
||||
|
||||
|
||||
@app.route('/redirect/<int:n>')
|
||||
def redirect_n_times(n):
|
||||
"""301 Redirects n times."""
|
||||
@@ -385,7 +394,7 @@ def drip():
|
||||
delay = float(args.get('delay', 0))
|
||||
if delay > 0:
|
||||
time.sleep(delay)
|
||||
|
||||
|
||||
def generate_bytes():
|
||||
for i in xrange(numbytes):
|
||||
yield bytes(chr(42))
|
||||
|
||||
@@ -8,6 +8,7 @@ This module provides response filter decorators.
|
||||
"""
|
||||
|
||||
import gzip as gzip2
|
||||
import zlib
|
||||
|
||||
from cStringIO import StringIO
|
||||
from decimal import Decimal
|
||||
@@ -63,3 +64,27 @@ def gzip(f, *args, **kwargs):
|
||||
|
||||
return gzip_data
|
||||
|
||||
|
||||
@decorator
|
||||
def deflate(f, *args, **kwargs):
|
||||
"""Deflate Flask Response Decorator."""
|
||||
|
||||
data = f(*args, **kwargs)
|
||||
|
||||
if isinstance(data, Response):
|
||||
content = data.data
|
||||
else:
|
||||
content = data
|
||||
|
||||
deflater = zlib.compressobj()
|
||||
deflated_data = deflater.compress(content)
|
||||
deflated_data += deflater.flush()
|
||||
|
||||
if isinstance(data, Response):
|
||||
data.data = deflated_data
|
||||
data.headers['Content-Encoding'] = 'deflate'
|
||||
data.headers['Content-Length'] = str(len(data.data))
|
||||
|
||||
return data
|
||||
|
||||
return deflated_data
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
<li><code>/patch</code> Returns PATCH data.</li>
|
||||
<li><code>/delete</code> Returns DELETE data</li>
|
||||
<li><a href="/gzip" data-bare-link="true"><code>/gzip</code></a> Returns gzip-encoded data.</li>
|
||||
<li><a href="/deflate" data-bare-link="true"><code>/deflate</code></a> Returns deflate-encoded data.</li>
|
||||
<li><a href="/status/418"><code>/status/:code</code></a> Returns given HTTP Status code.</li>
|
||||
<li><a href="/response-headers?Content-Type=text/plain;%20charset=UTF-8&Server=httpbin"><code>/response-headers?key=val</code></a> Returns given response headers.</li>
|
||||
<li><a href="/redirect/6"><code>/redirect/:n</code></a> 302 Redirects <em>n</em> times.</li>
|
||||
@@ -42,7 +43,7 @@
|
||||
|
||||
<p>Testing an HTTP Library can become difficult sometimes. <a href="http://postbin.org">Postbin</a> is fantastic
|
||||
for testing POST requests, but not much else. This exists to cover all kinds of HTTP
|
||||
scenarios. Additional endpoints are being considered (e.g. <code>/deflate</code>).</p>
|
||||
scenarios. Additional endpoints are being considered.</p>
|
||||
|
||||
<p>All endpoint responses are JSON-encoded.</p>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user