aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--mitmproxy/flow_export.py65
-rw-r--r--test/mitmproxy/test_flow_export.py29
2 files changed, 25 insertions, 69 deletions
diff --git a/mitmproxy/flow_export.py b/mitmproxy/flow_export.py
index a9fd5cde..e2d46d93 100644
--- a/mitmproxy/flow_export.py
+++ b/mitmproxy/flow_export.py
@@ -103,10 +103,10 @@ def locust_code(flow):
class UserBehavior(TaskSet):
def on_start(self):
''' on_start is called when a Locust start before any task is scheduled '''
- self.flow()
+ self.{name}()
@task()
- def flow(self):
+ def {name}(self):
url = '{url}'
{headers}{params}{data}
self.response = self.client.request(
@@ -121,10 +121,12 @@ def locust_code(flow):
task_set = UserBehavior
min_wait = 1000
max_wait = 3000
+""").strip()
- """).strip()
components = map(lambda x: quote(x, safe=""), flow.request.path_components)
+ file_name = "_".join(components)
+ name = re.sub('\W|^(?=\d)', '_', file_name)
url = flow.request.scheme + "://" + flow.request.host + "/" + "/".join(components)
args = ""
@@ -146,6 +148,7 @@ def locust_code(flow):
args += "\n data=data,"
code = code.format(
+ name=name,
url=url,
headers=headers,
params=params,
@@ -163,55 +166,9 @@ def locust_code(flow):
def locust_task(flow):
- code = dedent("""
- @task()
- def {name}(self):
- url = '{url}'
- {headers}{params}{data}
- self.response = self.client.request(
- method='{method}',
- url=url,{args}
- )
- """).strip()
-
- components = map(lambda x: quote(x, safe=""), flow.request.path_components)
- file_name = "_".join(components)
- name = re.sub('\W|^(?=\d)','_', file_name)
- url = flow.request.scheme + "://" + flow.request.host + "/" + "/".join(components)
-
- args = ""
- headers = ""
- if flow.request.headers:
- lines = [" '%s': '%s',\n" % (k, v) for k, v in flow.request.headers.fields if k.lower() not in ["host", "cookie"]]
- headers += "\n headers = {\n%s }\n" % "".join(lines)
- args += "\n headers=headers,"
-
- params = ""
- if flow.request.query:
- lines = [" '%s': '%s',\n" % (k, v) for k, v in flow.request.query]
- params = "\n params = {\n%s }\n" % "".join(lines)
- args += "\n params=params,"
+ code = locust_code(flow)
+ start_task = len(code.split('@task')[0]) - 4
+ end_task = -19 - len(code.split('### Additional')[1])
+ task_code = code[start_task:end_task]
- data = ""
- if flow.request.body:
- data = "\n data = '''%s'''\n" % flow.request.body
- args += "\n data=data,"
-
- code = code.format(
- name=name,
- url=url,
- headers=headers,
- params=params,
- data=data,
- method=flow.request.method,
- args=args,
- )
-
- host = flow.request.scheme + "://" + flow.request.host
- code = code.replace(host, "' + self.locust.host + '")
- code = code.replace(quote_plus(host), "' + quote_plus(self.locust.host) + '")
- code = code.replace(quote(host), "' + quote(self.locust.host) + '")
-
- code = "\n".join(" " + i for i in code.splitlines())
-
- return code
+ return task_code
diff --git a/test/mitmproxy/test_flow_export.py b/test/mitmproxy/test_flow_export.py
index 6c9a6756..d937135f 100644
--- a/test/mitmproxy/test_flow_export.py
+++ b/test/mitmproxy/test_flow_export.py
@@ -188,10 +188,10 @@ from locust import HttpLocust, TaskSet, task
class UserBehavior(TaskSet):
def on_start(self):
''' on_start is called when a Locust start before any task is scheduled '''
- self.flow()
+ self.path()
@task()
- def flow(self):
+ def path(self):
url = '' + self.locust.host + '/path'
headers = {
@@ -226,10 +226,10 @@ from locust import HttpLocust, TaskSet, task
class UserBehavior(TaskSet):
def on_start(self):
''' on_start is called when a Locust start before any task is scheduled '''
- self.flow()
+ self.path()
@task()
- def flow(self):
+ def path(self):
url = '' + self.locust.host + '/path'
data = '''content'''
@@ -261,10 +261,10 @@ from locust import HttpLocust, TaskSet, task
class UserBehavior(TaskSet):
def on_start(self):
''' on_start is called when a Locust start before any task is scheduled '''
- self.flow()
+ self.path()
@task()
- def flow(self):
+ def path(self):
url = '' + self.locust.host + '/path'
headers = {
@@ -312,13 +312,13 @@ class TestExportLocustTask():
'header': 'qvalue',
'content-length': '7',
}
-
+
self.response = self.client.request(
method='GET',
url=url,
headers=headers,
)
- """.strip()
+ """.strip() + '\n'
assert flow_export.locust_task(flow) == result
@@ -330,14 +330,13 @@ class TestExportLocustTask():
url = '' + self.locust.host + '/path'
data = '''content'''
-
+
self.response = self.client.request(
method='POST',
url=url,
data=data,
)
-
- """.strip()
+ """.strip() + '\n'
assert flow_export.locust_task(flow) == result
@@ -353,13 +352,13 @@ class TestExportLocustTask():
'header': 'qvalue',
'content-length': '7',
}
-
+
params = {
'query': 'param',
}
-
+
data = '''content'''
-
+
self.response = self.client.request(
method='PATCH',
url=url,
@@ -367,6 +366,6 @@ class TestExportLocustTask():
params=params,
data=data,
)
- """.strip()
+ """.strip() + '\n'
assert flow_export.locust_task(flow) == result
321' href='#n321'>321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603