tarekziade / molotov

Load Testing Tool
Apache License 2.0
450 stars 31 forks source link

MultipartWriter body breaks verbose printing of requests #121

Closed KoalaTea closed 3 years ago

KoalaTea commented 5 years ago

When using multipart writer from aiohttp with -vv the output of requests errors out with

TypeError('decoding to str: need a bytes-like object, NoneType found')
  File "/home/rwhittier/.virtualenvs/rampie/lib/python3.7/site-packages/molotov/listeners.py", line 118, in send_event
    await listener(event, **options)
  File "/home/rwhittier/.virtualenvs/rampie/lib/python3.7/site-packages/molotov/listeners.py", line 15, in __call__
    await attr(**options)
  File "/home/rwhittier/.virtualenvs/rampie/lib/python3.7/site-packages/molotov/listeners.py", line 55, in on_sending_request
    raw += '\n\n' + self._body2str(request.body) + '\n'
  File "/home/rwhittier/.virtualenvs/rampie/lib/python3.7/site-packages/molotov/listeners.py", line 37, in _body2str
    body = str(body, 'utf8')

example scenario

    csrf_token = "d19599a81c7dfb33390937f871b870a249019a650961b3f2396c84698a8e4e5d"
    cookies = {
        "csrftoken": csrf_token,
        "dtk": "1d8jvjcvq0000901",
        "djdt": "hide",
        "sessionid": "mt3asqjzf8fsk6mlhplz6uyiahw6es23",
    }
    boundary = "----WebKitFormBoundaryFTEq5swUadPoHOyD"
    headers = {
        "X-CSRFToken": csrf_token,
        # "Content-Type": "multipart/form-data; boundary={}".format(boundary),
    }
    with aiohttp.MultipartWriter("form-data", boundary=boundary) as mpwriter:
        mpwriter.append(
            data,
            {
                "Content-Disposition": 'form-data; name="json"; filename="blob"',
                "Content-Type": "application/json",
            },
        )
        async with session.post(
            _API + "/api/submit_form/", data=mpwriter, headers=headers, cookies=cookies
        ) as resp:
            res = await resp.json()

this is due to multipart writer setting value to None explicitly when calling super() https://github.com/aio-libs/aiohttp/blob/master/aiohttp/multipart.py#L781 https://github.com/aio-libs/aiohttp/blob/master/aiohttp/payload.py#L128 Which the send event expects to be a value https://github.com/loads/molotov/blob/master/molotov/listeners.py#L30

It might make sense to have a writer instead to use to set the value using https://github.com/aio-libs/aiohttp/blob/master/aiohttp/payload.py#L196

tarekziade commented 3 years ago

Hey @KoalaTea - would you be interested to contribute this feature? I have a bit of time to work on molotov again