Exemple #1
0
 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,
     )
Exemple #2
0
 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,
     )
Exemple #3
0
 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,
     )
Exemple #4
0
 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()]
Exemple #5
0
 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()]
Exemple #6
0
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
Exemple #7
0
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
Exemple #8
0
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
Exemple #9
0
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
Exemple #10
0
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
Exemple #11
0
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