.. -*-doctest-*-

Functional and testbrowser testing patches
==========================================

If you include the collective.testcaselayer configure.zcml, you'll get
some monkey patches for some functional testing and testbrowser bugs:

  - bug #98437 - TestBrowser Referer: header set to 'localhost'
    https://bugs.launchpad.net/bugs/98437
  - Streaming data into the response don't work

Add a document which renders the referer.

    >>> folder.addDTMLDocument(
    ...     'index_html', file='''\
    ... <html><body>
    ... <dtml-var "REQUEST['HTTP_REFERER']">
    ... <form action="." method="post" id="post"></form>
    ... <form action="." method="get" id="get"></form>
    ... <a href=".">link</a>
    ... </html></body>
    ... ''')
    ''

Open a browser.

    >>> from Products.Five.testbrowser import Browser
    >>> browser = Browser()
    >>> browser.handleErrors = False

Before patching, fresh requests have an invalid referer.

    >>> browser.open(folder.index_html.absolute_url())
    >>> print browser.contents
    <html><body>
    localhost
    <form action="." method="post" id="post"></form>
    <form action="." method="get" id="get"></form>
    <a href=".">link</a>
    </html></body>


Add a script that streams content to the response.

    >>> from Products.PythonScripts import PythonScript
    >>> PythonScript.manage_addPythonScript(folder, 'foo.txt')
    ''
    >>> folder['foo.txt'].ZPythonScript_edit(params='', body='''\
    ... context.REQUEST.response.setHeader('Content-Type', 'text/plain')
    ... context.REQUEST.response.setHeader(
    ...     'Content-Disposition',
    ...     'attachment;filename=foo.txt')
    ... context.REQUEST.response.write('foo')''')

Before patching, data streamed to the response is not in the browser
contents.

    >>> browser.open(folder['foo.txt'].absolute_url())
    >>> browser.isHtml
    False
    >>> print browser.contents

Apply the patches.

    >>> from Products.Five import zcml
    >>> from Products.Five import fiveconfigure
    >>> from collective import testcaselayer
    >>> fiveconfigure.debug_mode = True
    >>> zcml.load_config('configure.zcml', package=testcaselayer)
    >>> fiveconfigure.debug_mode = False

A fresh request should have no referer.

    >>> browser.open(folder.index_html.absolute_url())
    >>> print browser.contents
    <html><body>
    <form action="." method="post" id="post"></form>
    <form action="." method="get" id="get"></form>
    <a href=".">link</a>
    </html></body>

Submitting a form via post should have no referer.

    >>> browser.getForm('post').submit()
    >>> print browser.contents
    <html><body>
    <form action="." method="post" id="post"></form>
    <form action="." method="get" id="get"></form>
    <a href=".">link</a>
    </html></body>

Submitting a form via get should have no referer.

    >>> browser.getForm('get').submit()
    >>> print browser.contents
    <html><body>
    <form action="." method="post" id="post"></form>
    <form action="." method="get" id="get"></form>
    <a href=".">link</a>
    </html></body>

Clicking a link should set the referer.

    >>> browser.getLink('link').click()
    >>> print browser.contents
    <html><body>
    http://nohost/test_folder_1_/
    <form action="." method="post" id="post"></form>
    <form action="." method="get" id="get"></form>
    <a href=".">link</a>
    </html></body>

Data streamed to the response is now in the browser contents.

    >>> browser.open(folder['foo.txt'].absolute_url())
    >>> browser.isHtml
    False
    >>> print browser.contents
    Status: 200 OK
    X-Powered-By: Zope (www.zope.org), Python (www.python.org)
    Content-Length: 0
    Content-Type: text/plain
    Content-Disposition: attachment;filename=foo.txt
    foo
