Skip to content

Instantly share code, notes, and snippets.

@boxmein
Created February 5, 2020 14:29
Show Gist options
  • Save boxmein/e3da9da0d6bc000d99f76a5869a11f21 to your computer and use it in GitHub Desktop.
Save boxmein/e3da9da0d6bc000d99f76a5869a11f21 to your computer and use it in GitHub Desktop.
MITMProxy script that fakes HTTP endpoint error
#!/usr/bin/env python3
# Install mitmdump & mitmproxy with `brew install mitmproxy`
# run with: mitmdump -s mitm-error.py
# fire requests: curl --proxy localhost:8080 https://test.example.com/test
# set up your system proxy or configure your browser to use the proxy localhost:8080
import sys
import re
import json
from mitmproxy import http, proxy, options
# Add regexes that match URLs
# if any of these match, use the response (it can be customized below)
regexes = [
'/test$'
]
def url_matches(url, regexes):
return any(re.search(regex, url) for regex in regexes)
# When a request is encountered, do this thing:
def request(flow):
if url_matches(flow.request.pretty_url, regexes):
print("Matched request: ", flow.request.pretty_url)
flow.response = http.HTTPResponse.make(
500, # Status code: HTTP 500 (Internal Server Error)
json.dumps({ # Response body as bytestring
'statusCode': 500,
'message': 'broken'
}).encode("utf-8"),
{ # Response headers
'Content-Type': 'application/json'
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment