Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
shyal committed Dec 2, 2016
1 parent 4a9d6b0 commit 562dba8
Show file tree
Hide file tree
Showing 30 changed files with 713 additions and 680 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.10
0.1.11
43 changes: 20 additions & 23 deletions docs/source/basic.rst
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())

4 changes: 2 additions & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@
# built documents.
#
# The short X.Y version.
version = '0.1.10'
version = '0.1.11'
# The full version, including alpha/beta/rc tags.
release = '0.1.10'
release = '0.1.11'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
113 changes: 58 additions & 55 deletions docs/source/delays.rst
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()

61 changes: 29 additions & 32 deletions docs/source/modify.rst
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")


0 comments on commit 562dba8

Please sign in to comment.