Skip to content

Commit

Permalink
Merge pull request #17 from ivanpuchalka/body-string-literals
Browse files Browse the repository at this point in the history
Fix payload string generation
  • Loading branch information
cyberw authored May 5, 2024
2 parents 0c847e6 + 790f7df commit fb31f98
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 3 deletions.
8 changes: 8 additions & 0 deletions har2locust/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ def render(name: str, values: dict) -> str:
return py


@entriesprocessor_with_args
def transform_payload_strings(entries: list[dict], args):
for entry in entries:
request = entry["request"]
if "postData" in request and "text" in request["postData"] and request["fname"] != "rest":
request["postData"]["text"] = ast.unparse(ast.Constant(value=request["postData"]["text"]))


# Generate a valid identifier (https://docs.python.org/3.8/reference/lexical_analysis.html#identifiers) by replacing
# invalid characters with "_".
def generate_class_name(file_name: str) -> str:
Expand Down
2 changes: 1 addition & 1 deletion har2locust/locust.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class {{class_name}}(FastHttpUser):
{%- endfor %}}
{%- endif -%}
{%- if request.postData -%}
, data='{{ request.postData.text }}'
, data={{ request.postData.text }}
{%- endif -%}
{%- for (key, value) in request.extraparams -%}
,{{ key }}={{ value }}
Expand Down
115 changes: 115 additions & 0 deletions tests/inputs/string-problematic-characters.har
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
{
"log": {
"creator": {
"name": "Advanced REST Client",
"version": "Unknown"
},
"version": "1.2",
"entries": [
{
"startedDateTime": "2024-05-01T01:13:50.911Z",
"time": 601,
"cache": {
"afterRequest": null,
"beforeRequest": null,
"comment": "This application does not support caching."
},
"timings": {
"blocked": 0,
"connect": 42,
"receive": 5,
"send": 1,
"wait": 514,
"dns": 38,
"ssl": 1
},
"request": {
"method": "PATCH",
"url": "https://reqres.in/api/users/2",
"httpVersion": "HTTP/1.1",
"headers": [],
"bodySize": 145,
"headersSize": 0,
"cookies": [],
"queryString": [],
"postData": {
"mimeType": null,
"text": "------WebKitFormBoundaryaVpJHMwCHF5PHRS7\r\nContent-Disposition: form-data; name=\"name\"\r\n\r\n'\\'\\r\\n\"\\\"\r\n------WebKitFormBoundaryaVpJHMwCHF5PHRS7--\r\n"
}
},
"response": {
"status": 200,
"statusText": "OK",
"httpVersion": "HTTP/1.1",
"cookies": [],
"headers": [
{
"name": "Date",
"value": "Wed, 01 May 2024 01:13:51 GMT"
},
{
"name": "Content-Type",
"value": "application/json; charset=utf-8"
},
{
"name": "Content-Length",
"value": "40"
},
{
"name": "Connection",
"value": "keep-alive"
},
{
"name": "Report-To",
"value": "{\"group\":\"heroku-nel\",\"max_age\":3600,\"endpoints\":[{\"url\":\"https://nel.heroku.com/reports?ts=1714526031&sid=c4c9725f-1ab0-44d8-820f-430df2718e11&s=5TBzx4koRZlKOSEOrxvEEhkMCohSUw2t1EzIbUtg7WI%3D\"}]}"
},
{
"name": "Reporting-Endpoints",
"value": "heroku-nel=https://nel.heroku.com/reports?ts=1714526031&sid=c4c9725f-1ab0-44d8-820f-430df2718e11&s=5TBzx4koRZlKOSEOrxvEEhkMCohSUw2t1EzIbUtg7WI%3D"
},
{
"name": "Nel",
"value": "{\"report_to\":\"heroku-nel\",\"max_age\":3600,\"success_fraction\":0.005,\"failure_fraction\":0.05,\"response_headers\":[\"Via\"]}"
},
{
"name": "X-Powered-By",
"value": "Express"
},
{
"name": "Access-Control-Allow-Origin",
"value": "*"
},
{
"name": "Etag",
"value": "W/\"28-BR3knLS0fufDvS4u4ZiopG530R8\""
},
{
"name": "Via",
"value": "1.1 vegur"
},
{
"name": "CF-Cache-Status",
"value": "DYNAMIC"
},
{
"name": "Server",
"value": "cloudflare"
},
{
"name": "CF-RAY",
"value": "87cbe94d3f921ea4-EZE"
}
],
"headersSize": 820,
"bodySize": 40,
"content": {
"mimeType": "application/json",
"text": "{\"updatedAt\":\"2024-05-01T01:13:51.297Z\"}",
"size": 40,
"encoding": "utf-8"
}
}
}
]
}
}
4 changes: 2 additions & 2 deletions tests/outputs/apple-buy-a-mac.py
Original file line number Diff line number Diff line change
Expand Up @@ -1855,7 +1855,7 @@ def t(self):
"scnt": "",
"x-csrf-token": "",
},
data='{"type":"INFO","title":"AppleAuthDebug","message":"APPLE ID : Launching AppleAuth application.{"data":{"initApp":{"startTime":1400.0300000188872}},"order":["initApp"]}","iframeId":"auth-bbfc2b43-ol01-rowz-a4jz-l79n3zhj","details":"{"pageVisibilityState":"visible"}"}',
data='{"type":"INFO","title":"AppleAuthDebug","message":"APPLE ID : Launching AppleAuth application.{\\"data\\":{\\"initApp\\":{\\"startTime\\":1400.0300000188872}},\\"order\\":[\\"initApp\\"]}","iframeId":"auth-bbfc2b43-ol01-rowz-a4jz-l79n3zhj","details":"{\\"pageVisibilityState\\":\\"visible\\"}"}',
catch_response=True,
) as resp:
pass
Expand Down Expand Up @@ -1903,7 +1903,7 @@ def t(self):
"scnt": "",
"x-csrf-token": "",
},
data='{"type":"INFO","title":"AppleAuthPerf","message":"APPLE ID : TTI {"data":{"initApp":{"startTime":1400.0300000188872},"loadAuthComponent":{"startTime":2087.4300000141375},"startAppToTTI":{"duration":686.1000000208151}},"order":["initApp","loadAuthComponent","startAppToTTI"]}","iframeId":"auth-bbfc2b43-ol01-rowz-a4jz-l79n3zhj","details":"{"pageVisibilityState":"visible"}"}',
data='{"type":"INFO","title":"AppleAuthPerf","message":"APPLE ID : TTI {\\"data\\":{\\"initApp\\":{\\"startTime\\":1400.0300000188872},\\"loadAuthComponent\\":{\\"startTime\\":2087.4300000141375},\\"startAppToTTI\\":{\\"duration\\":686.1000000208151}},\\"order\\":[\\"initApp\\",\\"loadAuthComponent\\",\\"startAppToTTI\\"]}","iframeId":"auth-bbfc2b43-ol01-rowz-a4jz-l79n3zhj","details":"{\\"pageVisibilityState\\":\\"visible\\"}"}',
catch_response=True,
) as resp:
pass
Expand Down
19 changes: 19 additions & 0 deletions tests/outputs/string-problematic-characters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from locust import FastHttpUser, run_single_user, task


class string_problematic_characters(FastHttpUser):
host = "https://reqres.in"

@task
def t(self):
with self.client.request(
"PATCH",
"/api/users/2",
data='------WebKitFormBoundaryaVpJHMwCHF5PHRS7\r\nContent-Disposition: form-data; name="name"\r\n\r\n\'\\\'\\r\\n"\\"\r\n------WebKitFormBoundaryaVpJHMwCHF5PHRS7--\r\n',
catch_response=True,
) as resp:
pass


if __name__ == "__main__":
run_single_user(string_problematic_characters)

0 comments on commit fb31f98

Please sign in to comment.