I have a Flask web app written and writing testcases for the same using unittest. Below is how it handles the signup
route
server.py
@app.route('/signup', methods=['GET', 'POST'])
def signup():
if current_user.__dict__.get('id', None):
return render_template('login.html')
if request.method == 'GET':
return render_template('signup.html')
else:
data = request.get_json()
username = data.get('username', '').lower()
password = data.get('password', '')
client = data.get('client', '')['name']
confirm_password = data.get('confirm_password', '')
Inside the test file, there are helper methods to signup
test_unittesting.py
def signup(self, username, password, confirm, client):
return self.app.post(
'/signup',
data=json.dumps(dict(username=username, password=password, confirm=confirm, client=client)),
follow_redirects=True
)
But it does not seem to be working. request.get_json()
returns empty. The parameters are present in request.get_data()
and not in request.get_json()
. I am unable to change the helper method to support the method defined for the route. What must I change in the test file to get it working as request.get_json()
?