-
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
30 changed files
with
713 additions
and
680 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 |
---|---|---|
@@ -1 +1 @@ | ||
0.1.10 | ||
0.1.11 |
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,45 +1,42 @@ | ||
.. basic | ||
.. _basic | ||
===== | ||
basic | ||
******** | ||
===== | ||
|
||
This is by far the simplest example on how to get started with HoverPy. Let's import our most important class HoverPy, along with whatever else we may need | ||
|
||
Import hoverpy's main class: HoverPy | ||
:: | ||
|
||
.. code:: python | ||
>>> from hoverpy import HoverPy | ||
>>> import requests | ||
|
||
from hoverpy import HoverPy | ||
|
||
Import requests for http | ||
Now let's create our HoverPy object in capture mode. We do so with a `with` statement as this is the pythonic way, although this is not a necessity. | ||
|
||
.. code:: python | ||
:: | ||
|
||
import requests | ||
>>> with HoverPy(capture=True) as hoverpy: | ||
|
||
Create our HoverPy object in capture mode | ||
|
||
.. code:: python | ||
Print the json from our get request. Hoverpy acted as a proxy: it made the request on our behalf, captured it, and returned it to us. | ||
|
||
hp = HoverPy(capture=True) | ||
:: | ||
|
||
Print the json from our get request. Hoverpy acted as a proxy: it made | ||
the request on our behalf, captured it, and returned it to us. | ||
>>> print(requests.get("http://ip.jsontest.com/myip").json()) | ||
|
||
.. code:: python | ||
|
||
print(requests.get("http://ip.jsontest.com/myip").json()) | ||
Switch HoverPy to simulate mode. HoverPy no longer acts as a proxy; all it does from now on is replay the captured data. | ||
|
||
Switch HoverPy to simulate mode. HoverPy no longer acts as a proxy; all | ||
it does from now on is replay the captured data. | ||
:: | ||
|
||
.. code:: python | ||
>>> hoverpy.simulate() | ||
|
||
hp.simulate() | ||
|
||
Print the json from our get request. This time the data comes from the | ||
store. | ||
Print the json from our get request. This time the data comes from the store. | ||
|
||
.. code:: python | ||
:: | ||
|
||
>>> print(requests.get("http://ip.jsontest.com/myip").json()) | ||
|
||
print(requests.get("http://ip.jsontest.com/myip").json()) | ||
|
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 |
---|---|---|
@@ -1,86 +1,89 @@ | ||
.. delays | ||
.. _delays | ||
====== | ||
delays | ||
******** | ||
====== | ||
|
||
Demonstrates how to add latency to calls, based on host, and method type. import hoverpy's main class: HoverPy | ||
|
||
Demonstrates how to add latency to calls, based on host, and method | ||
type. import hoverpy's main class: HoverPy | ||
:: | ||
|
||
.. code:: python | ||
>>> from hoverpy import HoverPy | ||
|
||
from hoverpy import HoverPy | ||
|
||
Import requests and random for http and testing | ||
Import requests and random for http and testing | ||
|
||
.. code:: python | ||
:: | ||
|
||
import requests | ||
import random | ||
>>> import requests | ||
>>> import random | ||
|
||
Create our HoverPy object in capture mode | ||
|
||
.. code:: python | ||
Create our HoverPy object in capture mode | ||
|
||
hp = HoverPy(capture=True) | ||
:: | ||
|
||
This function either generates a echo server url, or a md5 url it is | ||
seeded so that we get the exact same requests on capture as we do on | ||
simulate | ||
>>> with HoverPy(capture=True) as hp: | ||
|
||
.. code:: python | ||
|
||
def getServiceData(): | ||
for i in range(10): | ||
random.seed(i) | ||
print( | ||
requests.get( | ||
random.choice( | ||
[ | ||
"http://echo.jsontest.com/i/%i" % | ||
i, | ||
"http://md5.jsontest.com/?text=%i" % | ||
i])).json()) | ||
This function either generates a echo server url, or a md5 url it is seeded so that we get the exact same requests on capture as we do on simulate | ||
|
||
Make the requests to the desired host dependencies | ||
:: | ||
|
||
.. code:: python | ||
>>> def getServiceData(): | ||
>>> for i in range(10): | ||
>>> random.seed(i) | ||
>>> print( | ||
>>> requests.get( | ||
>>> random.choice( | ||
>>> [ | ||
>>> "http://echo.jsontest.com/i/%i" % | ||
>>> i, | ||
>>> "http://md5.jsontest.com/?text=%i" % | ||
>>> i])).json()) | ||
|
||
print("capturing responses from echo server\n") | ||
getServiceData() | ||
|
||
There are two ways to add delays. One is to call the delays method with | ||
the desired delay rules passed in as a json document | ||
Make the requests to the desired host dependencies | ||
|
||
.. code:: python | ||
:: | ||
|
||
print(hp.delays({"data": [ | ||
{ | ||
"urlPattern": "md5.jsontest.com", | ||
"delay": 1000 | ||
} | ||
] | ||
} | ||
)) | ||
>>> print("capturing responses from echo server\n") | ||
>>> getServiceData() | ||
|
||
The other more pythonic way is to call addDelay(...) | ||
|
||
.. code:: python | ||
There are two ways to add delays. One is to call the delays method with the desired delay rules passed in as a json document | ||
|
||
print(hp.addDelay(urlPattern="echo.jsontest.com", delay=3000)) | ||
:: | ||
|
||
Now let's switch over to simulate mode | ||
>>> print(hp.delays({"data": [ | ||
>>> { | ||
>>> "urlPattern": "md5.jsontest.com", | ||
>>> "delay": 1000 | ||
>>> } | ||
>>> ] | ||
>>> } | ||
>>> )) | ||
|
||
.. code:: python | ||
|
||
print(hp.simulate()) | ||
The other more pythonic way is to call addDelay(...) | ||
|
||
Make the requests. This time HoverFly adds the simulated delays. these | ||
requests would normally be run asynchronously, and we could deal | ||
gracefully with the dependency taking too long to respond | ||
:: | ||
|
||
.. code:: python | ||
>>> print(hp.addDelay(urlPattern="echo.jsontest.com", delay=3000)) | ||
|
||
|
||
Now let's switch over to simulate mode | ||
|
||
:: | ||
|
||
>>> print(hp.simulate()) | ||
|
||
|
||
Make the requests. This time HoverFly adds the simulated delays. these requests would normally be run asynchronously, and we could deal gracefully with the dependency taking too long to respond | ||
|
||
:: | ||
|
||
>>> print("\nreplaying delayed responses from echo server\n") | ||
>>> getServiceData() | ||
|
||
print("\nreplaying delayed responses from echo server\n") | ||
getServiceData() | ||
|
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,48 +1,45 @@ | ||
.. modify | ||
.. _modify | ||
====== | ||
modify | ||
******** | ||
====== | ||
|
||
Demonstrates how to modify requests. 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 | ||
|
||
Demonstrates how to modify requests. 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 | ||
:: | ||
|
||
.. code:: python | ||
>>> from hoverpy import HoverPy | ||
|
||
from hoverpy import HoverPy | ||
|
||
Import requests for http | ||
Import requests for http | ||
|
||
.. code:: python | ||
:: | ||
|
||
import requests | ||
>>> 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 | ||
|
||
.. code:: python | ||
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 | ||
|
||
hoverpy = HoverPy( | ||
flags=[ | ||
"-modify", | ||
"-middleware", | ||
"python examples/modify/modify_payload.py"]) | ||
:: | ||
|
||
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 | ||
>>> with HoverPy( | ||
>>> flags=[ | ||
>>> "-modify", | ||
>>> "-middleware", | ||
>>> "python examples/modify/modify_payload.py"]) as hoverpy: | ||
|
||
.. code:: python | ||
|
||
for i in range(30): | ||
r = requests.get("http://time.jsontest.com") | ||
if "time" in r.json().keys(): | ||
print( | ||
"response successfully modified, current date is " + | ||
r.json()["time"]) | ||
else: | ||
print("something went wrong - deal with it gracefully") | ||
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 | ||
|
||
:: | ||
|
||
>>> for i in range(30): | ||
>>> r = requests.get("http://time.jsontest.com") | ||
>>> if "time" in r.json().keys(): | ||
>>> print( | ||
>>> "response successfully modified, current date is " + | ||
>>> r.json()["time"]) | ||
>>> else: | ||
>>> print("something went wrong - deal with it gracefully") | ||
|
||
|
Oops, something went wrong.