Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use make_mocked_request to make a post request with body? #2908

Closed
fy0 opened this issue Apr 3, 2018 · 3 comments
Closed

How to use make_mocked_request to make a post request with body? #2908

fy0 opened this issue Apr 3, 2018 · 3 comments
Labels

Comments

@fy0
Copy link

fy0 commented Apr 3, 2018

Long story short

I tried this:

async def test_post():
    protocol = mock.Mock()
    form = FormData()
    form.add_field('id', '123')
    form = form()

    headers = {'Content-Type': form.content_type}
    request = make_mocked_request('POST', '/api/test', headers=headers,
                                  protocol=protocol, payload=form, app=app)
    print(await request.post())

Expected behaviour

Get {'id': '123'} as result of await request.post().

Actual behaviour

self = <Request POST /api/test >

    async def read(self):
        """Read request body if present.

            Returns bytes object with full request content.
            """
        if self._read_bytes is None:
            body = bytearray()
            while True:
>               chunk = await self._payload.readany()
E               AttributeError: 'BytesPayload' object has no attribute 'readany'

Steps to reproduce

Just run the code

Your environment

Win10, python3.6, aiohttp 3.1.0

So hard to find a document or example, thanks for help.

@asvetlov
Copy link
Member

asvetlov commented Apr 3, 2018

Payload is a StreamReader instance, not a FormData.

@fy0
Copy link
Author

fy0 commented Apr 4, 2018

@asvetlov Seems no example, even in tests. Finally I used post cache for request.

request._post = MultiDict(info='a')

Well, at least it works.

@lock
Copy link

lock bot commented Oct 28, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a [new issue] for related bugs.
If you feel like there's important points made in this discussion, please include those exceprts into that [new issue].
[new issue]: https://github.com/aio-libs/aiohttp/issues/new

@lock lock bot added the outdated label Oct 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants