DocTest Functional Tests
========================

This file documents and tests doctest-based functional tests and basic
Zope web-application functionality.

Request/Response Functional Tests
---------------------------------

You can create Functional tests as doctests.  Typically, this is done
by using a script such as src/zope/app/tests/dochttp.py to convert
tcpwatch recorded output to a doctest, which is then edited to provide
explanation and to remove uninyeresting details.  That is how this
file was created.

Here we'll test some of the most basic types of access.

First, we'll test accessing a protected page without credentials:

  >>> print http(r"""
  ... GET /@@contents.html HTTP/1.1
  ... """)
  HTTP/1.1 401 Unauthorized
  Content-Length: ...
  Content-Type: text/html;charset=utf-8
  Www-Authenticate: basic realm=zope
  <BLANKLINE>
  <!DOCTYPE html PUBLIC ...

Here we see that we got:

  - A 404 response,
  - A Www-Authenticate header, and
  - An html body with an error message

Note that we used ellipeses to indicate ininteresting details.

Next, we'll access the same page with credentials: 

  >>> print http(r"""
  ... GET /@@contents.html HTTP/1.1
  ... Authorization: Basic bWdyOm1ncnB3
  ... """)
  HTTP/1.1 200 Ok
  Content-Length: ...
  Content-Type: text/html;charset=utf-8
  <BLANKLINE>
  <!DOCTYPE html PUBLIC ...

Important note: you must use the user named "mgr" with a password
"mgrpw". This means that when you record requests with tcpwatch, you
need to use that user login name and password.

And we get a normal output.

Next we'll try accessing site management. Since we used "/manage", 
we got redirected:

  >>> print http(r"""
  ... GET /++etc++site/@@manage HTTP/1.1
  ... Authorization: Basic bWdyOm1ncnB3
  ... Referer: http://localhost:8081/
  ... """)
  HTTP/1.1 303 See Other
  Content-Length: 0
  Content-Type: text/plain;charset=utf-8
  Location: @@tasks.html
  <BLANKLINE>

Note that, in this case, we got a 303 response.  A 303 response is the
prefered response for this sort of redirect with HTTP 1.1.  If we used
HTTP 1.0, we'd get a 302 response:

  >>> print http(r"""
  ... GET /++etc++site/@@manage HTTP/1.0
  ... Authorization: Basic bWdyOm1ncnB3
  ... Referer: http://localhost:8081/
  ... """)
  HTTP/1.0 302 Moved Temporarily
  Content-Length: 0
  Content-Type: text/plain;charset=utf-8
  Location: @@tasks.html
  <BLANKLINE>

Lets visit the page we were rediected to:

  >>> print http(r"""
  ... GET /++etc++site/@@tasks.html HTTP/1.1
  ... Authorization: Basic bWdyOm1ncnB3
  ... Referer: http://localhost:8081/
  ... """)
  HTTP/1.1 200 Ok
  Content-Length: ...
  Content-Type: text/html;charset=utf-8
  <BLANKLINE>
  <!DOCTYPE html PUBLIC ...

Finally, lets access the default page for the site:

  >>> print http(r"""
  ... GET / HTTP/1.1
  ... Authorization: Basic bWdyOm1ncnB3
  ... """)
  HTTP/1.1 200 Ok
  Content-Length: ...
  Content-Type: text/html;charset=utf-8
  <BLANKLINE>
  <!DOCTYPE html PUBLIC ...

Access to the object system
---------------------------

You can use the `getRootFolder()` function:

  >>> root = getRootFolder()
  >>> root
  <zope.app.folder.folder.Folder object at ...>

You can intermix HTTP requests with regular Python calls.  Note,
however, that making an `http()` call implied a transaction commit.
If you want to throw away changes made in Python code, abort the
transaction before the HTTP request.

  >>> print http(r"""
  ... POST /@@contents.html HTTP/1.1
  ... Authorization: Basic bWdyOm1ncnB3
  ... Content-Length: 73
  ... Content-Type: application/x-www-form-urlencoded
  ... 
  ... type_name=zope.app.browser.add.zope.app.folder.folder.Folder&new_value=f1""")
  HTTP/1.1 303 See Other
  Content-Length: ...
  Content-Type: text/html;charset=utf-8
  Location: http://localhost/@@contents.html
  <BLANKLINE>
  <!DOCTYPE html ...

Now we can see that the new folder was added:

  >>> list(root.keys())
  [u'f1']

