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 pass POST params in test? #694

Closed
asonnenschein opened this issue Jan 28, 2016 · 2 comments
Closed

How to pass POST params in test? #694

asonnenschein opened this issue Jan 28, 2016 · 2 comments

Comments

@asonnenschein
Copy link

Howdy! I'm building a REST API w/ falcon and am trying to write a POST test that spoofs an HTML form and can't quite figure out what I'm doing wrong here.

In this example -

class TestBase(unittest.TestCase):

    def setUp(self):
        self.app = server.server
        self.srmock = falcon.testing.StartResponseMock()

    def simulate_request(self, path, **kwargs):
        env = falcon.testing.create_environ(path=path, **kwargs)
        return self.app(env, self.srmock)

    def simulate_post(self, *args, **kwargs):
        kwargs['method'] = 'POST'
        return self.simulate_request(*args, **kwargs)


class TestRegister(TestBase):

    def setUp(self):
        super(TestRegister, self).setUp()
        self.entry_path = '/register/'

    def tearDown(self):
        super(TestRegister, self).tearDown()

    def test_register_user(self):
        body = {
            'username': 'new_user',
            'email': 'email',
            'password': 'password',
            'location': 'Tucson, AZ'
        }
        self.simulate_post(self.entry_path, body=body)
        self.assertEqual(self.srmock.status, falcon.HTTP_201)

The test fails with this error:

TypeError: a bytes-like object is required, not 'dict'

If I pass the body in as a string literal instead of a dict(), I don't get any errors but the request doesn't parse out the body as params - I've got to parse the body manually via req.stream.read().

@asonnenschein asonnenschein changed the title How to spoof POST params in test? How to pass POST params in test? Jan 28, 2016
@asonnenschein
Copy link
Author

OK, figured this one out - I had a look at the pytest-falcon code by @yohanboniface and figured out what I was doing wrong here. I didn't add the correct 'Content-Type' header and I didn't encode the body!

from urllib.parse import urlencode

class TestRegister(TestBase):

    def setUp(self):
        super(TestRegister, self).setUp()
        self.entry_path = '/register/'

    def tearDown(self):
        super(TestRegister, self).tearDown()

    def test_register_user(self):
        body = urlencode({
            'username': 'new_user',
            'email': 'email',
            'password': 'password',
            'location': 'Tucson, AZ'
        })
        headers = {"Content-Type": "application/x-www-form-urlencoded"}
        self.simulate_post(self.entry_path, body=body, headers=headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_201)

@kiddten
Copy link

kiddten commented Aug 16, 2016

Thanks. That is helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants