Skip to content

Commit

Permalink
Added support for quoted text
Browse files Browse the repository at this point in the history
  • Loading branch information
ezturner committed Aug 9, 2017
1 parent d6bedc7 commit 8b5e569
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{ "headers": {"Custom": "My Custom Value"},
"status": 200
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
11 changes: 11 additions & 0 deletions restclients_core/tests/dao_implementation/test_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from restclients_core.dao import DAO
from os.path import abspath, dirname
from restclients_core.dao import MockDAO
from restclients_core.exceptions import DataFailureException


class TDAO(DAO):
Expand Down Expand Up @@ -65,3 +66,13 @@ def test_quoted_params(self):
response = TDAO().getURL('/search?'
'first=a&second=a%3Ab%3Ac')
self.assertEquals(response.status, 200)

def test_no_body_file(self):
response = TDAO().getURL('/search?'
'first=abc')
self.assertEquals(response.status, 200)

def test_multiple_files(self):
with self.assertRaises(DataFailureException):
TDAO().getURL('/search?'
'first=a&second=b')
26 changes: 19 additions & 7 deletions restclients_core/util/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ def load_resource_from_path(resource_dir, service_name,
handle = open_file(orig_file_path)
header_handle = open_file(orig_file_path + ".http-headers")

if "?" in url and handle is None:
# attempt to open query permutations even on success
# so that if there are multiple files we throw an exception
if "?" in url:
handle = attempt_open_query_permutations(url, orig_file_path, False)


if "?" in url and header_handle is None:
if "?" in url:
header_handle = attempt_open_query_permutations(url, orig_file_path, True)

if handle is None and header_handle is None:
Expand Down Expand Up @@ -130,10 +131,15 @@ def attempt_open_query_permutations(url, orig_file_path, is_header_file):
# ensure that there are not extra parameters on any files
if is_header_file:
filenames = [f for f in filenames if ".http-headers" in f]
filenames = [f for f in filenames if len(orig_file_path + ".http-headers") - len(directory) == len(f)]
filenames = [f for f in filenames if
_compare_file_name(orig_file_path + ".http-headers",
directory,
f)]
else:
filenames = [f for f in filenames if ".http-headers" not in f]
filenames = [f for f in filenames if len(orig_file_path) - len(directory) == len(f)]
filenames = [f for f in filenames if _compare_file_name(orig_file_path,
directory,
f)]

url_parts = url.split("/")
url_parts = url_parts[len(url_parts) - 1].split("?")
Expand All @@ -159,5 +165,11 @@ def attempt_open_query_permutations(url, orig_file_path, is_header_file):

# if there is more than one file, raise an exception
if len(filenames) > 1:
raise DataFailureException("Multiple mock data files matched the " +
"parameters provided!")
raise DataFailureException(url,
"Multiple mock data files matched the " +
"parameters provided!",
404)


def _compare_file_name(orig_file_path, directory, filename):
return len(unquote(orig_file_path)) - len(directory) == len(filename)

0 comments on commit 8b5e569

Please sign in to comment.