Exemplo n.º 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,
     )
Exemplo n.º 2
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
Exemplo n.º 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,
     )
Exemplo n.º 4
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)
Exemplo n.º 5
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
Exemplo n.º 6
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
Exemplo n.º 7
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
Exemplo n.º 8
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
Exemplo n.º 9
0
 def from_structure(structure: JsonStructure) -> "AddStub":
     return AddStub(
         index=structure.get("index"),
         stub=Stub().from_structure(structure.get("stub")),
     )