contrib: Make the failures mitmproxy script a bit smarter.
parent
83926e154b
commit
56a696d1c0
|
@ -11,6 +11,7 @@ To run execute it with mitmproxy:
|
||||||
"""
|
"""
|
||||||
import time
|
import time
|
||||||
import json
|
import json
|
||||||
|
import random
|
||||||
|
|
||||||
from mitmproxy import http
|
from mitmproxy import http
|
||||||
from mitmproxy.script import concurrent
|
from mitmproxy.script import concurrent
|
||||||
|
@ -18,30 +19,43 @@ from mitmproxy.script import concurrent
|
||||||
REQUEST_COUNT = 0
|
REQUEST_COUNT = 0
|
||||||
|
|
||||||
|
|
||||||
@concurrent
|
def timeout(flow):
|
||||||
def request(flow):
|
timeout = 60 if "sync" in flow.request.pretty_url else 30
|
||||||
global REQUEST_COUNT
|
time.sleep(timeout)
|
||||||
|
return None
|
||||||
|
|
||||||
REQUEST_COUNT += 1
|
|
||||||
|
|
||||||
if REQUEST_COUNT % 2 == 0:
|
# A map holding our failure modes.
|
||||||
return
|
# The keys are just descriptive names for the failure mode while the values
|
||||||
elif REQUEST_COUNT % 3 == 0:
|
# hold a tuple containing a function that may or may not create a failure and
|
||||||
flow.response = http.HTTPResponse.make(
|
# the probability weight at which rate this failure should be triggered.
|
||||||
500,
|
#
|
||||||
b"Gateway error",
|
# The method should return an http.HTTPResponse if it should modify the
|
||||||
)
|
# response or None if the response should be passed as is.
|
||||||
elif REQUEST_COUNT % 7 == 0:
|
FAILURES = {
|
||||||
if "sync" in flow.request.pretty_url:
|
"Success": (lambda x: None, 50),
|
||||||
time.sleep(60)
|
"Gateway error":
|
||||||
else:
|
(lambda _: http.HTTPResponse.make(500, b"Gateway error"), 20),
|
||||||
time.sleep(30)
|
"Limit exeeded": (lambda _: http.HTTPResponse.make(
|
||||||
else:
|
|
||||||
flow.response = http.HTTPResponse.make(
|
|
||||||
429,
|
429,
|
||||||
json.dumps({
|
json.dumps({
|
||||||
"errcode": "M_LIMIT_EXCEEDED",
|
"errcode": "M_LIMIT_EXCEEDED",
|
||||||
"error": "Too many requests",
|
"error": "Too many requests",
|
||||||
"retry_after_ms": 2000
|
"retry_after_ms": 2000
|
||||||
})
|
})), 20),
|
||||||
)
|
"Timeout error": (timeout, 10)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@concurrent
|
||||||
|
def request(flow):
|
||||||
|
global FAILURES
|
||||||
|
|
||||||
|
weights = [weight for (_, weight) in FAILURES.values()]
|
||||||
|
failure = random.choices(list(FAILURES), weights=weights)[0]
|
||||||
|
failure_func, _ = FAILURES[failure]
|
||||||
|
|
||||||
|
response = failure_func(flow)
|
||||||
|
|
||||||
|
if response:
|
||||||
|
flow.response = response
|
||||||
|
|
Loading…
Reference in New Issue