Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/oztqa/reqtools
Browse files Browse the repository at this point in the history
  • Loading branch information
mikhail.trifonov committed May 14, 2019
2 parents 7e17463 + 5664b60 commit 1e128b7
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 26 deletions.
19 changes: 8 additions & 11 deletions reqtools/sessions.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# -*- coding: utf-8 -*-

import os
import re
import json
import logging
from urllib.parse import urljoin

import curlify
from requests import Session


logger = logging.getLogger(__name__)


class RemoteApiSession(Session):
__attrs__ = Session.__attrs__ + ['_base_url', '_prefix']

def __init__(self, base_url: str, *, prefix: str = None):
super(RemoteApiSession, self).__init__()
Expand All @@ -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)
Expand All @@ -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:
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
platforms='any',
install_requires=[
'requests',
'curlify'
],
classifiers=(
'Development Status :: 4 - Beta',
Expand Down
1 change: 1 addition & 0 deletions test.requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
60 changes: 45 additions & 15 deletions tests/suite_sessions.py
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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(
Expand All @@ -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))

0 comments on commit 1e128b7

Please sign in to comment.