简体   繁体   中英

Flask test client using GET request instead of POST

I have a route for only POST request and it returns json response if conditions are met. It's something like this:

@app.route('/panel', methods=['POST'])
def post_panel():
    # Check for conditions and database operations
    return jsonify({"message": "Panel added to database!"
                    "success": 1})

I am using flask-sslify to force http requests to https.

I am testing this route with Flask test client and unittest. The test function is similar to following:

class TestAPI2_0(unittest.TestCase):
    def setUp(self):
    self.app = create_app('testing')
    self.app_context = self.app.app_context()
    self.app_context.push()
    db.create_all()
    create_fake_data(db)
    self.client = self.app.test_client()

    def tearDown(self):
        ....

    def test_post_panel_with_good_data(self):    
        # data
        r = self.client.post('/panel',
                            data=json.dumps(data),
                            follow_redirects=True)  
        print(r.data)      
        self.assertEqual(r.status_code, 200)

Output is exactly below:

test_post_panel_with_good_data (tests.test_api_2_0.TestAPI2_0) ... b'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">\n<title>405 Method Not Allowed</title>\n<h1>Method Not Allowed</h1>\n<p>The method is not allowed for the requested URL.</p>\n'


======================================================================
FAIL: test_post_panel_with_good_data (tests.test_api_2_0.TestAPI2_0)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/tanjibpa/work/craftr-master/tests/test_api_2_0.py", line 110, in test_post_panel_with_good_data
    self.assertEqual(r.status_code, 200)
AssertionError: 405 != 200

I am getting an error that Method is not allowed in that route. If I specify GET as a method ( methods=['GET', 'POST'] ) for the route test seems to work. But why test client is making a GET request? Is there any way around rather than specifying a GET request for the route?

Update:

If do it like this:

@app.route('/panel', methods=['GET', 'POST'])
def post_panel():
    if request.method == 'POST':
        # Check for conditions and database operations
        return jsonify({"message": "Panel added to database!"
                        "success": 1})
    return jsonify({"message": "GET request"})

I get output like this:

test_post_panel_with_good_data (tests.test_api_2_0.TestAPI2_0) ... b'{\n  "message": "GET request"\n}\n'

I found out what was causing the GET request within flask test client. I am using flask-sslify to force http requests to https. Somehow flask-sslify is enforcing a GET request although test client is specified with other kind of requests (POST, PUT, DELETE...).

So, If I disable sslify during testing flask test client works as it should.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM