Add random status code feature

Fixes issue #22
This commit is contained in:
Johannes
2012-01-20 04:11:41 +00:00
parent 2768b5486e
commit 7646e33daa
2 changed files with 49 additions and 2 deletions
+20 -2
View File
@@ -17,7 +17,7 @@ from werkzeug.datastructures import WWWAuthenticate
from . import filters
from .helpers import get_headers, status_code, get_dict, check_basic_auth, check_digest_auth, H
from .utils import weighted_choice
ENV_COOKIES = (
'_gauges_unique',
@@ -80,7 +80,6 @@ def view_get():
@app.route('/post', methods=('POST',))
def view_post():
"""Returns POST Data."""
@@ -173,6 +172,25 @@ def view_status_code(code):
return status_code(code)
@app.route('/random-status/<codes>')
def view_random_status_code(codes):
"""Return random status code from given status codes."""
choices = []
for choice in codes.split(','):
if not ':' in choice:
code = choice
weight = 1
else:
code, weight = choice.split(':')
choices.append((int(code), float(weight)))
code = weighted_choice(choices)
return status_code(code)
@app.route('/cookies')
def view_cookies(hide_env=True):
"""Returns cookie data."""
+29
View File
@@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
"""
httpbin.utils
~~~~~~~~~~~~~~~
Utility functions.
"""
import random
import bisect
def weighted_choice(choices):
"""Returns a value from choices chosen by weighted random selection
choices should be a list of (value, weight) tuples.
eg. weighted_choice([('val1', 5), ('val2', 0.3), ('val3', 1)])
"""
values, weights = zip(*choices)
total = 0
cum_weights = []
for w in weights:
total += w
cum_weights.append(total)
x = random.uniform(0, total)
i = bisect.bisect(cum_weights, x)
return values[i]