Skip to content

Commit

Permalink
Merge pull request #4 from igorozt/master
Browse files Browse the repository at this point in the history
fix for non-origin base_url + test-cases
  • Loading branch information
trifonovmixail authored Mar 29, 2019
2 parents 1ca4739 + 734da9a commit 4c83e51
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
16 changes: 5 additions & 11 deletions reqtools/sessions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
# -*- coding: utf-8 -*-

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

import curlify
from requests import Session
Expand Down Expand Up @@ -36,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 Down
18 changes: 18 additions & 0 deletions tests/suite_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,24 @@ def test_remote_api_build_url_with_prefix(prefix, url_path, expected):
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'
Expand Down

0 comments on commit 4c83e51

Please sign in to comment.