Skip to content

Commit

Permalink
update plugin example
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberw committed Dec 17, 2023
1 parent fbb58b6 commit 76b3516
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 48 deletions.
33 changes: 21 additions & 12 deletions har2locust/extra_plugins/plugin_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ def visit_FunctionDef(self, node: FunctionDef) -> FunctionDef:
for i in range(len(node.body)):
try:
if node.body[i].items[0].context_expr.args[1].value == "/player/1/authenticate/testlogin": # type: ignore
node.body[i] = parse("self.auth()").body[0]
block = parse(
"""
self.customer = next(self.customer_iterator)
self.auth()
"""
)
node.body[i] = block.body[0]
# yea, the next line modifies what we're iterating over so we'll miss the last element, which is ugly
node.body.insert(i + 1, block.body[1])
# if node.body[i].items[0].context_expr.args[1].value == "/wager/9/wagers": # type: ignore
# json_param = [
# kw_arg.value
Expand All @@ -51,17 +59,18 @@ def visit_FunctionDef(self, node: FunctionDef) -> FunctionDef:
except:
pass

# wrap the entire task function body in a with-block.
if node.name == "t":
with_block = parse(
f"""
with self.reader.user() as self.customer:
pass
"""
).body[0]
assert isinstance(with_block, With), with_block
with_block.body = node.body
node.body = [with_block]
# Old school
# # wrap the entire task function body in a with-block.
# if node.name == "t":
# with_block = parse(
# f"""
# with self.reader.user() as self.customer:
# pass
# """
# ).body[0]
# assert isinstance(with_block, With), with_block
# with_block.body = node.body
# node.body = [with_block]
self.generic_visit(node)
return node

Expand Down
70 changes: 34 additions & 36 deletions tests/outputs/login_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,40 @@ class login(RestUser):

@task
def t(self):
with self.reader.user() as self.customer:
self.auth()
with self.rest_(
"GET",
"/player/1/customizedsettings",
headers={"accept": "application/json, text/javascript, */*; q=0.01"},
) as resp:
pass
with self.client.request(
"GET",
"https://spela.test3.svenskaspel.se/",
headers={
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
},
catch_response=True,
) as resp:
pass
with self.client.request(
"GET",
"https://spela.test3.svenskaspel.se/logga-in/uppdaterade-villkor?returnUrl=%2F",
headers={
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
},
catch_response=True,
) as resp:
pass
with self.rest(
"POST", "/player/1/terms", headers={"accept": "application/json, text/javascript, */*; q=0.01"}, json={}
) as resp:
pass
with self.rest_(
"GET",
"/player/1/info?include=accountBalance",
headers={"accept": "application/json, text/javascript, */*; q=0.01"},
) as resp:
pass
self.customer = next(self.customer_iterator)
self.auth()
with self.rest_(
"GET", "/player/1/customizedsettings", headers={"accept": "application/json, text/javascript, */*; q=0.01"}
) as resp:
pass
with self.client.request(
"GET",
"https://spela.test3.svenskaspel.se/",
headers={
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
},
catch_response=True,
) as resp:
pass
with self.client.request(
"GET",
"https://spela.test3.svenskaspel.se/logga-in/uppdaterade-villkor?returnUrl=%2F",
headers={
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
},
catch_response=True,
) as resp:
pass
with self.rest(
"POST", "/player/1/terms", headers={"accept": "application/json, text/javascript, */*; q=0.01"}, json={}
) as resp:
pass
with self.rest_(
"GET",
"/player/1/info?include=accountBalance",
headers={"accept": "application/json, text/javascript, */*; q=0.01"},
) as resp:
pass


if __name__ == "__main__":
Expand Down

0 comments on commit 76b3516

Please sign in to comment.