Globally log requests

This commit is contained in:
Markus Unterwaditzer 2014-03-29 17:52:57 +01:00
parent bd351242e6
commit 4e1371e561
4 changed files with 17 additions and 31 deletions

View file

@ -19,27 +19,3 @@ def normalize_item(item):
def assert_item_equals(a, b):
assert normalize_item(a) == normalize_item(b)
def log_request(method, url, data, headers):
print(method)
print(url)
print(data)
print(headers)
def log_response(r):
print(r.status_code)
print(r.text)
def requests_mock(monkeypatch):
'''It is easier than setting up the logging module!'''
import requests.sessions
old_func = requests.sessions.Session.request
def mock_request(self, method, url, data=None, headers=None, **kw):
log_request(method, url, data, headers)
r = old_func(self, method, url, data=data, headers=headers, **kw)
log_response(r)
return r
monkeypatch.setattr('requests.sessions.Session.request', mock_request)

17
tests/conftest.py Normal file
View file

@ -0,0 +1,17 @@
import pytest
@pytest.fixture(autouse=True)
def requests_mock(monkeypatch):
'''It is easier than setting up the logging module!'''
import requests.sessions
old_func = requests.sessions.Session.request
def mock_request(self, method, url, data=None, headers=None, **kw):
print(method)
print(url)
print(data)
print(headers)
r = old_func(self, method, url, data=data, headers=headers, **kw)
print(r.status_code)
print(r.text)
return r
monkeypatch.setattr('requests.sessions.Session.request', mock_request)

View file

@ -15,7 +15,6 @@ import os
import time
import pytest
import requests
from tests import requests_mock
owncloud_repo = expand_path(os.path.join(
os.path.dirname(__file__), '../../../owncloud-testserver/'
@ -46,7 +45,6 @@ class ServerMixin(object):
xprocess.ensure('owncloud_server', preparefunc)
subprocess.check_call([os.path.join(owncloud_repo, 'reset.sh')])
requests_mock(monkeypatch)
def get_storage_args(self, collection='test'):
url = 'http://127.0.0.1:8080'

View file

@ -18,8 +18,6 @@ import urlparse
import shutil
import pytest
from tests import log_request, log_response
from werkzeug.test import Client
from werkzeug.wrappers import BaseResponse as WerkzeugResponse
@ -118,8 +116,6 @@ class ServerMixin(object):
from requests import Response
def send(self, request, *args, **kwargs):
log_request(request.method, request.url, request.body,
request.headers)
path = urlparse.urlparse(request.url).path
wr = c.open(path=path, method=request.method,
data=request.body, headers=dict(request.headers))
@ -129,7 +125,6 @@ class ServerMixin(object):
r.headers = wr.headers
r.encoding = wr.charset
r.status_code = wr.status_code
log_response(r)
return r