title: Flask test client using GET request instead of POST
body: <p>I have a route for only POST request and it returns json response if conditions are met. It's something like this:</p>
  
  <pre><code>@app.route('/panel', methods=['POST'])
  def post_panel():
      # Check for conditions and database operations
      return jsonify({"message": "Panel added to database!"
                      "success": 1})
  </code></pre>
  
  <p>I am using <a href="https://github.com/kennethreitz/flask-sslify" rel="nofollow noreferrer">flask-sslify</a> to force http requests to https.</p>
  
  <p>I am testing this route with Flask test client and unittest. The test function is similar to following: </p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Output is exactly below:</p>
  
  <pre><code>test_post_panel_with_good_data (tests.test_api_2_0.TestAPI2_0) ... b'&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"&gt;\n&lt;title&gt;405 Method Not Allowed&lt;/title&gt;\n&lt;h1&gt;Method Not Allowed&lt;/h1&gt;\n&lt;p&gt;The method is not allowed for the requested URL.&lt;/p&gt;\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
  </code></pre>
  
  <p>I am getting an error that Method is not allowed in that route. 
  If I specify GET as a method (<code>methods=['GET', 'POST']</code>) 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?</p>
  
  <p><strong>Update:</strong> </p>
  
  <p>If do it like this:</p>
  
  <pre><code>@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"})
  </code></pre>
  
  <p>I get output like this:</p>
  
  <pre><code>test_post_panel_with_good_data (tests.test_api_2_0.TestAPI2_0) ... b'{\n  "message": "GET request"\n}\n'
  </code></pre>