最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

flask - pytest failing to post to data, body is always empty - Stack Overflow

programmeradmin6浏览0评论

I am new to pytest and testing in general. I am running into issues with getting the data to the route. The content is empty.

pytest
conftest.py
@pytest.fixture()
  def app():
  app = create_app()
  app.config['WTF_CSRF_ENABLED'] = False

  yield app

  @pytest.fixture()
    def client(app):
    return app.test_client()


test_project.py
def test_registration(client,app):
headers = {"Content-Type":"application/json"}

registerResponse = client.get("/register/")
html = registerResponse.get_data(as_text=True)

csrf_token = parse_form(html)

createResponseRaw = client.post("/create/", data = {"fullName":"George Washington","displayName":"gwashington","csrf_token":csrf_token},headers = {"Content-Type":"application/json"}) 
createResponse = return_response(createResponseRaw)
print(createResponse)  

The post occurs to the route

@appBP.route('/create/',methods=['POST'])
def create():
   msg = {"results":""}
   print(request.method) #POST
   print(request.form) #ImmutableMultiDict([])
   print(request.headers.get('Content-Type')) #application/json
   data = request.get_json()

The error that comes back is 400 Bad Request the browser or proxy sent a request that this server could not understand

When I print request.form or request.json there is no data. I am sending the csrf_token, however I also disabled it so it is not needed.

I am new to pytest and testing in general. I am running into issues with getting the data to the route. The content is empty.

pytest
conftest.py
@pytest.fixture()
  def app():
  app = create_app()
  app.config['WTF_CSRF_ENABLED'] = False

  yield app

  @pytest.fixture()
    def client(app):
    return app.test_client()


test_project.py
def test_registration(client,app):
headers = {"Content-Type":"application/json"}

registerResponse = client.get("/register/")
html = registerResponse.get_data(as_text=True)

csrf_token = parse_form(html)

createResponseRaw = client.post("/create/", data = {"fullName":"George Washington","displayName":"gwashington","csrf_token":csrf_token},headers = {"Content-Type":"application/json"}) 
createResponse = return_response(createResponseRaw)
print(createResponse)  

The post occurs to the route

@appBP.route('/create/',methods=['POST'])
def create():
   msg = {"results":""}
   print(request.method) #POST
   print(request.form) #ImmutableMultiDict([])
   print(request.headers.get('Content-Type')) #application/json
   data = request.get_json()

The error that comes back is 400 Bad Request the browser or proxy sent a request that this server could not understand

When I print request.form or request.json there is no data. I am sending the csrf_token, however I also disabled it so it is not needed.

Share Improve this question edited yesterday newdeveloper asked yesterday newdevelopernewdeveloper 7027 silver badges21 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

It appears that caching is a big problem in pytest. I needed to run the command with --cache-clear to fix my issues. pytest test_project.py -v -s --cache-clear

发布评论

评论列表(0)

  1. 暂无评论