Back to index

moin  1.9.0~rc2
Public Member Functions | Public Attributes
MoinMoin.support.werkzeug.test.Client Class Reference

List of all members.

Public Member Functions

def __init__
def open
def get
def post
def head
def put
def delete
def __repr__

Public Attributes

 application
 response_wrapper
 cookie_jar

Detailed Description

This class allows to send requests to a wrapped application.

The response wrapper can be a class or factory function that takes
three arguments: app_iter, status and headers.  The default response
wrapper just returns a tuple.

Example::

    class ClientResponse(BaseResponse):
        ...

    client = Client(MyApplication(), response_wrapper=ClientResponse)

The use_cookies parameter indicates whether cookies should be stored and
sent for subsequent requests. This is True by default, but passing False
will disable this behaviour.

.. versionadded:: 0.5
   `use_cookies` is new in this version.  Older versions did not provide
   builtin cookie support.

Definition at line 551 of file test.py.


Constructor & Destructor Documentation

def MoinMoin.support.werkzeug.test.Client.__init__ (   self,
  application,
  response_wrapper = None,
  use_cookies = True 
)

Definition at line 574 of file test.py.

00574 
00575     def __init__(self, application, response_wrapper=None, use_cookies=True):
00576         self.application = application
00577         if response_wrapper is None:
00578             response_wrapper = lambda a, s, h: (a, s, h)
00579         self.response_wrapper = response_wrapper
00580         if use_cookies:
00581             self.cookie_jar = _TestCookieJar()
00582         else:
00583             self.cookie_jar = None


Member Function Documentation

Definition at line 689 of file test.py.

00689 
00690     def __repr__(self):
00691         return '<%s %r>' % (
00692             self.__class__.__name__,
00693             self.application
00694         )
00695 

def MoinMoin.support.werkzeug.test.Client.delete (   self,
  args,
  kw 
)
Like open but method is enforced to DELETE.

Definition at line 684 of file test.py.

00684 
00685     def delete(self, *args, **kw):
00686         """Like open but method is enforced to DELETE."""
00687         kw['method'] = 'DELETE'
00688         return self.open(*args, **kw)

Here is the call graph for this function:

def MoinMoin.support.werkzeug.test.Client.get (   self,
  args,
  kw 
)
Like open but method is enforced to GET.

Definition at line 664 of file test.py.

00664 
00665     def get(self, *args, **kw):
00666         """Like open but method is enforced to GET."""
00667         kw['method'] = 'GET'
00668         return self.open(*args, **kw)

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.support.werkzeug.test.Client.head (   self,
  args,
  kw 
)
Like open but method is enforced to HEAD.

Definition at line 674 of file test.py.

00674 
00675     def head(self, *args, **kw):
00676         """Like open but method is enforced to HEAD."""
00677         kw['method'] = 'HEAD'
00678         return self.open(*args, **kw)

Here is the call graph for this function:

def MoinMoin.support.werkzeug.test.Client.open (   self,
  args,
  kwargs 
)
Takes the same arguments as the :class:`EnvironBuilder` class with
some additions:  You can provide a :class:`EnvironBuilder` or a WSGI
environment as only argument instead of the :class:`EnvironBuilder`
arguments and two optional keyword arguments (`as_tuple`, `buffered`)
that change the type of the return value or the way the application is
executed.

.. versionchanged:: 0.5
   If a dict is provided as file in the dict for the `data` parameter
   the content type has to be called `content_type` now instead of
   `mimetype`.  This change was made for consistency with
   :class:`werkzeug.FileWrapper`.

    The `follow_redirects` parameter was added to :func:`open`.

Additional parameters:

:param as_tuple: Returns a tuple in the form ``(environ, result)``
:param buffered: Set this to true to buffer the application run.
         This will automatically close the application for
         you as well.
:param follow_redirects: Set this to True if the `Client` should
                 follow HTTP redirects.

Definition at line 584 of file test.py.

00584 
00585     def open(self, *args, **kwargs):
00586         """Takes the same arguments as the :class:`EnvironBuilder` class with
00587         some additions:  You can provide a :class:`EnvironBuilder` or a WSGI
00588         environment as only argument instead of the :class:`EnvironBuilder`
00589         arguments and two optional keyword arguments (`as_tuple`, `buffered`)
00590         that change the type of the return value or the way the application is
00591         executed.
00592 
00593         .. versionchanged:: 0.5
00594            If a dict is provided as file in the dict for the `data` parameter
00595            the content type has to be called `content_type` now instead of
00596            `mimetype`.  This change was made for consistency with
00597            :class:`werkzeug.FileWrapper`.
00598 
00599             The `follow_redirects` parameter was added to :func:`open`.
00600 
00601         Additional parameters:
00602 
00603         :param as_tuple: Returns a tuple in the form ``(environ, result)``
00604         :param buffered: Set this to true to buffer the application run.
00605                          This will automatically close the application for
00606                          you as well.
00607         :param follow_redirects: Set this to True if the `Client` should
00608                                  follow HTTP redirects.
00609         """
00610         as_tuple = kwargs.pop('as_tuple', False)
00611         buffered = kwargs.pop('buffered', False)
00612         follow_redirects = kwargs.pop('follow_redirects', False)
00613         environ = None
00614         if not kwargs and len(args) == 1:
00615             if isinstance(args[0], EnvironBuilder):
00616                 environ = args[0].get_environ()
00617             elif isinstance(args[0], dict):
00618                 environ = args[0]
00619         if environ is None:
00620             builder = EnvironBuilder(*args, **kwargs)
00621             try:
00622                 environ = builder.get_environ()
00623             finally:
00624                 builder.close()
00625 
00626         if self.cookie_jar is not None:
00627             self.cookie_jar.inject_wsgi(environ)
00628         rv = run_wsgi_app(self.application, environ, buffered=buffered)
00629         if self.cookie_jar is not None:
00630             self.cookie_jar.extract_wsgi(environ, rv[2])
00631 
00632         # handle redirects
00633         redirect_chain = []
00634         status_code = int(rv[1].split(None, 1)[0])
00635         while status_code in (301, 302, 303, 305, 307) and follow_redirects:
00636             redirect = dict(rv[2])['Location']
00637             host = get_host(create_environ('/', redirect))
00638             if get_host(environ).split(':', 1)[0] != host:
00639                 raise RuntimeError('%r does not support redirect to '
00640                                    'external targets' % self.__class__)
00641 
00642             scheme, netloc, script_root, qs, anchor = urlparse.urlsplit(redirect)
00643             redirect_chain.append((redirect, status_code))
00644 
00645             kwargs.update({
00646                 'base_url':         urlparse.urlunsplit((scheme, host,
00647                                     script_root, '', '')).rstrip('/') + '/',
00648                 'query_string':     qs,
00649                 'as_tuple':         as_tuple,
00650                 'buffered':         buffered,
00651                 'follow_redirects': False
00652             })
00653             rv = self.open(*args, **kwargs)
00654             status_code = int(rv[1].split(None, 1)[0])
00655 
00656             # Prevent loops
00657             if redirect_chain[-1] in redirect_chain[0:-1]:
00658                 break
00659 
00660         response = self.response_wrapper(*rv)
00661         if as_tuple:
00662             return environ, response
00663         return response

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.support.werkzeug.test.Client.post (   self,
  args,
  kw 
)
Like open but method is enforced to POST.

Definition at line 669 of file test.py.

00669 
00670     def post(self, *args, **kw):
00671         """Like open but method is enforced to POST."""
00672         kw['method'] = 'POST'
00673         return self.open(*args, **kw)

Here is the call graph for this function:

def MoinMoin.support.werkzeug.test.Client.put (   self,
  args,
  kw 
)
Like open but method is enforced to PUT.

Definition at line 679 of file test.py.

00679 
00680     def put(self, *args, **kw):
00681         """Like open but method is enforced to PUT."""
00682         kw['method'] = 'PUT'
00683         return self.open(*args, **kw)

Here is the call graph for this function:


Member Data Documentation

Definition at line 575 of file test.py.

Definition at line 580 of file test.py.

Definition at line 578 of file test.py.


The documentation for this class was generated from the following file: