0
0
Flaskframework~10 mins

Testing forms and POST data in Flask - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to send a POST request with form data using Flask's test client.

Flask
with app.test_client() as client:
    response = client.post('/submit', data=[1])
    assert response.status_code == 200
Drag options to blanks, or click blank then click option'
A['name', 'Alice']
B'name=Alice'
CNone
D{'name': 'Alice'}
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a list instead of a dictionary
Passing a string instead of a dictionary
2fill in blank
medium

Complete the code to check the response contains the submitted form value.

Flask
with app.test_client() as client:
    response = client.post('/submit', data={'username': 'bob'})
    assert [1] in response.get_data(as_text=True)
Drag options to blanks, or click blank then click option'
A'form'
B'bob'
C'submit'
D'username'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for the field name instead of the value
Not converting response data to text
3fill in blank
hard

Fix the error in the test code to correctly simulate a form POST with Flask test client.

Flask
response = client.post('/login', [1]='user=admin&pass=1234')
Drag options to blanks, or click blank then click option'
Adata
Bjson
Cquery_string
Dheaders
Attempts:
3 left
💡 Hint
Common Mistakes
Using json parameter for form data
Using query_string for POST data
4fill in blank
hard

Fill both blanks to send form data and check the response status code.

Flask
with app.test_client() as client:
    response = client.post('/register', data=[1])
    assert response.status_code == [2]
Drag options to blanks, or click blank then click option'
A{'email': 'test@example.com'}
B{'email': 'user@example.com'}
C200
D201
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong status code
Passing form data as a string
5fill in blank
hard

Fill all three blanks to test a form POST and verify the response contains the submitted username.

Flask
with app.test_client() as client:
    response = client.post('/profile', data=[1])
    content = response.get_data(as_text=True)
    assert [2] in content
    assert response.status_code == [3]
Drag options to blanks, or click blank then click option'
A{'username': 'charlie'}
B'charlie'
C200
D404
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for wrong string in response
Expecting wrong status code