mirror of
https://github.com/samsonjs/vdirsyncer.git
synced 2026-04-27 14:57:41 +00:00
Globally log requests
This commit is contained in:
parent
bd351242e6
commit
4e1371e561
4 changed files with 17 additions and 31 deletions
|
|
@ -19,27 +19,3 @@ def normalize_item(item):
|
||||||
|
|
||||||
def assert_item_equals(a, b):
|
def assert_item_equals(a, b):
|
||||||
assert normalize_item(a) == normalize_item(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
17
tests/conftest.py
Normal 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)
|
||||||
|
|
@ -15,7 +15,6 @@ import os
|
||||||
import time
|
import time
|
||||||
import pytest
|
import pytest
|
||||||
import requests
|
import requests
|
||||||
from tests import requests_mock
|
|
||||||
|
|
||||||
owncloud_repo = expand_path(os.path.join(
|
owncloud_repo = expand_path(os.path.join(
|
||||||
os.path.dirname(__file__), '../../../owncloud-testserver/'
|
os.path.dirname(__file__), '../../../owncloud-testserver/'
|
||||||
|
|
@ -46,7 +45,6 @@ class ServerMixin(object):
|
||||||
|
|
||||||
xprocess.ensure('owncloud_server', preparefunc)
|
xprocess.ensure('owncloud_server', preparefunc)
|
||||||
subprocess.check_call([os.path.join(owncloud_repo, 'reset.sh')])
|
subprocess.check_call([os.path.join(owncloud_repo, 'reset.sh')])
|
||||||
requests_mock(monkeypatch)
|
|
||||||
|
|
||||||
def get_storage_args(self, collection='test'):
|
def get_storage_args(self, collection='test'):
|
||||||
url = 'http://127.0.0.1:8080'
|
url = 'http://127.0.0.1:8080'
|
||||||
|
|
|
||||||
|
|
@ -18,8 +18,6 @@ import urlparse
|
||||||
import shutil
|
import shutil
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from tests import log_request, log_response
|
|
||||||
|
|
||||||
from werkzeug.test import Client
|
from werkzeug.test import Client
|
||||||
from werkzeug.wrappers import BaseResponse as WerkzeugResponse
|
from werkzeug.wrappers import BaseResponse as WerkzeugResponse
|
||||||
|
|
||||||
|
|
@ -118,8 +116,6 @@ class ServerMixin(object):
|
||||||
from requests import Response
|
from requests import Response
|
||||||
|
|
||||||
def send(self, request, *args, **kwargs):
|
def send(self, request, *args, **kwargs):
|
||||||
log_request(request.method, request.url, request.body,
|
|
||||||
request.headers)
|
|
||||||
path = urlparse.urlparse(request.url).path
|
path = urlparse.urlparse(request.url).path
|
||||||
wr = c.open(path=path, method=request.method,
|
wr = c.open(path=path, method=request.method,
|
||||||
data=request.body, headers=dict(request.headers))
|
data=request.body, headers=dict(request.headers))
|
||||||
|
|
@ -129,7 +125,6 @@ class ServerMixin(object):
|
||||||
r.headers = wr.headers
|
r.headers = wr.headers
|
||||||
r.encoding = wr.charset
|
r.encoding = wr.charset
|
||||||
r.status_code = wr.status_code
|
r.status_code = wr.status_code
|
||||||
log_response(r)
|
|
||||||
return r
|
return r
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue