Ejemplo n.º 1
0
 def test_with_positional_and_kwargs(self):
     assert (str(
         py.FunctionCall(
             "m.f",
             [py.Literal(True),
              py.FunctionCall("g", [py.Symbol("f")])],
             {
                 "a": py.Literal(2),
                 "bc": py.Literal("x")
             },
         )) == "m.f(True, g(f), a=2, bc='x')")
Ejemplo n.º 2
0
 def test_repr(self):
     arg = py.Symbol("a")
     kwarg = py.Symbol("v")
     assert (repr(py.FunctionCall(
         "f", [arg],
         {"k": kwarg
          })) == f"FunctionCall('f', [{arg!r}], {{'k': {kwarg!r}}})")
Ejemplo n.º 3
0
def lreq_to_expr(lr: LocustRequest) -> py.FunctionCall:
    # TODO: Remove me once LocustRequest no longer exists.
    #   See https://github.com/zalando-incubator/Transformer/issues/11.
    url = _peel_off_repr(lr.url)
    name = _peel_off_repr(lr.name) if lr.name else url

    args: Dict[str, py.Expression] = OrderedDict(
        url=url,
        name=name,
        timeout=py.Literal(TIMEOUT),
        allow_redirects=py.Literal(False),
    )
    if lr.headers:
        args["headers"] = py.Literal(lr.headers)
    if lr.method is HttpMethod.POST:
        if lr.post_data:
            rpd = RequestsPostData.from_har_post_data(lr.post_data)
            args.update(rpd.as_kwargs())
    elif lr.method in (HttpMethod.PUT, HttpMethod.PATCH):
        if lr.post_data:
            rpd = RequestsPostData.from_har_post_data(lr.post_data)
            args.update(rpd.as_kwargs())

        args.setdefault("params", py.Literal([]))
        cast(py.Literal, args["params"]).value.extend(
            _params_from_name_value_dicts(
                [dataclasses.asdict(q) for q in lr.query]))
    elif lr.method not in NOOP_HTTP_METHODS:
        raise ValueError(f"unsupported HTTP method: {lr.method!r}")

    method = lr.method.name.lower()
    return py.FunctionCall(name=f"self.client.{method}", named_args=args)
Ejemplo n.º 4
0
 def test_with_kwargs(self):
     assert (str(
         py.FunctionCall("f",
                         named_args={
                             "a": py.Literal(2),
                             "bc": py.Literal("x")
                         })) == "f(a=2, bc='x')")
Ejemplo n.º 5
0
 def test_it_supports_patch_requests_with_payload(self):
     url = "http://abc.de"
     r = LocustRequest.from_request(
         Request(
             timestamp=MagicMock(),
             method=HttpMethod.PATCH,
             url=urlparse(url),
             har_entry={"entry": "data"},
             headers={"a": "b"},
             query=[QueryPair("c", "d")],
             post_data={
                 "mimeType": "application/json",
                 "params": [{"name": "x", "value": "y"}],
                 "text": """{"z": 7}""",
             },
         )
     )
     assert lreq_to_expr(r) == py.FunctionCall(
         name="self.client.patch",
         named_args={
             "url": py.Literal(url),
             "name": py.Literal(url),
             "headers": py.Literal({"a": "b"}),
             "timeout": py.Literal(TIMEOUT),
             "allow_redirects": py.Literal(False),
             "json": py.Literal({"z": 7}),
             "params": py.Literal([(b"x", b"y"), (b"c", b"d")]),
         },
     )
Ejemplo n.º 6
0
def req_to_expr(r: Request) -> py.FunctionCall:
    url = py.Literal(str(r.url.geturl()))
    args: Dict[str, py.Expression] = OrderedDict(
        url=url,
        name=py.Literal(r.name) if r.name else url,
        timeout=py.Literal(TIMEOUT),
        allow_redirects=py.Literal(False),
    )
    if r.headers:
        args["headers"] = py.Literal(r.headers)

    if r.method is HttpMethod.POST:
        if r.post_data:
            rpd = RequestsPostData.from_har_post_data(r.post_data)
            args.update(rpd.as_kwargs())
    elif r.method in (HttpMethod.PUT, HttpMethod.PATCH):
        if r.post_data:
            rpd = RequestsPostData.from_har_post_data(r.post_data)
            args.update(rpd.as_kwargs())

        args.setdefault("params", py.Literal([]))
        cast(py.Literal, args["params"]).value.extend(
            _params_from_name_value_dicts(
                [dataclasses.asdict(q) for q in r.query]))
    elif r.method not in NOOP_HTTP_METHODS:
        raise ValueError(f"unsupported HTTP method: {r.method!r}")

    method = r.method.name.lower()
    return py.FunctionCall(name=f"self.client.{method}", named_args=args)
Ejemplo n.º 7
0
 def test_it_supports_json_post_requests(self):
     url = "http://abc.de"
     r = Request(
         timestamp=MagicMock(),
         method=HttpMethod.POST,
         url=urlparse(url),
         har_entry={"entry": "data"},
         headers={"a": "b"},
         post_data={
             "mimeType": "application/json",
             "params": [{"name": "x", "value": "y"}],
             "text": """{"z": 7}""",
         },
     )
     assert req_to_expr(r) == py.FunctionCall(
         name="self.client.post",
         named_args={
             "url": py.Literal(url),
             "name": py.Literal(url),
             "headers": py.Literal({"a": "b"}),
             "timeout": py.Literal(TIMEOUT),
             "allow_redirects": py.Literal(False),
             "json": py.Literal({"z": 7}),
             "params": py.Literal([(b"x", b"y")]),
         },
     )
Ejemplo n.º 8
0
 def test_it_supports_urlencoded_post_requests(self):
     url = "http://abc.de"
     r = LocustRequest.from_request(
         Request(
             timestamp=MagicMock(),
             method=HttpMethod.POST,
             url=urlparse(url),
             har_entry={"entry": "data"},
             headers={"a": "b"},
             post_data={
                 "mimeType": "application/x-www-form-urlencoded",
                 "params": [{"name": "x", "value": "y"}],
                 "text": "z=7",
             },
         )
     )
     assert lreq_to_expr(r) == py.FunctionCall(
         name="self.client.post",
         named_args={
             "url": py.Literal(url),
             "name": py.Literal(url),
             "headers": py.Literal({"a": "b"}),
             "timeout": py.Literal(TIMEOUT),
             "allow_redirects": py.Literal(False),
             "data": py.Literal(b"z=7"),
             "params": py.Literal([(b"x", b"y")]),
         },
     )
Ejemplo n.º 9
0
 def test_it_supports_fstring_urls(self):
     url = "http://abc.{tld}"
     r = LocustRequest(method=HttpMethod.GET, url=f"f'{url}'", headers={"a": "b"})
     assert lreq_to_expr(r) == py.FunctionCall(
         name="self.client.get",
         named_args={
             "url": py.FString(url),
             "name": py.FString(url),
             "headers": py.Literal({"a": "b"}),
             "timeout": py.Literal(TIMEOUT),
             "allow_redirects": py.Literal(False),
         },
     )
Ejemplo n.º 10
0
 def test_it_uses_the_custom_name_if_provided(self):
     url = "http://abc.de"
     name = "my-req"
     r = Request(
         name=name,
         timestamp=MagicMock(),
         method=HttpMethod.GET,
         url=urlparse(url),
         har_entry={"entry": "data"},
     )
     assert req_to_expr(r) == py.FunctionCall(
         name="self.client.get",
         named_args={
             "url": py.Literal(url),
             "name": py.Literal(name),
             "timeout": py.Literal(TIMEOUT),
             "allow_redirects": py.Literal(False),
         },
     )
Ejemplo n.º 11
0
 def test_it_supports_empty_post_requests(self):
     url = "http://abc.de"
     r = Request(
         timestamp=MagicMock(),
         method=HttpMethod.POST,
         url=urlparse(url),
         har_entry={"entry": "data"},
         headers={"a": "b"},
         post_data=None,
     )
     assert req_to_expr(r) == py.FunctionCall(
         name="self.client.post",
         named_args={
             "url": py.Literal(url),
             "name": py.Literal(url),
             "headers": py.Literal({"a": "b"}),
             "timeout": py.Literal(TIMEOUT),
             "allow_redirects": py.Literal(False),
         },
     )
Ejemplo n.º 12
0
 def test_it_supports_get_requests(self):
     url = "http://abc.de"
     r = Request(
         timestamp=MagicMock(),
         method=HttpMethod.GET,
         url=urlparse(url),
         har_entry={"entry": "data"},
         headers={"a": "b"},
         query=[QueryPair("x", "y")],  # query is currently ignored for GET
     )
     assert req_to_expr(r) == py.FunctionCall(
         name="self.client.get",
         named_args={
             "url": py.Literal(url),
             "name": py.Literal(url),
             "headers": py.Literal({"a": "b"}),
             "timeout": py.Literal(TIMEOUT),
             "allow_redirects": py.Literal(False),
         },
     )
Ejemplo n.º 13
0
 def test_it_supports_patch_requests_without_payload(self):
     url = "http://abc.de"
     r = Request(
         timestamp=MagicMock(),
         method=HttpMethod.PATCH,
         url=urlparse(url),
         har_entry={"entry": "data"},
         headers={"a": "b"},
         query=[QueryPair("c", "d")],
         post_data=None,
     )
     assert req_to_expr(r) == py.FunctionCall(
         name="self.client.patch",
         named_args={
             "url": py.Literal(url),
             "name": py.Literal(url),
             "headers": py.Literal({"a": "b"}),
             "timeout": py.Literal(TIMEOUT),
             "allow_redirects": py.Literal(False),
             "params": py.Literal([(b"c", b"d")]),
         },
     )
Ejemplo n.º 14
0
 def test_lines_for_if_elif(self, level: int):
     x = py.Line.INDENT_UNIT
     assert [
         str(l) for l in py.IfElse([
             (
                 py.BinaryOp(py.Symbol("t"), "is", py.Literal(None)),
                 [py.Assignment("t", py.Literal(1))],
             ),
             (
                 py.Literal(False),
                 [
                     py.Assignment("t", py.Literal(2)),
                     py.Standalone(py.FunctionCall("f", [py.Symbol("t")])),
                 ],
             ),
         ]).lines(level)
     ] == [
         x * level + "if t is None:",
         x * (level + 1) + "t = 1",
         x * level + "elif False:",
         x * (level + 1) + "t = 2",
         x * (level + 1) + "f(t)",
     ]
Ejemplo n.º 15
0
 def test_with_positional_args(self):
     assert str(py.FunctionCall("f", [py.Literal(2)])) == "f(2)"
Ejemplo n.º 16
0
 def test_with_no_args(self):
     assert str(py.FunctionCall("f")) == "f()"