desc
stringlengths
3
26.7k
decl
stringlengths
11
7.89k
bodies
stringlengths
8
553k
'Return kwargs for a HTTP request. :rtype: dict'
@property def request_kwargs(self):
kwargs = {'url': self.stats_url} if (self.config.get('scheme') == 'https'): kwargs['verify'] = self.config.get('verify_ssl_cert', False) if (('username' in self.config) and ('password' in self.config)): kwargs['auth'] = (self.config['username'], self.config['password']) LOGGER.debug('Req...
'Fetch the data from the stats URL :rtype: dict'
def fetch_data(self):
data = super(CSVStatsPlugin, self).fetch_data() if (not data): return dict() temp = tempfile.TemporaryFile() temp.write(data) temp.seek(0) reader = csv.DictReader(temp) data = list() for row in reader: data.append(row) temp.close() return data
'Poll HTTP JSON endpoint for stats data'
def poll(self):
self.initialize() data = self.fetch_data() if data: self.add_datapoints(data) self.finish()
'Fetch the data from the stats URL :rtype: dict'
def fetch_data(self):
data = self.http_get() try: return (data.json() if data else {}) except Exception as error: LOGGER.error('JSON decoding error: %r', error) return {}
'Poll HTTP JSON endpoint for stats data'
def poll(self):
self.initialize() data = self.fetch_data() if data: self.add_datapoints(data) self.finish()
'Create a DSN to connect to :return str: The DSN to connect'
@property def dsn(self):
dsn = ("host='%(host)s' port=%(port)i dbname='pgbouncer' user='%(user)s'" % self.config) if self.config.get('password'): dsn += (" password='%s'" % self.config['password']) return dsn
'Return the summed data as a dict :rtype: dict'
def sum_data(self, stats):
data = {'Queue': {'Current': 0, 'Max': 0}, 'Sessions': {'Current': 0, 'Max': 0, 'Total': 0}, 'Bytes': {'In': 0, 'Out': 0}, 'Denied': {'Request': 0, 'Response': 0}, 'Errors': {'Request': 0, 'Response': 0, 'Connections': 0}, 'Warnings': {'Retry': 0, 'Redispatch': 0}, 'Server': {'Downtime': 0}} for row in stats: ...
'Add all of the data points for a node :param list stats: The parsed csv content'
def add_datapoints(self, stats):
if (not stats): return stats = self.sum_data(stats) for section in [key for key in stats.keys() if (key != 'server')]: for key in stats[section].keys(): self.add_derive_value(('%s/%s' % (section, key)), self.UNIT.get(section, dict()).get(key, ''), stats[section][key]) self.ad...
'Connect to PostgreSQL, returning the connection object. :rtype: psycopg2.connection'
def connect(self):
conn = psycopg2.connect(**self.connection_arguments) conn.set_isolation_level(extensions.ISOLATION_LEVEL_AUTOCOMMIT) return conn
'Create connection parameter dictionary for psycopg2.connect :return dict: The dictionary to be passed to psycopg2.connect via double-splat'
@property def connection_arguments(self):
filtered_args = ['name', 'superuser', 'relation_stats'] args = {} for key in (set(self.config) - set(filtered_args)): if (key == 'dbname'): args['database'] = self.config[key] else: args[key] = self.config[key] return args
'Return connection server version in PEP 369 format :returns: tuple'
@property def server_version(self):
return (((self.connection.server_version % 1000000) / 10000), ((self.connection.server_version % 10000) / 100), (self.connection.server_version % 100))
'Add all of the data points for a node :param str stats: The stub stats content'
def add_datapoints(self, stats):
if (not stats): return matches = PATTERN.match(stats) if matches: for key in self.KEYS.keys(): try: value = int((matches.group(key) or 0)) except (IndexError, ValueError): value = 0 if (key in self.GAUGES): s...
'Add all of the data points for a node :param list node_data: all of the nodes :param list queue_data: all of the queues :param list channel_data: all of the channels'
def add_node_datapoints(self, node_data, queue_data, channel_data):
channels = 0 for node in node_data: name = node['name'].split('@')[(-1)] self.add_node_channel_datapoints(name, channel_data) self.add_node_message_datapoints(name, queue_data, channel_data) self.add_node_queue_datapoints(name, queue_data) count = 0 for channel in...
'Add datapoints for a node, creating summary values for top-level queue consumer counts and message counts. :param str node: The node name :param list channel_data: The full stack of queue metrics'
def add_node_channel_datapoints(self, node, channel_data):
channel_flow_blocked = 0 for channel in channel_data: if (channel['node'].split('@')[(-1)] == node): if channel.get('client_flow_blocked'): channel_flow_blocked += 1 self.add_gauge_value(('Node/%s/Channels/Blocked' % node), 'channels', channel_flow_blocked)
'Add message stats for the node :param str node: The node name :param list queue_data: all of the queues :param list channel_data: all of the channels'
def add_node_message_datapoints(self, node, queue_data, channel_data):
base_name = ('Node/%s/Messages' % node) keys = self.DUMMY_STATS.keys() (count, total, min_val, max_val, values) = self.initialize_counters(keys) for channel in channel_data: if (channel['node'].split('@')[(-1)] == node): for key in keys: total[key] += channel.get(key,...
'Add datapoints for a node, creating summary values for top-level queue consumer counts and message counts. :param str node: The node name :param list queue_data: The full stack of queue metrics'
def add_node_queue_datapoints(self, node, queue_data):
keys = ['consumers', 'active_consumers', 'idle_consumers'] (count, total, min_val, max_val, values) = self.initialize_counters(keys) del keys[2] for queue in queue_data: if (queue['node'].split('@')[(-1)] == node): for key in keys: count[key] += 1 valu...
'Checks whether the data for a vhost queue should be tracked or not The check is based on the user configs, no configs means track everything :param str vhost_name: the virtual host name :param str queue_name: the queue name'
def track_vhost_queue(self, vhost_name, queue_name):
TRACK_EVERYTHING = dict() tracked_vhosts = self.config.get('vhosts', TRACK_EVERYTHING) vhost_settings = (tracked_vhosts.get(vhost_name) or {}) vhost_queues = vhost_settings.get('queues', []) if (tracked_vhosts is TRACK_EVERYTHING): return True if ((vhost_name in tracked_vhosts) and (vhos...
'Add per-queue datapoints to the processing stack. :param list queue_data: The raw queue data list'
def add_queue_datapoints(self, queue_data):
count = 0 (available, consumers, deliver, publish, redeliver, unacked) = (0, 0, 0, 0, 0, 0) for (count, queue) in enumerate(queue_data): if (queue['name'][0:7] == 'amq.gen'): LOGGER.debug('Skipping auto-named queue: %s', queue['name']) continue message_stats ...
'Make a HTTP request for the URL. :param str url: The URL to request :param dict params: Get query string parameters'
def http_get(self, url, params=None):
kwargs = {'url': url, 'auth': (self.config.get('username', self.DEFAULT_USER), self.config.get('password', self.DEFAULT_PASSWORD)), 'verify': self.config.get('verify_ssl_cert', True)} if params: kwargs['params'] = params try: return self.requests_session.get(**kwargs) except requests.Con...
'Fetch the data from the RabbitMQ server for the specified data type :param str data_type: The type of data to query :param list columns: Ask for specific columns :rtype: list'
def fetch_data(self, data_type, columns=None):
url = ('%s/%s' % (self.rabbitmq_base_url, data_type)) params = ({'columns': ','.join(columns)} if columns else {}) response = self.http_get(url, params) if ((not response) or (response.status_code != 200)): if response: LOGGER.error('Error response from %s (%s): %s', u...
'Return the channel data from the RabbitMQ server :rtype: list'
def fetch_channel_data(self):
return self.fetch_data('channels')
'Return the node data from the RabbitMQ server :rtype: list'
def fetch_node_data(self):
return self.fetch_data('nodes')
'Return the queue data from the RabbitMQ server :rtype: list'
def fetch_queue_data(self):
return self.fetch_data('queues')
'Poll the RabbitMQ server'
def poll(self):
LOGGER.info('Polling RabbitMQ via %s', self.rabbitmq_base_url) start_time = time.time() self.requests_session = requests.Session() self.derive = dict() self.gauge = dict() self.rate = dict() self.consumers = 0 channel_data = self.fetch_channel_data() node_data = self.fetch_n...
'Return the fully composed RabbitMQ base URL :rtype: str'
@property def rabbitmq_base_url(self):
port = self.config.get('port', self.DEFAULT_PORT) secure = self.config.get('secure', False) host = self.config.get('host', self.DEFAULT_HOST) api_path = self.config.get('api_path', self.DEFAULT_API_PATH) scheme = ('https' if secure else 'http') return '{scheme}://{host}:{port}{api_path}'.format(...
'Add all of the data points for a node :param dict stats: The stats content from APC as a string'
def add_datapoints(self, stats):
shared_memory = stats.get('shared_memory', dict()) self.add_gauge_value('Shared Memory/Available', 'bytes', shared_memory.get('avail_mem', 0)) self.add_gauge_value('Shared Memory/Segment Size', 'bytes', shared_memory.get('seg_size', 0)) self.add_gauge_value('Shared Memory/Segment Count', ...
'Add all of the data points for a node :param dict stats: all of the nodes'
def add_datapoints(self, stats):
LOGGER.debug('Stats: %r', stats) self.add_database_stats(stats['couchdb']) self.add_request_methods(stats['httpd_request_methods']) self.add_request_stats(stats['couchdb'], stats['httpd']) self.add_response_code_stats(stats['httpd_status_codes'])
'Add all of the data points for a node :param dict stats: all of the nodes'
def add_datapoints(self, stats):
self.add_gauge_value('Delays/Convergence', 'us', stats.get('converge_delay_total', 0), min_val=stats.get('converge_delay_min', 0), max_val=stats.get('converge_delay_max', 0)) self.add_gauge_value('Delays/Rebalance', 'us', stats.get('rebalance_delay_total', 0), min_val=stats.get('rebalance_delay_min', 0), max_va...
'Add all of the data points for a node :param dict stats: all of the nodes'
def add_datapoints(self, stats):
self.command_value('CAS', 'cas', stats) self.add_derive_value('Command/Requests/Flush', 'flush', stats['cmd_flush']) self.add_derive_value('Command/Errors/CAS', 'errors', stats['cas_badval']) self.command_value('Decr', 'decr', stats) self.command_value('Delete', 'delete', stats) self.command_val...
'Process commands adding the command and the hit ratio. :param str name: The command name :param str prefix: The command prefix :param dict stats: The request stats'
def command_value(self, name, prefix, stats):
total = (stats[('%s_hits' % prefix)] + stats[('%s_misses' % prefix)]) if (total > 0): ratio = ((float(stats[('%s_hits' % prefix)]) / float(total)) * 100) else: ratio = 0 self.add_derive_value(('Command/Requests/%s' % name), 'requests', total) self.add_gauge_value(('Command/Hit Rat...
'Loop in and read in all the data until we have received it all. :param socket connection: The connection'
def fetch_data(self, connection):
connection.send('stats\n') data = super(Memcached, self).fetch_data(connection) data_in = [] for line in data.replace('\r', '').split('\n'): if (line == 'END'): return self.process_data(data_in) data_in.append(line.strip()) return None
'Loop through all the rows and parse each line, looking to see if it is in the data points we would like to process, adding the key => value pair to values if it is. :param list data: The list of rows :returns: dict'
def process_data(self, data):
values = dict() for row in data: parts = row.split(' ') if (parts[1] in self.KEYS): try: values[parts[1]] = int(parts[2]) except ValueError: try: values[parts[1]] = float(parts[2]) except ValueError: ...
'Add all of the data points for a database :param str name: The name of the database for the stats :param dict stats: The stats data to add'
def add_datapoints(self, name, stats):
base_key = ('Database/%s' % name) self.add_gauge_value(('%s/Extents' % base_key), 'extents', stats.get('extents', 0)) self.add_gauge_value(('%s/Size' % base_key), 'bytes', (stats.get('dataSize', 0) / 1048576)) self.add_gauge_value(('%s/File Size' % base_key), 'bytes', (stats.get('fileSize', 0) / 1048...
'Add all of the data points for a server :param dict stats: The stats data to add'
def add_server_datapoints(self, stats):
asserts = stats.get('asserts', dict()) self.add_derive_value('Asserts/Regular', 'asserts', asserts.get('regular', 0)) self.add_derive_value('Asserts/Warning', 'asserts', asserts.get('warning', 0)) self.add_derive_value('Asserts/Message', 'asserts', asserts.get('msg', 0)) self.add_derive_value('Asser...
'Fetch the data from the MongoDB server and add the datapoints'
def get_and_add_db_stats(self):
databases = self.config.get('databases', list()) if isinstance(databases, list): self.get_and_add_db_list(databases) else: self.get_and_add_db_dict(databases)
'Handle the list of databases while supporting authentication for the admin if needed :param list databases: The database list'
def get_and_add_db_list(self, databases):
LOGGER.debug('Processing list of mongo databases') client = self.connect() if (not client): return for database in databases: LOGGER.debug('Collecting stats for %s', database) db = client[database] try: self.add_datapoints(database, db.com...
'Handle the nested database structure with username and password. :param dict databases: The databases data structure'
def get_and_add_db_dict(self, databases):
LOGGER.debug('Processing dict of mongo databases') client = self.connect() if (not client): return db_names = databases.keys() for database in db_names: db = client[database] try: if ('username' in databases[database]): db.authenticate(...
'Add all of the data points for a node :param dict stats: all of the nodes'
def add_datapoints(self, stats):
self.add_gauge_value('Listen Queue Size', 'connections', stats.get('listen_queue', 0)) self.add_gauge_value('Listen Queue Errors', 'errors', stats.get('listen_queue_errors', 0)) for lock in stats.get('locks', list()): lock_name = lock.keys()[0] self.add_gauge_value(('Locks/%s' % ...
'Read the data from the socket :param socket connection: The connection :return: dict'
def fetch_data(self, connection):
data = super(uWSGI, self).fetch_data(connection, read_till_empty=True) if data: data = re.sub('"HTTP_COOKIE=[^"]*"', '""', data) return json.loads(data) return {}
'Add all of the datapoints for the Elasticsearch poll :param dict stats: The stats to process for the values'
def add_datapoints(self, stats):
totals = dict() for node in stats.get('nodes'): for key in stats['nodes'][node].keys(): if isinstance(stats['nodes'][node][key], dict): if (key not in totals): totals[key] = dict() self.process_tree(totals[key], stats['nodes'][node][key]) ...
'Add stats that go under Component/Cluster'
def add_cluster_stats(self):
url = self.stats_url.replace(self.DEFAULT_PATH, '/_cluster/health') response = self.http_get(url) if (response.status_code == 200): data = response.json() self.add_gauge_value('Cluster/Status', 'level', self.STATUS_CODE[data.get('status', 'red')]) self.add_gauge_value('Cluster/Nodes'...
'Add the data points for Component/Indices :param dict stats: The stats to process for the values'
def add_index_datapoints(self, stats):
indices = stats.get('indices', dict()) docs = indices.get('docs', dict()) self.add_gauge_value('Indices/Documents/Count', 'docs', docs.get('count', 0)) self.add_derive_value('Indices/Documents/Added', 'docs', docs.get('count', 0)) self.add_derive_value('Indices/Documents/Deleted', 'docs', docs.get('...
'Add the data points for Component/Network :param dict stats: The stats to process for the values'
def add_network_datapoints(self, stats):
transport = stats.get('transport', dict()) self.add_derive_value('Network/Traffic/Received', 'bytes', transport.get('rx_size_in_bytes', 0)) self.add_derive_value('Network/Traffic/Sent', 'bytes', transport.get('tx_size_in_bytes', 0)) network = stats.get('network', dict()) self.add_derive_value('Netwo...
'Recursively combine all node stats into a single top-level value :param dict tree: The output values :param dict values: The input values'
def process_tree(self, tree, values):
for key in values: if (key == 'timestamp'): continue if isinstance(values[key], dict): if (key not in tree): tree[key] = dict() self.process_tree(tree[key], values[key]) elif isinstance(values[key], int): if (key not in tree): ...
'Add all of the data points for a fpm-pool :param dict stats: Stats from php-fpm for a pool'
def add_datapoints(self, stats):
self.add_derive_value('Connections/Accepted', 'connections', stats.get('accepted conn', 0)) self.add_gauge_value('Connections/Pending', 'connections', stats.get('listen queue', 0), max_val=stats.get('max listen queue', 0)) self.add_gauge_value('Socket Queue', 'connections', stats.get('listen ...
'Fetch the scoreboard from the stats URL :rtype: str'
def get_scoreboard(self, data):
keys = ['_', 'S', 'R', 'W', 'K', 'D', 'C', 'L', 'G', 'I', '.'] values = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] score_out = dict(zip(keys, values)) for line in data.splitlines(): if (line.find('Scoreboard') != (-1)): scoreboard = line.replace('Scoreboard: ', '') for i in ran...
'Add all of the data points for a node :param str stats: The stats content from Apache as a string'
def add_datapoints(self, stats):
matches = PATTERN.findall((stats or '')) for (key, value) in matches: try: value = int(value) except ValueError: try: value = float(value) except ValueError: value = 0 if (key in self.KEYS): if (self.KEYS[key...
'ConfigDict should behaves like a normal dict.'
def test_isadict(self):
(d, m) = (dict(), ConfigDict()) (d['key'], m['key']) = ('value', 'value') (d['k2'], m['k2']) = ('v1', 'v1') (d['k2'], m['k2']) = ('v2', 'v2') self.assertEqual(d.keys(), m.keys()) self.assertEqual(list(d.values()), list(m.values())) self.assertEqual(d.get('key'), m.get('key')) self.assert...
'DateParser: RFC 1123 format'
def test_rfc1123(self):
ts = time.time() rs = time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime(ts)) self.assertEqual(int(ts), int(parse_date(rs)))
'DateParser: RFC 850 format'
def test_rfc850(self):
ts = time.time() rs = time.strftime('%A, %d-%b-%y %H:%M:%S GMT', time.gmtime(ts)) self.assertEqual(int(ts), int(parse_date(rs)))
'DateParser: asctime format'
def test_asctime(self):
ts = time.time() rs = time.strftime('%a %b %d %H:%M:%S %Y', time.gmtime(ts)) self.assertEqual(int(ts), int(parse_date(rs)))
'DateParser: Bad format'
def test_bad(self):
self.assertEqual(None, parse_date('Bad 123'))
'SendFile: Valid requests'
def test_valid(self):
out = static_file(basename, root=root) self.assertEqual(open(__file__, 'rb').read(), out.body.read())
'SendFile: Invalid requests'
def test_invalid(self):
self.assertEqual(404, static_file('not/a/file', root=root).status_code) f = static_file(os.path.join('./../', basename), root='./views/') self.assertEqual(403, f.status_code) try: (fp, fn) = tempfile.mkstemp() os.chmod(fn, 0) self.assertEqual(403, static_file(fn, root='/').status...
'SendFile: Mime Guessing'
def test_mime(self):
f = static_file(basename, root=root) self.assertTrue((f.headers['Content-Type'].split(';')[0] in ('application/x-python-code', 'text/x-python'))) f = static_file(basename, root=root, mimetype='some/type') self.assertEqual('some/type', f.headers['Content-Type']) f = static_file(basename, root=root, m...
'SendFile: If-Modified-Since'
def test_ims(self):
request.environ['HTTP_IF_MODIFIED_SINCE'] = time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime()) res = static_file(basename, root=root) self.assertEqual(304, res.status_code) self.assertEqual(int(os.stat(__file__).st_mtime), parse_date(res.headers['Last-Modified'])) self.assertAl...
'SendFile: If-Modified-Since'
def test_etag(self):
res = static_file(basename, root=root) self.assertTrue(('ETag' in res.headers)) self.assertEqual(200, res.status_code) etag = res.headers['ETag'] request.environ['HTTP_IF_NONE_MATCH'] = etag res = static_file(basename, root=root) self.assertTrue(('ETag' in res.headers)) self.assertEqual(...
'SendFile: Download as attachment'
def test_download(self):
f = static_file(basename, root=root, download='foo.mp3') self.assertEqual('audio/mpeg', f.headers['Content-Type']) f = static_file(basename, root=root, download=True) self.assertEqual(('attachment; filename="%s"' % basename), f.headers['Content-Disposition']) request.environ['HTTP_IF_MODIFIED_SIN...
'Templates: Parse string'
def test_string(self):
self.assertRenders('start {{var}} end', 'start var end', var='var')
'Templates: utf8 code in file'
def test_unicode_code(self):
with chdir(__file__): t = SimpleTemplate(name='./views/stpl_unicode.tpl', lookup=['.']) self.assertRenders(t, 'start \xc3\xb1\xc3\xa7 \xc3\xa4\xc3\xb6\xc3\xbc end\n', var=touni('\xc3\xa4\xc3\xb6\xc3\xbc'))
'Templates: import statement'
def test_import(self):
t = '%from base64 import b64encode\nstart {{b64encode(var.encode("ascii") if hasattr(var, "encode") else var)}} end' self.assertRenders(t, 'start dmFy end', var='var')
'Templates: Data representation'
def test_data(self):
t = SimpleTemplate('<{{var}}>') self.assertRenders('<{{var}}>', '<True>', var=True) self.assertRenders('<{{var}}>', '<False>', var=False) self.assertRenders('<{{var}}>', '<>', var=None) self.assertRenders('<{{var}}>', '<0>', var=0) self.assertRenders('<{{var}}>', '<5>', var=5) self.assertRen...
'Templates: Code blocks and loops'
def test_blocks(self):
t = 'start\n%for i in l:\n{{i}} \n%end\nend' self.assertRenders(t, 'start\n1 \n2 \n3 \nend', l=[1, 2, 3]) self.assertRenders(t, 'start\nend', l=[]) t = 'start\n%if i:\n{{i}} \n%end\nend' self.assertRenders(t, 'start\nTrue \nend', i=True) self.assertRenders(t, 'start...
'Whirespace between block keyword and colon is allowed'
def test_elsebug(self):
self.assertRenders('%if 1:\nyes\n%else:\nno\n%end\n', 'yes\n') self.assertRenders('%if 1:\nyes\n%else :\nno\n%end\n', 'yes\n')
'A "#" sign within an string is not a comment'
def test_commentbug(self):
self.assertRenders("%if '#':\nyes\n%end\n", 'yes\n')
'Block statements with non-terminating newlines'
def test_multiline(self):
self.assertRenders('%if 1\\\n%and 1:\nyes\n%end\n', 'yes\n')
'Block statements with non-terminating newlines in list'
def test_newline_in_parameterlist(self):
self.assertRenders('%a=[1,\n%2]\n{{len(a)}}', '2')
'One-Line dednet blocks should not change indention'
def test_dedentbug(self):
t = '%if x: a="if"\n%else: a="else"\n%end\n{{a}}' self.assertRenders(t, 'if', x=True) self.assertRenders(t, 'else', x=False) t = '%if x:\n%a="if"\n%else: a="else"\n%end\n{{a}}' self.assertRenders(t, 'if', x=True) self.assertRenders(t, 'else', x=False) t = SimpleTemplate('%if ...
'One-Line blocks should not change indention'
def test_onelinebugs(self):
t = '%if x:\n%a=1\n%end\n{{a}}' self.assertRenders(t, '1', x=True) t = '%if x: a=1; end\n{{a}}' self.assertRenders(t, '1', x=True) t = '%if x:\n%a=1\n%else:\n%a=2\n%end\n{{a}}' self.assertRenders(t, '1', x=True) self.assertRenders(t, '2', x=False) t = '%if x: ...
'Templates: one line code blocks'
def test_onelineblocks(self):
t = "start\n%a=''\n%for i in l: a += str(i); end\n{{a}}\nend" self.assertRenders(t, 'start\n123\nend', l=[1, 2, 3]) self.assertRenders(t, 'start\n\nend', l=[])
'Templates: Nobreak statements'
def test_nobreak(self):
self.assertRenders('start\\\\\n%pass\nend', 'startend')
'Templates: Escaped nobreak statements'
def test_nonobreak(self):
self.assertRenders('start\\\\\n\\\\\n%pass\nend', 'start\\\\\nend')
'Templates: Include statements'
def test_include(self):
with chdir(__file__): t = SimpleTemplate(name='stpl_include', lookup=['./views/']) self.assertRenders(t, 'before\nstart var end\nafter\n', var='var')
'Templates: %rebase and method passing'
def test_rebase(self):
with chdir(__file__): t = SimpleTemplate(name='stpl_t2main', lookup=['./views/']) result = '+base+\n+main+\n!1234!\n+include+\n-main-\n+include+\n-base-\n' self.assertRenders(t, result, content='1234')
'Templates: Unavailable templates'
def test_notfound(self):
self.assertRaises(TemplateError, SimpleTemplate, name='abcdef', lookup=['.'])
'Templates: Exceptions'
def test_error(self):
self.assertRaises(SyntaxError, (lambda : SimpleTemplate('%for badsyntax').co)) self.assertRaises(IndexError, SimpleTemplate('{{i[5]}}', lookup=['.']).render, i=[0])
'Templates: Test windows line breaks'
def test_winbreaks(self):
self.assertRenders('%var+=1\r\n{{var}}\r\n', '6\r\n', var=5)
'Templates: Commentd should behave like code-lines (e.g. flush text-lines)'
def test_commentonly(self):
t = SimpleTemplate('...\n%#test\n...') self.assertNotEqual('#test', t.code.splitlines()[0])
'#595: Everything before an \'if\' statement is removed, resulting in SyntaxError.'
def test_bug_block_keywords_eat_prefixed_code(self):
tpl = "% m = 'x' if True else 'y'\n{{m}}" self.assertRenders(tpl, 'x')
'FomsDict.attribute returs string values as unicode.'
def test_attr_access(self):
d = FormsDict(py2=tob('\xe7\x93\xb6'), py3=tob('\xe7\x93\xb6').decode('latin1')) self.assertEqual(touni('\xe7\x93\xb6'), d.py2) self.assertEqual(touni('\xe7\x93\xb6'), d.py3)
'FomsDict.attribute returs u\'\' on missing keys.'
def test_attr_missing(self):
d = FormsDict() self.assertEqual(touni(''), d.missing)
'FomsDict.attribute returs u\'\' on UnicodeError.'
def test_attr_unicode_error(self):
d = FormsDict(latin=touni('\xc3\xb6\xc3\xa4\xc3\xbc\xc3\x9f').encode('latin1')) self.assertEqual(touni(''), d.latin) d.input_encoding = 'latin1' self.assertEqual(touni('\xc3\xb6\xc3\xa4\xc3\xbc\xc3\x9f'), d.latin)
'Test a simple static page with this server adapter.'
def test_simple(self):
if self.skip: return self.assertEqual(tob('OK'), self.fetch('test'))
'MultiDict should behaves like a normal dict'
def test_isadict(self):
(d, m) = (dict(a=5), MultiDict(a=5)) (d['key'], m['key']) = ('value', 'value') (d['k2'], m['k2']) = ('v1', 'v1') (d['k2'], m['k2']) = ('v2', 'v2') self.assertEqual(list(d.keys()), list(m.keys())) self.assertEqual(list(d.values()), list(m.values())) self.assertEqual(list(d.keys()), list(m.ite...
'MultiDict has some special features'
def test_ismulti(self):
m = MultiDict(a=5) m['a'] = 6 self.assertEqual([5, 6], m.getall('a')) self.assertEqual([], m.getall('b')) self.assertEqual([('a', 5), ('a', 6)], list(m.iterallitems()))
'HeaderDict replaces by default and title()s its keys'
def test_isheader(self):
m = HeaderDict(abc_def=5) m['abc_def'] = 6 self.assertEqual(['6'], m.getall('abc_def')) m.append('abc_def', 7) self.assertEqual(['6', '7'], m.getall('abc_def')) self.assertEqual([('Abc-Def', '6'), ('Abc-Def', '7')], list(m.iterallitems()))
'Assure HeaderDict.get() to be case insensitive'
def test_headergetbug(self):
d = HeaderDict() d['UPPER'] = 'UPPER' d['lower'] = 'lower' self.assertEqual(d.get('upper'), 'UPPER') self.assertEqual(d.get('LOWER'), 'lower')
'Verify that 500 errors serializing dictionaries don\'t return content-type application/json'
def test_json_serialization_error(self):
self.app.route('/')((lambda : {'a': set()})) try: self.assertStatus(500) self.assertHeader('Content-Type', 'text/html; charset=UTF-8') except ImportError: warn('Skipping JSON tests.')
'WSGI: Cookies'
def test_cookie(self):
@bottle.route('/cookie') def test(): bottle.response.set_cookie('b', 'b') bottle.response.set_cookie('c', 'c', path='/') return 'hello' try: c = self.urlopen('/cookie')['header'].get_all('Set-Cookie', '') except: c = self.urlopen('/cookie')['header'].get('Set-Cook...
'WSGI: GET routes'
def test_get(self):
@bottle.route('/') def test(): return 'test' self.assertStatus(404, '/not/found') self.assertStatus(405, '/', post='var=value') self.assertBody('test', '/')
'WSGI: POST routes'
def test_post(self):
@bottle.route('/', method='POST') def test(): return 'test' self.assertStatus(404, '/not/found') self.assertStatus(405, '/') self.assertBody('test', '/', post='var=value')
'WSGI: HEAD routes and GET fallback'
def test_headget(self):
@bottle.route('/get') def test(): return 'test' @bottle.route('/head', method='HEAD') def test2(): return 'test' self.assertStatus(405, '/head') self.assertStatus(200, '/head', method='HEAD') self.assertBody('', '/head', method='HEAD') self.assertStatus(200, '/get', metho...
'WSGI: POST routes'
def test_request_attrs(self):
@bottle.route('/') def test(): self.assertEqual(bottle.request.app, bottle.default_app()) self.assertEqual(bottle.request.route, bottle.default_app().routes[0]) return 'foo' self.assertBody('foo', '/')
'204 responses must not return some entity headers'
def get204(self):
bad = ('content-length', 'content-type') for h in bad: bottle.response.set_header(h, 'foo') bottle.status = 204 for (h, v) in bottle.response.headerlist: self.assertFalse((h.lower() in bad), ('Header %s not deleted' % h))
'304 responses must not return entity headers'
def get304(self):
bad = ('allow', 'content-encoding', 'content-language', 'content-length', 'content-md5', 'content-range', 'content-type', 'last-modified') for h in bad: bottle.response.set_header(h, 'foo') bottle.status = 304 for (h, v) in bottle.response.headerlist: self.assertFalse((h.lower() in bad),...
'WSGI: Exceptions within handler code (HTTP 500)'
def test_500(self):
@bottle.route('/') def test(): return (1 / 0) self.assertStatus(500, '/')
'WSGI: UTF-8 Characters in the URL'
def test_utf8_url(self):
@bottle.route('/my-\xc3\xb6\xc3\xa4\xc3\xbc/:string') def test(string): return string self.assertBody(tob('urf8-\xc3\xb6\xc3\xa4\xc3\xbc'), '/my-\xc3\xb6\xc3\xa4\xc3\xbc/urf8-\xc3\xb6\xc3\xa4\xc3\xbc')
'WSGI: abort(401, \'\') (HTTP 401)'
def test_401(self):
@bottle.route('/') def test(): bottle.abort(401) self.assertStatus(401, '/') @bottle.error(401) def err(e): bottle.response.status = 200 return str(type(e)) self.assertStatus(200, '/') self.assertBody("<class 'bottle.HTTPError'>", '/')
'WSGI: redirect (HTTP 303)'
def test_303(self):
@bottle.route('/') def test(): bottle.redirect('/yes') @bottle.route('/one') def test2(): bottle.redirect('/yes', 305) env = {'SERVER_PROTOCOL': 'HTTP/1.1'} self.assertStatus(303, '/', env=env) self.assertHeader('Location', 'http://127.0.0.1/yes', '/', env=env) env = {'SE...
'WSGI: Cookies'
def test_cookie(self):
@bottle.route('/cookie') def test(): bottle.response.set_cookie('b', 'b') bottle.response.set_cookie('c', 'c', path='/') return 'hello' try: c = self.urlopen('/cookie')['header'].get_all('Set-Cookie', '') except: c = self.urlopen('/cookie')['header'].get('Set-Cook...