[svn r88] Refactored the body-parsing logic that used to be in read_body() into parsed_body() and added some docs. This means that if you were calling read_body expecting to get anything but a string out, you should change to parsed_body() instead. Refactored tests.py a little bit.
This commit is contained in:
@@ -63,7 +63,8 @@ connection pools keyed on host,databasename"""
|
||||
new_kwargs['db'] = dbname
|
||||
new_kwargs['host'] = host
|
||||
new_kwargs.update(self.credentials_for(host))
|
||||
dbpool = self._conn_pool_class(self._module, self._min_size, self._max_size, *self._args, **new_kwargs)
|
||||
dbpool = self._conn_pool_class(self._module, min_size=self._min_size, max_size=self._max_size,
|
||||
*self._args, **new_kwargs)
|
||||
self._databases[key] = dbpool
|
||||
|
||||
return self._databases[key]
|
||||
|
@@ -337,22 +337,39 @@ class Request(object):
|
||||
return self._cached_body
|
||||
|
||||
def read_body(self):
|
||||
""" Returns the string body that was read off the request, or
|
||||
the empty string if there was no request body.
|
||||
|
||||
Requires a content-length header. Caches the body so multiple
|
||||
calls to read_body() are free.
|
||||
"""
|
||||
if not hasattr(self, '_cached_body'):
|
||||
length = self.get_header('content-length')
|
||||
if length:
|
||||
length = int(length)
|
||||
if length:
|
||||
self._cached_body = self.protocol.rfile.read(length)
|
||||
else:
|
||||
self._cached_body = ''
|
||||
return self._cached_body
|
||||
|
||||
def parsed_body(self):
|
||||
""" Returns the parsed version of the body, using the
|
||||
content-type header to select from the parsers on the site
|
||||
object.
|
||||
|
||||
If no parser is found, returns the string body from
|
||||
read_body(). Caches the parsed body so multiple calls to
|
||||
parsed_body() are free.
|
||||
"""
|
||||
if not hasattr(self, '_cached_parsed_body'):
|
||||
if not hasattr(self, '_cached_body'):
|
||||
length = self.get_header('content-length')
|
||||
if length:
|
||||
length = int(length)
|
||||
if length:
|
||||
self._cached_body = self.protocol.rfile.read(length)
|
||||
else:
|
||||
self._cached_body = ''
|
||||
body = self._cached_body
|
||||
body = self.read_body()
|
||||
if hasattr(self.site, 'parsers'):
|
||||
parser = self.site.parsers.get(
|
||||
self.get_header('content-type'))
|
||||
if parser is not None:
|
||||
body = parser(body)
|
||||
self._cached_parsed_body = body
|
||||
self._cached_parsed_body = body
|
||||
return self._cached_parsed_body
|
||||
|
||||
def override_body(self, body):
|
||||
|
@@ -23,7 +23,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
"""
|
||||
|
||||
import atexit
|
||||
import errno
|
||||
import os
|
||||
import sys
|
||||
@@ -36,8 +35,8 @@ TestCase = unittest.TestCase
|
||||
name = getattr(sys.modules['__main__'], '__name__', None)
|
||||
main = unittest.main
|
||||
|
||||
# listing of all non-*_test test methods
|
||||
eventlet_test_files = ['coros']
|
||||
# listing of files containing doctests
|
||||
doc_test_files = ['coros']
|
||||
|
||||
def find_command(command):
|
||||
for dir in os.getenv('PATH', '/usr/bin:/usr/sbin').split(os.pathsep):
|
||||
@@ -46,8 +45,8 @@ def find_command(command):
|
||||
return p
|
||||
raise IOError(errno.ENOENT, 'Command not found: %r' % command)
|
||||
|
||||
def run_all_tests(test_files = eventlet_test_files):
|
||||
""" Runs all the unit tests in eventlet, returning immediately after the
|
||||
def run_all_tests(test_files = doc_test_files):
|
||||
""" Runs all the unit tests, returning immediately after the
|
||||
first failed test.
|
||||
|
||||
Returns true if the tests all succeeded. This method is really much longer
|
||||
@@ -81,8 +80,10 @@ def run_all_tests(test_files = eventlet_test_files):
|
||||
failures, tests = doctest.testmod(test_module)
|
||||
if failures:
|
||||
return False
|
||||
else:
|
||||
print "OK"
|
||||
|
||||
return True
|
||||
|
||||
if __name__ == '__main__':
|
||||
run_all_tests()
|
||||
run_all_tests()
|
||||
|
Reference in New Issue
Block a user