-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
151 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ Contents | |
======== | ||
|
||
.. toctree:: | ||
:maxdepth: 2 | ||
:maxdepth: 3 | ||
|
||
installation | ||
usage | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
.. modify_payload | ||
============== | ||
modify_payload | ||
============== | ||
|
||
|
||
|
||
:: | ||
|
||
>>> #!/usr/bin/env python | ||
|
||
|
||
This is the payload modification script. It truly allows us to do all types of weird, wild and wonderful mutations to the data that gets sent back to our application. Let's begin by imporing what we'll need. | ||
|
||
:: | ||
|
||
>>> import sys | ||
>>> import json | ||
>>> import logging | ||
>>> import random | ||
>>> logging.basicConfig(filename='middleware.log', level=logging.DEBUG) | ||
>>> logging.debug('Middleware "modify_request" called') | ||
|
||
|
||
Above we've also configured our logging. This is essential, as it's difficult to figure out what went wrong otherwise. | ||
|
||
:: | ||
|
||
>>> def main(): | ||
>>> data = sys.stdin.readlines() | ||
>>> payload = data[0] | ||
>>> logging.debug(payload) | ||
>>> payload_dict = json.loads(payload) | ||
|
||
|
||
The response to our request gets sent to middleware via stdin. Therefore, we are really only interested in the first line. | ||
|
||
:: | ||
|
||
>>> payload_dict['response']['status'] = random.choice([200, 201]) | ||
|
||
|
||
Let's randomly switch the status for the responses between 200, and 201. This helps us build a resilient client, that can deal with curved balls. | ||
|
||
:: | ||
|
||
>>> if random.choice([True, False]): | ||
>>> payload_dict['response']['body'] = "{}" | ||
|
||
|
||
Let's also randomly return an empty response body. This is tricky middleware indeed. | ||
|
||
:: | ||
|
||
>>> print(json.dumps(payload_dict)) | ||
>>> if __name__ == "__main__": | ||
>>> main() | ||
|
||
|
||
If is good practice for your client to be able to deal with unexpected data. This is a great example building middleware that'll thoroughly test your apps. | ||
|
||
We are now ready to run our payload modification script: ``python examples/modify/modify.py`` | ||
|
||
Output: | ||
|
||
>>> something went wrong - deal with it gracefully | ||
>>> something went wrong - deal with it gracefully | ||
>>> something went wrong - deal with it gracefully | ||
>>> something went wrong - deal with it gracefully | ||
>>> something went wrong - deal with it gracefully | ||
>>> response successfully modified, current date is 01:45:15 PM | ||
>>> something went wrong - deal with it gracefully | ||
>>> something went wrong - deal with it gracefully | ||
>>> response successfully modified, current date is 01:45:16 PM | ||
>>> [...] | ||
|
||
Excellent, above we can see how our application now deals with dire responses adequately. This is how resilient software is built! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,32 @@ | ||
# Demonstrates how to modify requests. This is particularly useful for sending curved | ||
# Let's look into mutating responses using middleware. This is particularly useful for sending curved | ||
# balls to your applications, and make sure they deal with them correctly. | ||
# import hoverpy's main class: HoverPy | ||
from hoverpy import HoverPy | ||
|
||
# import requests for http | ||
import requests | ||
|
||
# create our HoverPy object with modify and middleware enabled. | ||
# please note this brings in ```python examples/modify/modify_payload.py``` | ||
# which will get run on every request | ||
with HoverPy( | ||
flags=[ | ||
"-modify", | ||
"-middleware", | ||
"python examples/modify/modify_payload.py"]) as hoverpy: | ||
|
||
# our middleware is designed to random return an empty body instead of what it's supposed | ||
# to return (the curren time). This is a good example of how to alter your dependencies, | ||
# and adequately test and react based on their content | ||
modify=True, | ||
middleware="python examples/modify/modify_payload.py") as hoverpy: | ||
|
||
# Above we created our HoverPy object with modify and middleware enabled. | ||
# Please note this brings in ```python examples/modify/modify_payload.py``` | ||
# which will get run on every request. | ||
|
||
for i in range(30): | ||
r = requests.get("http://time.jsontest.com") | ||
|
||
# let's make 30 requests to time.jsontest.com which simply gets us the | ||
# current local time | ||
|
||
if "time" in r.json().keys(): | ||
print( | ||
"response successfully modified, current date is " + | ||
r.json()["time"]) | ||
|
||
# time ``time`` key is inside the response, which is what we expected. | ||
|
||
else: | ||
print("something went wrong - deal with it gracefully") | ||
|
||
# however if the ``time`` key isn't in the response, then something | ||
# clearly went wrong. Next let's take a look at the middleware.<br><br> | ||
# .. include:: modify_payload.rst |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters