Skip to content

Commit

Permalink
Merge pull request #100 from camptocamp/put-handle-204-response
Browse files Browse the repository at this point in the history
Allow empty responses (status==204) in acceptance tests
  • Loading branch information
Patrick Valsecchi authored Feb 28, 2018
2 parents e984005 + c178baa commit 1bae4c2
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
7 changes: 7 additions & 0 deletions acceptance_tests/app/c2cwsgiutils_app/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
hello_service = services.create("hello", "/hello", cors_credentials=True)
error_service = services.create("error", "/error")
tracking_service = services.create("tracking", "/tracking/{depth:[01]}")
empty_service = services.create("empty", "/empty")
leaked_objects = []


Expand Down Expand Up @@ -77,3 +78,9 @@ def tracking(request):
if depth > 0:
result['sub'] = requests.get('http://localhost/api/tracking/%d' % (depth - 1)).json()
return result


@empty_service.put()
def empty(request):
request.response.status_code = 204
return request.response
2 changes: 2 additions & 0 deletions acceptance_tests/tests/tests/test_connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def test_empty_response(app_connection):
assert app_connection.put_json("empty", expected_status=204) is None
16 changes: 8 additions & 8 deletions c2cwsgiutils/acceptance/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ def __init__(self, base_url: str, origin: str) -> None:
self.origin = origin

def get(self, url: str, expected_status: int=200, params: Mapping[str, str]=None,
headers: Mapping[str, str]=None, cors: bool=True, cache_allowed: bool=False) -> str:
headers: Mapping[str, str]=None, cors: bool=True, cache_allowed: bool=False) -> Optional[str]:
"""
get the given URL (relative to the root of API).
"""
with self.session.get(self.base_url + url, params=params,
headers=self._merge_headers(headers, cors)) as r:
check_response(r, expected_status, cache_allowed=cache_allowed)
self._check_cors(cors, r)
return r.text
return None if r.status_code == 204 else r.text

def get_raw(self, url: str, expected_status: int=200, params: Mapping[str, str]=None,
headers: Mapping[str, str]=None, cors: bool=True,
Expand All @@ -44,7 +44,7 @@ def get_json(self, url: str, expected_status: int=200, params: Mapping[str, str]
headers=self._merge_headers(headers, cors)) as r:
check_response(r, expected_status, cache_allowed=cache_allowed)
self._check_cors(cors, r)
return r.json()
return None if r.status_code == 204 else r.json()

def get_xml(self, url: str, schema: Optional[str]=None, expected_status: int=200,
params: Mapping[str, str]=None, headers: Mapping[str, str]=None, cors: bool=True,
Expand Down Expand Up @@ -74,7 +74,7 @@ def post_json(self, url: str, data: Any=None, json: Any=None, expected_status: i
headers=self._merge_headers(headers, cors)) as r:
check_response(r, expected_status, cache_allowed=cache_allowed)
self._check_cors(cors, r)
return r.json()
return None if r.status_code == 204 else r.json()

def post_files(self, url: str, data: Any=None, files: Optional[Mapping[str, Any]]=None,
expected_status: int=200, params: Mapping[str, str]=None, headers: Mapping[str, str]=None,
Expand All @@ -86,10 +86,10 @@ def post_files(self, url: str, data: Any=None, files: Optional[Mapping[str, Any]
headers=self._merge_headers(headers, cors)) as r:
check_response(r, expected_status, cache_allowed)
self._check_cors(cors, r)
return r.json()
return None if r.status_code == 204 else r.json()

def post(self, url: str, data: Any=None, expected_status: int=200, params: Mapping[str, str]=None,
headers: Mapping[str, str]=None, cors: bool=True, cache_allowed: bool=False) -> str:
headers: Mapping[str, str]=None, cors: bool=True, cache_allowed: bool=False) -> Optional[str]:
"""
POST the given URL (relative to the root of API).
"""
Expand All @@ -98,7 +98,7 @@ def post(self, url: str, data: Any=None, expected_status: int=200, params: Mappi
data=data, params=params) as r:
check_response(r, expected_status, cache_allowed)
self._check_cors(cors, r)
return r.text
return None if r.status_code == 204 else r.text

def put_json(self, url: str, json: Any=None, expected_status: int=200, params: Mapping[str, str]=None,
headers: Mapping[str, str]=None, cors: bool=True, cache_allowed: bool=False) -> Any:
Expand All @@ -109,7 +109,7 @@ def put_json(self, url: str, json: Any=None, expected_status: int=200, params: M
headers=self._merge_headers(headers, cors)) as r:
check_response(r, expected_status, cache_allowed)
self._check_cors(cors, r)
return r.json()
return None if r.status_code == 204 else r.json()

def delete(self, url: str, expected_status: int=204, params: Mapping[str, str]=None,
headers: Mapping[str, str]=None, cors: bool=True,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from setuptools import setup, find_packages


VERSION = '1.7.0'
VERSION = '1.7.1'
HERE = os.path.abspath(os.path.dirname(__file__))
INSTALL_REQUIRES = [
pkg.split('==')[0]
Expand Down

0 comments on commit 1bae4c2

Please sign in to comment.