<commit_before>Flea
=====

Flea helps you test WSGI applications.

Contents:

.. toctree::
        :maxdepth: 2


Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`


.. include:: ../README

Driving WSGI applications
-------------------------

Use the ``TestAgent`` class to produce a user agent capable of driving your
WSGI application::

	>>> from flea import TestAgent
	>>> agent = TestAgent(my_wsgi_app)

You can now use this to navigate your WSGI application by...


.. testsetup:: *

        from pesto.request import Request
        from pesto.response import Response
        from pesto.testing import make_environ
        from pesto.wsgiutils import mount_app
        from flea import TestAgent
        agent = TestAgent(
                mount_app({
                        '/': Response(["""
                                                <a id="mylink" href="/">foo</a>
                                                <form name="login-form" action="/"></form>
                                                <form name="contact" action="/"><button type="submit" name="send"/></form>
                                                <form name="register" action="/register"></form>
                        """]),
                        '/register': Response.redirect('/', Request(make_environ()))
                })
        ).get('/')

Making GET requests:

.. doctest::

	>>> agent = agent.get('/my-page')

Making POST requests:

.. doctest::

	>>> agent = agent.post('/contact', data={'message': 'your father smells of elderberries'})

Clicking links:

.. doctest::

	>>> # Click on first <a> tag on page
	>>> agent = agent["//a[1]"].click()


	>>> # Click on first <a> tag with content 'foo'
	>>> agent = agent["//a[.='foo']"].click()

	>>> # Click on tag with id 'mylink'
	>>> agent = agent["//a[@id='mylink']"].click()

Submitting forms:

.. doctest::

	>>> agent = agent["//form"].submit()
	>>> agent = agent["//form[@name='login-form']"].submit()
	>>> agent = agent["//form[@name='contact']//button[@name='send']"].submit()

 Following HTTP redirects:
 
 	>>> agent = agent["//form[@name='register']"].submit()
 	>>> agent = agent.follow()
 
 	>>> # Or more succinctly...
 	>>> agent = agent["//form"].submit(follow=True)
 
Don't like XPath? Use CSS selectors instead::

	>>> agent = agent.findcss("a").click()
	>>> agent = agent.findcss("a.highlighted").click()
	>>> agent = agent.findcss("a#mylink").click()


Querying WSGI application responses
-----------------------------------

Checking the content of the request
`````````````````````````````````````

::

	>>> assert agent.request.path_info == '/index.html'
	>>> print agent.request.environ
        {...}

``agent.request`` is a `pesto.request.Request <http://pypi.python.org/pypi/pesto>`_
object, and all attributes of that class are available to examine.
<commit_msg>Updated docs for fileuploads<commit_after>Flea
=====

Flea helps you test WSGI applications.

Contents:

.. toctree::
        :maxdepth: 2


Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`


.. include:: ../README

Driving WSGI applications
-------------------------

Use the ``TestAgent`` class to produce a user agent capable of driving your
WSGI application::

	>>> from flea import TestAgent
	>>> agent = TestAgent(my_wsgi_app)

You can now use this to navigate your WSGI application by...


.. testsetup:: *

        from pesto.request import Request
        from pesto.response import Response
        from pesto.testing import make_environ
        from pesto.wsgiutils import mount_app
        from flea import TestAgent
        agent = TestAgent(
                mount_app({
                        '/': Response(["""
                                                <a id="mylink" href="/">foo</a>
                                                <form name="login-form" action="/"></form>
                                                <form name="contact" action="/"><button type="submit" name="send"/></form>
                                                <form name="register" action="/register"></form>
                                                <form name="upload" action="/">
                                                        <input type="file" name="photo"/>
                                                        <input type="text" name="title"/>
                                                </form>
                        """]),
                        '/register': Response.redirect('/', Request(make_environ()))
                })
        ).get('/')

Making GET requests:

.. doctest::

	>>> agent = agent.get('/my-page')

Making POST requests:

.. doctest::

	>>> agent = agent.post('/contact', data={'message': 'your father smells of elderberries'})

Clicking links:

.. doctest::

	>>> # Click on first <a> tag on page
	>>> agent = agent["//a[1]"].click()


	>>> # Click on first <a> tag with content 'foo'
	>>> agent = agent["//a[.='foo']"].click()

	>>> # Click on tag with id 'mylink'
	>>> agent = agent["//a[@id='mylink']"].click()

Submitting forms:

.. doctest::

	>>> agent["//input[@name='title']"].value = 'picture of me on holiday'
	>>> agent = agent["//form[@name='login-form']"].submit()
	>>> agent = agent["//form[@name='contact']//button[@name='send']"].submit()

When testing file upload fields, you need to assign a tuple of ``(filename,
content-type, data)``::



	>>> agent["//input[@name='title']"].value = 'picture of me on holiday'

        >>> # file data can be either a string
	>>> agent["//input[@name='image']"].value = ('photo.jpg', 'image/jpeg', 'test data')

        >>> # ...or a file object
        >>> photo = open('my-photo.jpg', 'r')
	>>> agent["//input[@name='image']"].value = ('photo.jpg', 'image/jpeg', photo)

Following HTTP redirects::
 
 	>>> agent = agent["//form[@name='register']"].submit()
 	>>> agent = agent.follow()
 
 	>>> # Or just...
 	>>> agent = agent["//form"].submit(follow=True)
 
Don't like XPath? Use CSS selectors instead::

	>>> agent = agent.findcss("a").click()
	>>> agent = agent.findcss("a.highlighted").click()
	>>> agent = agent.findcss("a#mylink").click()


Querying WSGI application responses
-----------------------------------

Checking the content of the request
`````````````````````````````````````

::

	>>> assert agent.request.path_info == '/index.html'
	>>> print agent.request.environ
        {...}

``agent.request`` is a `pesto.request.Request <http://pypi.python.org/pypi/pesto>`_
object, and all attributes of that class are available to examine.

{'fraction_non_alphanumeric': 0.2033641460827398, 'fraction_numerical': 0.0006061524473405061, 'mean_word_length': 2.1578947368421053, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 181, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5848713', 'n_tokens_mistral': 1904, 'n_tokens_neox': 1784, 'n_words': 480}