Example #1
0
 def from_structure(cls, structure: JsonStructure) -> "Predicate":
     operators = tuple(filter(Predicate.Operator.has_value, structure.keys()))
     if len(operators) != 1:
         raise Predicate.InvalidPredicateOperator(
             "Each predicate must define exactly one operator."
         )
     operator = operators[0]
     predicate = cls(operator=operator, case_sensitive=structure.get("caseSensitive", True))
     predicate.fields_from_structure(structure[operator])
     if "xpath" in structure:
         predicate.xpath = structure["xpath"]["selector"]
     return predicate
Example #2
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,
     )
Example #3
0
 def from_structure(cls, inner: JsonStructure) -> "HttpResponse":
     response = cls()
     response.set_if_in_dict(inner, "body", "_body")
     response.mode = Response.Mode(inner.get("_mode", "text"))
     response.set_if_in_dict(inner, "headers", "headers")
     response.set_if_in_dict(inner, "statusCode", "status_code")
     return response
Example #4
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,
     )
Example #5
0
 def from_json(json: JsonStructure) -> "SentEmail":
     email = {
         SentEmail._map_key(k): SentEmail._translate_value(v)
         for k, v in json.items()
     }  # type: Mapping[str, Union[str, Sequence[Address]]]
     sent_email = SentEmail(**email)
     return sent_email
Example #6
0
 def from_structure(structure: JsonStructure) -> "PredicateGenerator":
     path = structure["matches"].get("path", None)
     query = structure["matches"].get("query", None)
     operator = (Predicate.Operator[structure["operator"]]
                 if "operator" in structure else Predicate.Operator.EQUALS)
     case_sensitive = structure.get("caseSensitive", False)
     return PredicateGenerator(path=path,
                               query=query,
                               operator=operator,
                               case_sensitive=case_sensitive)
Example #7
0
 def from_structure(structure: JsonStructure) -> "BasePredicate":
     if "and" in structure:
         return AndPredicate.from_structure(structure)
     elif "or" in structure:
         return OrPredicate.from_structure(structure)
     elif "contains" in structure and "data" in structure["contains"]:
         return TcpPredicate.from_structure(structure)
     elif "inject" in structure:
         return InjectionPredicate.from_structure(structure)
     elif set(structure.keys()).intersection(
         {o.value
          for o in Predicate.Operator}):  # pragma: no cover
         return Predicate.from_structure(structure)
     raise NotImplementedError()  # pragma: no cover
Example #8
0
 def from_structure(structure: JsonStructure) -> "Response":
     response = Response()
     response.fields_from_structure(structure)
     behaviors = structure.get("_behaviors", {})
     response.set_if_in_dict(behaviors, "wait", "wait")
     response.set_if_in_dict(behaviors, "repeat", "repeat")
     response.set_if_in_dict(behaviors, "decorate", "decorate")
     response.set_if_in_dict(behaviors, "shellTransform", "shell_transform")
     if "copy" in behaviors:
         response.copy = [Copy.from_structure(c) for c in behaviors["copy"]]
     if "lookup" in behaviors:
         response.lookup = [
             Lookup.from_structure(lookup) for lookup in behaviors["lookup"]
         ]
     return response
Example #9
0
 def from_structure(structure: JsonStructure) -> "Proxy":
     proxy_structure = structure["proxy"]
     proxy = Proxy(
         to=furl(proxy_structure["to"]),
         inject_headers=proxy_structure["injectHeaders"]
         if "injectHeaders" in proxy_structure else None,
         mode=Proxy.Mode(proxy_structure["mode"]),
         predicate_generators=[
             PredicateGenerator.from_structure(pg)
             for pg in proxy_structure["predicateGenerators"]
         ] if "predicateGenerators" in proxy_structure else None,
     )
     wait = structure.get("_behaviors", {}).get("wait")
     if wait:
         proxy.wait = wait
     return proxy
Example #10
0
 def from_structure(cls, structure: JsonStructure) -> "Proxy":
     proxy_structure = structure["proxy"]
     proxy = cls(
         to=furl(proxy_structure["to"]),
         inject_headers=proxy_structure["injectHeaders"]
         if "injectHeaders" in proxy_structure else None,
         mode=Proxy.Mode(proxy_structure["mode"]),
         predicate_generators=[
             PredicateGenerator.from_structure(pg)
             for pg in proxy_structure["predicateGenerators"]
         ] if "predicateGenerators" in proxy_structure else None,
     )
     behaviors = structure.get("_behaviors", {})
     proxy.set_if_in_dict(behaviors, "wait", "wait")
     proxy.set_if_in_dict(behaviors, "decorate", "decorate")
     return proxy
Example #11
0
 def from_structure(structure: JsonStructure) -> "AddStub":
     return AddStub(
         index=structure.get("index"),
         stub=Stub().from_structure(structure.get("stub")),
     )
Example #12
0
 def from_json(json: JsonStructure) -> "HttpRequest":
     return HttpRequest(**{k: v for k, v in json.items()})