2017-11-11 1 views
1

POST 요청 만위한 경로가 있으며 조건이 충족되면 json 응답을 반환합니다. 다음과 같은 내용입니다.POST 대신 GET 요청을 사용하는 플라스크 테스트 클라이언트

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

HTTP 요청을 https로 보내려는 경우 flask-sslify입니다.

플라스크 테스트 클라이언트 및 unittest를 사용하여이 경로를 테스트하고 있습니다. 테스트 기능은 다음과 유사합니다

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) 

출력은 정확히 다음과 같습니다 :

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 

나는 방법은 그 길에서 허용되지 않는다는 오류를 얻고있다. 경로 테스트를 위해 GET을 메서드 (methods=['GET', 'POST'])로 지정하면 제대로 작동하는 것 같습니다. 하지만 왜 테스트 클라이언트가 GET 요청을하고 있습니까? 경로에 대한 GET 요청을 지정하는 대신 주변에 어떤 방법이 있습니까? 이런 식으로 할 경우

:

업데이트

@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"}) 

나는 다음과 같은 출력을 얻을 : 나는 플라스크 테스트 내에서 GET 요청을 일으키는 것을 발견

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

코드에서 나는'/ panel'로 정의 된 경로를 보았고, 테스트 메소드에서는'/ api/panel'을 호출했습니다. 그게 옳은가요? – janos

+0

예. 맞습니다. 이것은 API 청사진의 경로 프리픽스 때문입니다. – tanjibpa

+0

문제점을 재현 할 수 없습니다. 'client.post'는 post 요청을합니다. [편집]을 클릭하여 [mcve]를 포함하십시오. – davidism

답변

0

고객. flask-sslify를 사용하여 HTTP 요청을 https로 보내고 있습니다. 테스트 클라이언트가 다른 종류의 요청 (POST, PUT, DELETE ...)으로 지정되어 있지만 여하튼 flask-sslify가 GET 요청을 시행하고 있습니다.

따라서 테스트하는 동안 sslify를 비활성화하면 플라스크 테스트 클라이언트가 제대로 작동합니다.

관련 문제