Back to index

moin  1.9.0~rc2
Classes | Functions
MoinMoin.support.werkzeug.test Namespace Reference

Classes

class  _TestCookieHeaders
class  _TestCookieResponse
class  _TestCookieJar
class  EnvironBuilder
class  Client

Functions

def stream_encode_multipart
def encode_multipart
def File
def _iter_data
def create_environ
def run_wsgi_app

Function Documentation

Iterates over a dict or multidict yielding all keys and values.
This is used to iterate over the data passed to the
:class:`EnvironBuilder`.

Definition at line 167 of file test.py.

00167 
00168 def _iter_data(data):
00169     """Iterates over a dict or multidict yielding all keys and values.
00170     This is used to iterate over the data passed to the
00171     :class:`EnvironBuilder`.
00172     """
00173     if isinstance(data, MultiDict):
00174         for key, values in data.iterlists():
00175             for value in values:
00176                 yield key, value
00177     else:
00178         for item in data.iteritems():
00179             yield item
00180 

def MoinMoin.support.werkzeug.test.create_environ (   args,
  kwargs 
)
Create a new WSGI environ dict based on the values passed.  The first
parameter should be the path of the request which defaults to '/'.  The
second one can either be an absolute path (in that case the host is
localhost:80) or a full path to the request with scheme, netloc port and
the path to the script.

This accepts the same arguments as the :class:`EnvironBuilder`
constructor.

.. versionchanged:: 0.5
   This function is now a thin wrapper over :class:`EnvironBuilder` which
   was added in 0.5.  The `headers`, `environ_base`, `environ_overrides`
   and `charset` parameters were added.

Definition at line 696 of file test.py.

00696 
00697 def create_environ(*args, **kwargs):
00698     """Create a new WSGI environ dict based on the values passed.  The first
00699     parameter should be the path of the request which defaults to '/'.  The
00700     second one can either be an absolute path (in that case the host is
00701     localhost:80) or a full path to the request with scheme, netloc port and
00702     the path to the script.
00703 
00704     This accepts the same arguments as the :class:`EnvironBuilder`
00705     constructor.
00706 
00707     .. versionchanged:: 0.5
00708        This function is now a thin wrapper over :class:`EnvironBuilder` which
00709        was added in 0.5.  The `headers`, `environ_base`, `environ_overrides`
00710        and `charset` parameters were added.
00711     """
00712     builder = EnvironBuilder(*args, **kwargs)
00713     try:
00714         return builder.get_environ()
00715     finally:
00716         builder.close()
00717 

Here is the caller graph for this function:

def MoinMoin.support.werkzeug.test.encode_multipart (   values,
  boundary = None,
  charset = 'utf-8' 
)
Like `stream_encode_multipart` but returns a tuple in the form
(``boundary``, ``data``) where data is a bytestring.

Definition at line 97 of file test.py.

00097 
00098 def encode_multipart(values, boundary=None, charset='utf-8'):
00099     """Like `stream_encode_multipart` but returns a tuple in the form
00100     (``boundary``, ``data``) where data is a bytestring.
00101     """
00102     stream, length, boundary = stream_encode_multipart(
00103         values, use_tempfile=False, boundary=boundary, charset=charset)
00104     return boundary, stream.read()
00105 

Here is the call graph for this function:

def MoinMoin.support.werkzeug.test.File (   fd,
  filename = None,
  mimetype = None 
)
Backwards compat.

Definition at line 106 of file test.py.

00106 
00107 def File(fd, filename=None, mimetype=None):
00108     """Backwards compat."""
00109     from warnings import warn
00110     warn(DeprecationWarning('werkzeug.test.File is deprecated, use the '
00111                             'EnvironBuilder or FileStorage instead'))
00112     return FileStorage(fd, filename=filename, content_type=mimetype)
00113 

Here is the caller graph for this function:

def MoinMoin.support.werkzeug.test.run_wsgi_app (   app,
  environ,
  buffered = False 
)
Return a tuple in the form (app_iter, status, headers) of the
application output.  This works best if you pass it an application that
returns an iterator all the time.

Sometimes applications may use the `write()` callable returned
by the `start_response` function.  This tries to resolve such edge
cases automatically.  But if you don't get the expected output you
should set `buffered` to `True` which enforces buffering.

If passed an invalid WSGI application the behavior of this function is
undefined.  Never pass non-conforming WSGI applications to this function.

:param app: the application to execute.
:param buffered: set to `True` to enforce buffering.
:return: tuple in the form ``(app_iter, status, headers)``

Definition at line 718 of file test.py.

00718 
00719 def run_wsgi_app(app, environ, buffered=False):
00720     """Return a tuple in the form (app_iter, status, headers) of the
00721     application output.  This works best if you pass it an application that
00722     returns an iterator all the time.
00723 
00724     Sometimes applications may use the `write()` callable returned
00725     by the `start_response` function.  This tries to resolve such edge
00726     cases automatically.  But if you don't get the expected output you
00727     should set `buffered` to `True` which enforces buffering.
00728 
00729     If passed an invalid WSGI application the behavior of this function is
00730     undefined.  Never pass non-conforming WSGI applications to this function.
00731 
00732     :param app: the application to execute.
00733     :param buffered: set to `True` to enforce buffering.
00734     :return: tuple in the form ``(app_iter, status, headers)``
00735     """
00736     response = []
00737     buffer = []
00738 
00739     def start_response(status, headers, exc_info=None):
00740         if exc_info is not None:
00741             raise exc_info[0], exc_info[1], exc_info[2]
00742         response[:] = [status, headers]
00743         return buffer.append
00744 
00745     app_iter = app(environ, start_response)
00746 
00747     # when buffering we emit the close call early and conver the
00748     # application iterator into a regular list
00749     if buffered:
00750         close_func = getattr(app_iter, 'close', None)
00751         try:
00752             app_iter = list(app_iter)
00753         finally:
00754             if close_func is not None:
00755                 close_func()
00756 
00757     # otherwise we iterate the application iter until we have
00758     # a response, chain the already received data with the already
00759     # collected data and wrap it in a new `ClosingIterator` if
00760     # we have a close callable.
00761     else:
00762         while not response:
00763             buffer.append(app_iter.next())
00764         if buffer:
00765             app_iter = chain(buffer, app_iter)
00766             close_func = getattr(app_iter, 'close', None)
00767             if close_func is not None:
00768                 app_iter = ClosingIterator(app_iter, close_func)
00769 
00770     return app_iter, response[0], response[1]

Here is the caller graph for this function:

def MoinMoin.support.werkzeug.test.stream_encode_multipart (   values,
  use_tempfile = True,
  threshold = 1024 * 500,
  boundary = None,
  charset = 'utf-8' 
)
Encode a dict of values (either strings or file descriptors or
:class:`FileStorage` objects.) into a multipart encoded string stored
in a file descriptor.

Definition at line 31 of file test.py.

00031 
00032                             boundary=None, charset='utf-8'):
00033     """Encode a dict of values (either strings or file descriptors or
00034     :class:`FileStorage` objects.) into a multipart encoded string stored
00035     in a file descriptor.
00036     """
00037     if boundary is None:
00038         boundary = '---------------WerkzeugFormPart_%s%s' % (time(), random())
00039     _closure = [StringIO(), 0, False]
00040 
00041     if use_tempfile:
00042         def write(string):
00043             stream, total_length, on_disk = _closure
00044             if on_disk:
00045                 stream.write(string)
00046             else:
00047                 length = len(string)
00048                 if length + _closure[1] <= threshold:
00049                     stream.write(string)
00050                 else:
00051                     new_stream = TemporaryFile('wb+')
00052                     new_stream.write(stream.getvalue())
00053                     new_stream.write(string)
00054                     _closure[0] = new_stream
00055                     _closure[2] = True
00056                 _closure[1] = total_length + length
00057     else:
00058         write = _closure[0].write
00059 
00060     if not isinstance(values, MultiDict):
00061         values = MultiDict(values)
00062 
00063     for key, values in values.iterlists():
00064         for value in values:
00065             write('--%s\r\nContent-Disposition: form-data; name="%s"' %
00066                   (boundary, key))
00067             reader = getattr(value, 'read', None)
00068             if reader is not None:
00069                 filename = getattr(value, 'filename',
00070                                    getattr(value, 'name', None))
00071                 content_type = getattr(value, 'content_type', None)
00072                 if content_type is None:
00073                     content_type = filename and \
00074                         mimetypes.guess_type(filename)[0] or \
00075                         'application/octet-stream'
00076                 if filename is not None:
00077                     write('; filename="%s"\r\n' % filename)
00078                 else:
00079                     write('\r\n')
00080                 write('Content-Type: %s\r\n\r\n' % content_type)
00081                 while 1:
00082                     chunk = reader(16384)
00083                     if not chunk:
00084                         break
00085                     write(chunk)
00086             else:
00087                 if isinstance(value, unicode):
00088                     value = value.encode(charset)
00089                 write('\r\n\r\n' + value)
00090             write('\r\n')
00091     write('--%s--\r\n' % boundary)
00092 
00093     length = int(_closure[0].tell())
00094     _closure[0].seek(0)
00095     return _closure[0], length, boundary
00096 

Here is the caller graph for this function: