modify

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.

>>> from hoverpy import HoverPy
>>> import requests
>>> with HoverPy(
>>>         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 http://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"])

The 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.

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!