def from_structure(structure: JsonStructure) -> "Stub": responses: List[Union[Proxy, Response]] = [] for response in structure.get("responses", ()): if "proxy" in response: responses.append(Proxy.from_structure(response)) else: responses.append(Response.from_structure(response)) return Stub( [ Predicate.from_structure(predicate) for predicate in structure.get("predicates", ()) ], responses, )
def from_structure(structure): responses = [] for response in structure.get("responses", ()): if "proxy" in response: responses.append(Proxy.from_structure(response)) else: responses.append(Response.from_structure(response)) return Stub( [ Predicate.from_structure(predicate) for predicate in structure.get("predicates", ()) ], responses, )
def from_structure(cls, structure: JsonStructure) -> "Stub": responses: List[Union[InjectionResponse, Proxy, Response]] = [] for response in structure.get("responses", ()): if "proxy" in response: responses.append(Proxy.from_structure(response)) elif "inject" in response: responses.append(InjectionResponse.from_structure(response)) else: responses.append(Response.from_structure(response)) return cls( [ BasePredicate.from_structure(predicate) for predicate in structure.get("predicates", ()) ], responses, )
def __init__( self, predicates: Optional[Union[BasePredicate, Iterable[BasePredicate]]] = None, responses: Optional[Union[BaseResponse, Iterable[BaseResponse]]] = None, ) -> None: if predicates: self.predicates = predicates if isinstance( predicates, Sequence) else [predicates] else: self.predicates = [Predicate()] if responses: self.responses = responses if isinstance( responses, Sequence) else [responses] else: self.responses = [Response()]
def __init__(self, predicates=None, responses=None): """ :param predicates: Trigger this stub if one of these predicates matches the request :type predicates: Predicate or list(Predicate) :param responses: Use these response behaviors (in order) :type responses: Response or list(Response) """ if predicates: self.predicates = predicates if isinstance( predicates, Sequence) else [predicates] else: self.predicates = [Predicate()] if responses: self.responses = responses if isinstance( responses, Sequence) else [responses] else: self.responses = [Response()]
def test_structure_wait(): expected_response = Response(wait=300) response_structure = expected_response.as_structure() response = BaseResponse.from_structure(response_structure) assert response.wait == expected_response.wait
def test_structure_repeat(): expected_response = Response(repeat=1) response_structure = expected_response.as_structure() response = BaseResponse.from_structure(response_structure) assert response.repeat == expected_response.repeat
def test_structure_no_status(): expected_response = Response() response_structure = expected_response.as_structure() del response_structure["is"]["statusCode"] response = BaseResponse.from_structure(response_structure) assert response.status_code == expected_response.status_code
def test_structure_status(): expected_response = Response(status_code=204) response_structure = expected_response.as_structure() response = BaseResponse.from_structure(response_structure) assert response.status_code == expected_response.status_code
def test_structure_body(): expected_response = Response(body="darwin") response_structure = expected_response.as_structure() response = BaseResponse.from_structure(response_structure) assert response.body == expected_response.body
def test_structure_headers(): expected_response = Response( headers={"X-Clacks-Overhead": "GNU Terry Pratchett"}) response_structure = expected_response.as_structure() response = BaseResponse.from_structure(response_structure) assert response.headers == expected_response.headers