Skip to content
This repository has been archived by the owner on Nov 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #683 from cisco-open/fix_issue_646
Browse files Browse the repository at this point in the history
Fix Issue646 Not content in response
  • Loading branch information
jpkrajewski authored May 20, 2024
2 parents 96cda5d + a3c83d3 commit 5454f14
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
8 changes: 6 additions & 2 deletions catalystwan/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,9 @@ def parse_cookies_to_dict(cookies: str) -> Dict[str, str]:


class JsonPayload:
def __init__(self, json: Any = None):
def __init__(self, json: Any = None, empty: bool = False):
self.json = json
self.empty = empty
self.data = None
self.error = None
self.headers = None
Expand All @@ -141,7 +142,7 @@ def __init__(self, response: Response):
try:
self.payload = JsonPayload(response.json())
except JSONDecodeError:
self.payload = JsonPayload()
self.payload = JsonPayload(empty=True)

def _detect_expired_jsessionid(self) -> bool:
"""Determines if server sent expired JSESSIONID"""
Expand Down Expand Up @@ -184,6 +185,9 @@ def dataseq(self, cls: Type[T], sourcekey: Optional[str] = "data", validate: boo
DataSequence[T] of given type T which is subclassing from Dataclass/BaseModel,
in case JSON payload was containing a single Object - sequence with one element is returned
"""
if self.payload.empty:
return DataSequence(cls, [])

if sourcekey is None:
data = self.payload.json
else:
Expand Down
10 changes: 10 additions & 0 deletions catalystwan/tests/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from attr import define, field # type: ignore
from parameterized import parameterized # type: ignore
from pydantic import BaseModel, Field, ValidationError
from requests.exceptions import JSONDecodeError

from catalystwan.dataclasses import DataclassBase
from catalystwan.response import ManagerErrorInfo, ManagerResponse
Expand Down Expand Up @@ -171,3 +172,12 @@ def test_dataseq_optional_validate(self):
assert data.important == VALIDATE_DATASEQ_TEST_DATA[i]["important"]
with self.assertRaises(ValidationError):
vmng_response.dataseq(DataForValidateTest, sourcekey=None, validate=True)

def test_dataseq_with_misisng_data(self):
self.response_mock.json.side_effect = JSONDecodeError("test", "test", 1)
vmng_response = ManagerResponse(self.response_mock)
# Act
dataseq = vmng_response.dataseq(DataForValidateTest, sourcekey="data", validate=True)
# Assert
assert isinstance(dataseq, DataSequence)
assert len(dataseq) == 0

0 comments on commit 5454f14

Please sign in to comment.