Shubham Sharma
Shubham Sharma

Reputation: 195

how to make a fake post request using unittest module with cookies and data?

I have an endpoint like this:-

@app.route('/name', methods=['POST'])
@limiter.limit("2000/day;300/hour;5/minute", key_func = get_uid_from_request)
@authenticate
def post(user):

How do I make a fake post request using unittest module?

Upvotes: 2

Views: 3790

Answers (2)

Serdmanczyk
Serdmanczyk

Reputation: 1224

You could do like already suggested, and do an acceptance test against a running test instance with real post requests.

You can also check out Flasks documentation for testing at http://flask.pocoo.org/docs/0.10/testing/ which demonstrates how to do unit tests in which you can mock the incoming requests and test results.

Upvotes: 0

Brendan Abel
Brendan Abel

Reputation: 37539

Well, you can make an actual post request

import requests

def test_post():
    resp = requests.post('http://localhost/name', 
                         data={'arg': 'value'}, 
                         cookies={'from-my': 'browser'})
    assert resp.status_code == 200

I would recommend using py.test instead of unittest, but if you must use unittest

class TestPost(unittest.TestCase):

    def test_post(self):
        resp = requests.post('http://localhost/name')
        self.assertEqual(resp.status_code, 200)

Upvotes: 1

Related Questions