0
0
Flaskframework~10 mins

Test client for request simulation 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 create a Flask test client instance.

Flask
from flask import Flask
app = Flask(__name__)
test_client = app.[1]()
Drag options to blanks, or click blank then click option'
Atest_client.post
Btest_client
Ctest_client.get
Dtest_client()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'test_client' without parentheses creates a reference, not an instance.
Trying to call 'get' or 'post' directly on the app instead of on the test client.
2fill in blank
medium

Complete the code to send a GET request to '/' using the Flask test client.

Flask
response = test_client.[1]('/')
Drag options to blanks, or click blank then click option'
Adelete
Bpost
Cget
Dput
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'post' instead of 'get' for a GET request.
Using 'put' or 'delete' which are for other HTTP methods.
3fill in blank
hard

Fix the error in accessing the response data as text.

Flask
data = response.[1]
Drag options to blanks, or click blank then click option'
Atext
Bjson
Cdata
Dcontent
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'data' which is bytes, not string.
Using 'json' without parsing the response.
4fill in blank
hard

Fill both blanks to send a POST request with JSON data using the test client.

Flask
response = test_client.[1]('/submit', json=[2])
Drag options to blanks, or click blank then click option'
Apost
B{'name': 'Alice'}
C{'age': 30}
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' instead of 'post' for sending data.
Passing JSON data as a string instead of a dictionary.
5fill in blank
hard

Fill all three blanks to check the response status code and content type.

Flask
assert response.status_code == [1]
assert response.headers['Content-Type'] == '[2]'
assert 'text/html' in response.headers.get('[3]', '')
Drag options to blanks, or click blank then click option'
A200
Btext/html
CContent-Type
D404
Attempts:
3 left
💡 Hint
Common Mistakes
Using 404 status code which means not found.
Using wrong header key names or values.