0
0
Rest APIprogramming~10 mins

Contract testing in Rest API - Interactive Code Practice

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

Complete the code to define a contract test that checks the response status code.

Rest API
def test_api_response():
    response = client.get('/api/data')
    assert response.status_code == [1]
Drag options to blanks, or click blank then click option'
A500
B404
C200
D302
Attempts:
3 left
💡 Hint
Common Mistakes
Using 404 which means Not Found
Using 500 which means Server Error
2fill in blank
medium

Complete the code to check that the response content type is JSON.

Rest API
def test_content_type():
    response = client.get('/api/data')
    assert response.headers['Content-Type'] == [1]
Drag options to blanks, or click blank then click option'
Atext/html
Bapplication/json
Ctext/plain
Dapplication/xml
Attempts:
3 left
💡 Hint
Common Mistakes
Using text/html which is for web pages
Using application/xml which is for XML data
3fill in blank
hard

Fix the error in the contract test that verifies the JSON response has a 'name' field.

Rest API
def test_response_has_name():
    response = client.get('/api/user')
    data = response.json()
    assert [1] in data
Drag options to blanks, or click blank then click option'
A'name'
B'username'
C'id'
D'email'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for 'username' instead of 'name'
Checking for 'email' which is not required
4fill in blank
hard

Fill both blanks to create a contract test that verifies the 'age' field is an integer and greater than 18.

Rest API
def test_age_field():
    response = client.get('/api/user')
    data = response.json()
    assert isinstance(data['age'], [1]) and data['age'] [2] 18
Drag options to blanks, or click blank then click option'
Aint
B>
C<
Dstr
Attempts:
3 left
💡 Hint
Common Mistakes
Using str instead of int
Using < instead of >
5fill in blank
hard

Fill all three blanks to write a contract test that filters users with 'active' status and checks their 'email' contains '@'.

Rest API
def test_active_users_email():
    response = client.get('/api/users')
    users = response.json()
    active_users = [user[1] user in users if user['status'] [2] 'active']
    assert all('[3]' in user['email'] for user in active_users)
Drag options to blanks, or click blank then click option'
A for
B==
C@
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' for status check
Missing '@' in email check
Incorrect list comprehension syntax