diff options
Diffstat (limited to 'src/mailman/tests/test_documentation.py')
| -rw-r--r-- | src/mailman/tests/test_documentation.py | 75 |
1 files changed, 64 insertions, 11 deletions
diff --git a/src/mailman/tests/test_documentation.py b/src/mailman/tests/test_documentation.py index d03762188..e65885d79 100644 --- a/src/mailman/tests/test_documentation.py +++ b/src/mailman/tests/test_documentation.py @@ -1,4 +1,4 @@ -# Copyright (C) 2007-2010 by the Free Software Foundation, Inc. +# Copyright (C) 2007-2011 by the Free Software Foundation, Inc. # # This file is part of GNU Mailman. # @@ -35,6 +35,7 @@ import json import doctest import unittest +from base64 import b64encode from email import message_from_string from httplib2 import Http from urllib import urlencode @@ -98,6 +99,9 @@ def stop(): def dump_msgdata(msgdata, *additional_skips): """Dump in a more readable way a message metadata dictionary.""" + if len(msgdata) == 0: + print '*Empty*' + return skips = set(additional_skips) # Some stuff we always want to skip, because their values will always be # variable data. @@ -109,8 +113,22 @@ def dump_msgdata(msgdata, *additional_skips): print '{0:{2}}: {1}'.format(key, msgdata[key], longest) -def dump_json(url, data=None, method=None): - """Print the JSON dictionary read from a URL. +def dump_list(list_of_things, key=str): + """Print items in a string to get rid of stupid u'' prefixes.""" + # List of things may be a generator. + list_of_things = list(list_of_things) + if len(list_of_things) == 0: + print '*Empty*' + if key is not None: + list_of_things = sorted(list_of_things, key=key) + for item in list_of_things: + print item + + +def call_http(url, data=None, method=None, username=None, password=None): + """'Call' a URL with a given HTTP method and return the resulting object. + + The object will have been JSON decoded. :param url: The url to open, read, and print. :type url: string @@ -118,36 +136,69 @@ def dump_json(url, data=None, method=None): :type data: dict :param method: Alternative HTTP method to use. :type method: str + :param username: The HTTP Basic Auth user name. None means use the value + from the configuration. + :type username: str + :param password: The HTTP Basic Auth password. None means use the value + from the configuration. + :type username: str """ - if data is not None: - data = urlencode(data) headers = {} + if data is not None: + data = urlencode(data, doseq=True) + headers['Content-Type'] = 'application/x-www-form-urlencoded' if method is None: if data is None: method = 'GET' else: method = 'POST' - headers['Content-Type'] = 'application/x-www-form-urlencoded' method = method.upper() + basic_auth = '{0}:{1}'.format( + (config.webservice.admin_user if username is None else username), + (config.webservice.admin_pass if password is None else password)) + headers['Authorization'] = 'Basic ' + b64encode(basic_auth) response, content = Http().request(url, method, data, headers) # If we did not get a 2xx status code, make this look like a urllib2 # exception, for backward compatibility with existing doctests. if response.status // 100 != 2: - raise HTTPError(url, response.status, response.reason, response, None) + raise HTTPError(url, response.status, content, response, None) if len(content) == 0: for header in sorted(response): print '{0}: {1}'.format(header, response[header]) + return None + # XXX Workaround http://bugs.python.org/issue10038 + content = unicode(content) + return json.loads(content) + + +def dump_json(url, data=None, method=None, username=None, password=None): + """Print the JSON dictionary read from a URL. + + :param url: The url to open, read, and print. + :type url: string + :param data: Data to use to POST to a URL. + :type data: dict + :param method: Alternative HTTP method to use. + :type method: str + :param username: The HTTP Basic Auth user name. None means use the value + from the configuration. + :type username: str + :param password: The HTTP Basic Auth password. None means use the value + from the configuration. + :type username: str + """ + results = call_http(url, data, method, username, password) + if results is None: return - data = json.loads(content) - for key in sorted(data): + for key in sorted(results): if key == 'entries': - for i, entry in enumerate(data[key]): + for i, entry in enumerate(results[key]): # entry is a dictionary. print 'entry %d:' % i for entry_key in sorted(entry): print ' {0}: {1}'.format(entry_key, entry[entry_key]) else: - print '{0}: {1}'.format(key, data[key]) + print '{0}: {1}'.format(key, results[key]) @@ -161,10 +212,12 @@ def setup(testobj): # doctests should do the imports themselves. It makes for better # documentation that way. However, a few are really useful, or help to # hide some icky test implementation details. + testobj.globs['call_http'] = call_http testobj.globs['config'] = config testobj.globs['create_list'] = create_list testobj.globs['dump_json'] = dump_json testobj.globs['dump_msgdata'] = dump_msgdata + testobj.globs['dump_list'] = dump_list testobj.globs['message_from_string'] = specialized_message_from_string testobj.globs['smtpd'] = SMTPLayer.smtpd testobj.globs['stop'] = stop |
