Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

During tests, log all paths used with the test client #188

Merged
merged 1 commit into from
Apr 23, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions relengapi/lib/testing/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import inspect
import logging
import relengapi.app
import wrapt

from flask import json
from relengapi.lib import auth

log = logging.getLogger(__name__)


class TestContext(object):

Expand Down Expand Up @@ -76,22 +79,40 @@ def set_user():
self.options['app_setup'](app)
return app

def _wrap_client(self, client):
# create a post_json convenience method
def post_json(path, data):
return client.post(
path, data=json.dumps(data),
headers=[('Content-Type', 'application/json')])
client.post_json = post_json

# patch 'open' to log the request
old_open = client.open

def open(*args, **kwargs):
path = args[0]
method = kwargs.get('method')
log.info('request: {} {}'.format(method, path))
resp = old_open(*args, **kwargs)
log.info('response: {}'.format(resp.status))
return resp
client.open = open

@wrapt.decorator
def __call__(self, wrapped, instance, given_args, kwargs):
arginfo = inspect.getargspec(wrapped)
args = set((arginfo.args if arginfo.args else []) +
(arginfo.keywords if arginfo.keywords else []))

def post_json(path, data):
return kwargs['client'].post(
path, data=json.dumps(data),
headers=[('Content-Type', 'application/json')])
app = self._make_app()
if 'app' in args:
kwargs['app'] = app

if 'client' in args:
kwargs['client'] = app.test_client()
kwargs['client'].post_json = post_json
self._wrap_client(kwargs['client'])

if 'db_setup' in self.options:
self.options['db_setup'](app)
try:
Expand Down