0
0
Rest APIprogramming~20 mins

Why testing validates contracts in Rest API - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
API Contract Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output when the API returns unexpected data?

Consider a REST API client that expects a JSON response with a status field set to success. The client tests the response to validate the contract.

What will be the output of the following code if the API returns {"status": "error", "message": "Invalid request"}?

Rest API
response = {"status": "error", "message": "Invalid request"}
if response.get("status") == "success":
    print("Contract validated: success")
else:
    print("Contract violation detected")
AContract violation detected
BContract validated: success
CKeyError exception
DNo output
Attempts:
2 left
💡 Hint

Check the value of the status key in the response dictionary.

🧠 Conceptual
intermediate
2:00remaining
Why is testing important for API contracts?

Which of the following best explains why testing validates API contracts?

ATesting guarantees the API will never have bugs.
BTesting improves the API server's hardware performance.
CTesting ensures the API implementation matches the agreed request and response formats.
DTesting replaces the need for documentation.
Attempts:
2 left
💡 Hint

Think about what a contract means in API communication.

🔧 Debug
advanced
2:00remaining
Identify the error in this API contract test

The following test checks if the API response contains a data field with a list. What error will this code produce if the API returns {"data": null}?

Rest API
response = {"data": None}
if isinstance(response["data"], list):
    print("Valid data list")
else:
    print("Invalid data format")
AInvalid data format
BKeyError exception
CValid data list
DTypeError exception
Attempts:
2 left
💡 Hint

Check the type of response["data"] when it is None.

📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in this API test code?

Which of the following code snippets will cause a syntax error when testing an API response?

Aif 'status' in response: print('OK')
Bif response.get('status') == 'success': print('OK')
Cif response.get('status') != 'error': print('OK')
Dif response['status'] = 'success': print('OK')
Attempts:
2 left
💡 Hint

Look for the assignment operator used inside the if condition.

🚀 Application
expert
3:00remaining
How many contract violations are detected in this batch test?

Given the following list of API responses, how many violate the contract that requires status to be success?

responses = [
  {"status": "success", "data": [1,2]},
  {"status": "error", "message": "fail"},
  {"status": "success", "data": []},
  {"status": "pending"}
]
Rest API
responses = [
  {"status": "success", "data": [1,2]},
  {"status": "error", "message": "fail"},
  {"status": "success", "data": []},
  {"status": "pending"}
]
violations = 0
for r in responses:
    if r.get("status") != "success":
        violations += 1
print(violations)
A1
B2
C3
D4
Attempts:
2 left
💡 Hint

Count how many responses have a status not equal to success.