diff --git a/reqtools/sessions.py b/reqtools/sessions.py index a829905..cbe7818 100644 --- a/reqtools/sessions.py +++ b/reqtools/sessions.py @@ -1,11 +1,9 @@ # -*- coding: utf-8 -*- -import os -import re import json import logging -from urllib.parse import urljoin +import curlify from requests import Session @@ -13,6 +11,7 @@ class RemoteApiSession(Session): + __attrs__ = Session.__attrs__ + ['_base_url', '_prefix'] def __init__(self, base_url: str, *, prefix: str = None): super(RemoteApiSession, self).__init__() @@ -34,19 +33,16 @@ def prefix(self): @property def url(self): if self.prefix: - return urljoin(self._base_url, self._prefix) - + return self._glue_parts(self._base_url, self._prefix) return self._base_url def _build_url(self, url_path: str): if self._prefix: - slash_in_the_end = url_path.endswith('/') - url_path = os.path.join(self._prefix, re.sub('^/|/$', '', url_path)) - - if slash_in_the_end: - url_path += '/' + url_path = self._glue_parts(self._prefix, url_path) + return self._glue_parts(self._base_url, url_path) - return urljoin(self._base_url, url_path) + def _glue_parts(self, part1, part2: str): + return part1.rstrip('/') + '/' + part2.lstrip('/') def request(self, method: str, url_path: str, **kwargs): url = self._build_url(url_path) @@ -59,6 +55,7 @@ def request(self, method: str, url_path: str, **kwargs): logger.info(f'Request param "{k}": {message}') resp = super(RemoteApiSession, self).request(method, url, **kwargs) + logger.info(curlify.to_curl(resp.request)) logger.info(f'Response status code is "{resp.status_code}"') try: diff --git a/setup.py b/setup.py index 765fe12..39496bc 100644 --- a/setup.py +++ b/setup.py @@ -31,6 +31,7 @@ platforms='any', install_requires=[ 'requests', + 'curlify' ], classifiers=( 'Development Status :: 4 - Beta', diff --git a/test.requirements.txt b/test.requirements.txt index 0cad7a6..2296ff7 100644 --- a/test.requirements.txt +++ b/test.requirements.txt @@ -1,6 +1,7 @@ requests pytest==3.6.0 pyhamcrest==1.9.0 +curlify==2.1.1 git+https://gitlab.com/pycqa/flake8#egg=flake8 flake8-quotes diff --git a/tests/suite_sessions.py b/tests/suite_sessions.py index 65acb86..e623f93 100644 --- a/tests/suite_sessions.py +++ b/tests/suite_sessions.py @@ -1,34 +1,35 @@ # -*- coding: utf-8 -*- +import pickle import pytest from requests import Session as RequestsSession -from hamcrest import assert_that, equal_to, instance_of +from hamcrest import assert_that, equal_to, instance_of, is_ -from reqtools import sessions +from reqtools import RemoteApiSession def test_remote_api_create_instance(): - session = sessions.RemoteApiSession('http://test.ru') + session = RemoteApiSession('http://test.ru') assert_that(session, instance_of(RequestsSession)) def test_remote_api_base_url(): url = 'http://test.ru' - session = sessions.RemoteApiSession(url) - assert_that(session.base_url, url) + session = RemoteApiSession(url) + assert_that(session.base_url, is_(equal_to(url))) def test_remote_api_prefix(): url = 'http://test.ru' prefix = 'prefix' - session = sessions.RemoteApiSession(url, prefix=prefix) - assert_that(session.prefix, prefix) + session = RemoteApiSession(url, prefix=prefix) + assert_that(session.prefix, is_(equal_to(prefix))) def test_remote_api_url(): url = 'http://test.ru' - session = sessions.RemoteApiSession(url) - assert_that(session.url, url) + session = RemoteApiSession(url) + assert_that(session.url, is_(equal_to(url))) @pytest.mark.parametrize( @@ -40,13 +41,13 @@ def test_remote_api_url(): ], ) def test_remote_api_url_with_prefix(url, prefix, expected): - session = sessions.RemoteApiSession(url, prefix=prefix) - assert_that(session.url, expected) + session = RemoteApiSession(url, prefix=prefix) + assert_that(session.url, is_(equal_to(expected))) def test_remote_api_build_url(): - session = sessions.RemoteApiSession('http://test.ru') - assert_that(session._build_url('/test'), equal_to('http://test.ru/test')) + session = RemoteApiSession('http://test.ru') + assert_that(session._build_url('/test'), is_(equal_to('http://test.ru/test'))) @pytest.mark.parametrize( @@ -61,5 +62,34 @@ def test_remote_api_build_url(): ], ) def test_remote_api_build_url_with_prefix(prefix, url_path, expected): - session = sessions.RemoteApiSession('http://test.ru', prefix=prefix) - assert_that(session._build_url(url_path), equal_to(expected)) + session = RemoteApiSession('http://test.ru', prefix=prefix) + assert_that(session._build_url(url_path), is_(equal_to(expected))) + + +@pytest.mark.parametrize( + 'base, prefix, url_path, expected', + [ + ('http://localhost:5000/api', 'prefix', 'test/', 'http://localhost:5000/api/prefix/test/'), + ('http://localhost:5000/api', 'prefix/', 'test', 'http://localhost:5000/api/prefix/test'), + ('http://localhost:5000/api', '/prefix', 'test/', 'http://localhost:5000/api/prefix/test/'), + ('http://localhost:5000/api', '/prefix/', 'test', 'http://localhost:5000/api/prefix/test'), + ('http://localhost:5000/api/', 'prefix', 'test/', 'http://localhost:5000/api/prefix/test/'), + ('http://localhost:5000/api/', 'prefix/', 'test', 'http://localhost:5000/api/prefix/test'), + ('http://localhost:5000/api/', '/prefix', 'test/', 'http://localhost:5000/api/prefix/test/'), + ('http://localhost:5000/api/', '/prefix/', 'test', 'http://localhost:5000/api/prefix/test'), + ], +) +def test_remote_api_build_url_with_prefix_and_nonorigin_base(base, prefix, url_path, expected): + session = RemoteApiSession(base, prefix=prefix) + assert_that(session._build_url(url_path), is_(equal_to(expected))) + + +def test_serializable(): + base_url = 'http://test.ru' + prefix = '/prefix' + session_original = RemoteApiSession(base_url, prefix=prefix) + serialization = pickle.dumps(session_original) + del session_original + session_loaded = pickle.loads(serialization) + assert_that(session_loaded.base_url, is_(base_url)) + assert_that(session_loaded.prefix, is_(prefix))