API Testing Essentials: Assertions, Async Issues, and Flaky Test Solutions Quiz

Explore key concepts in API testing with this quiz focused on assertions, asynchronous calls, flaky test detection, and security testing basics. Strengthen your understanding of reliable, secure, and robust API test practices through practical scenarios and clear explanations.

  1. Assertions in API Testing

    What is the primary purpose of an assertion in API testing?

    1. To verify that the actual response matches expected results
    2. To record API response times for reports
    3. To display the API documentation automatically
    4. To encrypt the API payload during tests

    Explanation: Assertions are used to check that the API's actual response aligns with what is expected, ensuring the API behaves correctly. Recording response times is related to performance monitoring, not assertions. Displaying documentation and encrypting payloads address different aspects of API management and security, not response validation.

  2. Common Assertion Example

    Which of the following is a typical assertion when testing a successful API GET request?

    1. The response status code should be 200
    2. The test should timeout immediately
    3. The request body should be null
    4. The database schema must change

    Explanation: A successful GET request normally returns a 200 status code, so asserting this helps verify correct operation. Expecting immediate timeout suggests failure, not success. Requiring a null request body is not standard and database schema changes are unrelated to standard GET request assertions.

  3. Asynchronous API Calls

    Why is special handling required for asynchronous API calls in automated tests?

    1. Their responses may arrive at unpredictable times
    2. Their endpoints are always unauthorized
    3. They automatically encrypt all data
    4. Their status codes are always 400

    Explanation: Asynchronous calls can respond later than expected, so tests must wait or check for completion before asserting results. Endpoints being unauthorized or always returning 400 is incorrect and not a property of asynchronous APIs. Encryption of data is unrelated to asynchronicity.

  4. Detecting Flaky API Tests

    What characteristic most commonly signals a flaky API test?

    1. It passes or fails unpredictably without code changes
    2. It always passes regardless of input
    3. It always logs a security warning
    4. It only runs on weekends

    Explanation: Flaky tests are unreliable because their outcomes change even when the code hasn't, indicating instability or external dependencies. Always passing tests ignore failures, which is not flaky. Logging a warning or running only on weekends does not indicate test flakiness.

  5. Mitigating Flakiness

    Which solution best addresses flakiness caused by delayed API responses?

    1. Implementing retries or increasing wait times
    2. Removing all assertions
    3. Reducing input data size
    4. Adding more log statements

    Explanation: Retries and adjusted waits account for variable response times, minimizing flakiness. Removing assertions prevents useful verification, reducing test effectiveness. Reducing input data or adding logs may help diagnosis but does not directly fix the timing issue.

  6. Validating JSON Responses

    In API security testing, why should tests assert the exact structure of JSON responses?

    1. To detect unauthorized or unexpected data exposure
    2. To block all GET requests
    3. To automatically upgrade clients
    4. To trigger server restarts on error

    Explanation: By asserting JSON structure, you ensure sensitive fields are not leaked and responses follow security protocols. Blocking requests, upgrading clients automatically, or triggering server restarts are unrelated to the validation of JSON structure.

  7. Assertion Types

    Which assertion type verifies sensitive information is not present in API error messages?

    1. Negative assertion
    2. Rate limiting assertion
    3. Performance assertion
    4. Redirect assertion

    Explanation: A negative assertion checks that something should not occur, such as ensuring error messages do not reveal sensitive information. Rate limiting and performance assertions address different aspects. Redirect assertions deal with HTTP redirects, not content checks.

  8. Handling 401 Status Codes

    How should an API security test assert correct handling of unauthorized access?

    1. Verify a 401 status code is returned for invalid credentials
    2. Check that the response body is always empty
    3. Assert that no status code is returned
    4. Expect a 500 internal server error

    Explanation: A 401 code signals to the client that authentication has failed, which is expected for invalid login attempts. An empty body or lack of status code is not standard and may indicate improper error handling. A 500 error suggests a server problem, not an authentication issue.

  9. Testing for API Rate Limits

    What is the main goal of testing API rate limiting functionality?

    1. To ensure excessive requests are properly controlled
    2. To verify developer email addresses
    3. To reduce API endpoint count
    4. To automate user onboarding

    Explanation: Testing rate limits checks that too many requests are detected and limited, preventing abuse. Verifying emails, reducing endpoints, or automating onboarding do not test or enforce rate limiting policies.

  10. API Parameter Fuzzing

    Which describes a basic security testing technique that submits unexpected or random parameters to an API?

    1. Fuzzing
    2. Puzzling
    3. Spoofing
    4. Phishing

    Explanation: Fuzzing sends random or malformed data to find vulnerabilities and ensure robust handling. Puzzling is not a recognized technique. Spoofing and phishing are security attacks targeting different weaknesses, not parameter stress-testing.

  11. Testing Async Resource Updates

    A test for an API endpoint that updates a resource asynchronously must ensure what before making assertions?

    1. That the resource state change is complete
    2. That the database has been archived
    3. That all API keys are invalid
    4. That the test is run at midnight

    Explanation: Assertions must wait for async operations to complete; otherwise, assertions may check incomplete or outdated data. Archiving the database or invalidating keys does not relate to async operations. Running tests at midnight is irrelevant.

  12. Proper Error Code Handling

    Which assertion verifies the API server correctly handles requests with missing required fields?

    1. The response returns a 400 Bad Request status code
    2. The request is silently ignored
    3. The server returns a success status regardless
    4. The client is automatically disconnected

    Explanation: A 400 status informs the client the request was invalid, which is proper API behavior. Simply ignoring requests or returning success for bad requests can lead to security or usability problems. Disconnecting the client is unnecessarily harsh.

  13. Timing Issues in Flaky Tests

    Flaky API tests that rely on time delays can be improved by replacing hardcoded waits with what approach?

    1. Polling the API until the expected condition is met
    2. Doubling the wait time each test run
    3. Randomizing request order
    4. Disabling all asynchronous tests

    Explanation: Polls check for a specific outcome rather than waiting a set time, making tests more adaptable and reliable. Doubling wait times increases run duration without guarantee of stability. Randomization and disabling async tests do not solve timing issues.

  14. Sensitive Data Exposure Check

    When testing for sensitive data exposure, which type of assertion confirms the absence of secret keys in API responses?

    1. Negative content assertion
    2. Positive schema assertion
    3. Parallel test assertion
    4. Load assertion

    Explanation: Negative content assertions confirm that certain sensitive values, like secret keys, do not appear where they shouldn't. Positive schema assertions validate structure, not content. Parallel and load assertions address concurrency and performance, not data exposure.

  15. Ensuring API Idempotence

    Idempotence in API testing is verified by which of the following?

    1. Sending the same request repeatedly does not change the resource further
    2. The request always triggers a server restart
    3. The API refuses repeated identical requests
    4. The response always contains random data

    Explanation: Idempotence means repeating the same request yields the same result, an important property for safe, predictable APIs. Forcing server restarts or refusing requests is disruptive or incorrect behavior. Random responses break the principle of idempotence.

  16. Concurrency and Flaky Test Causes

    Which scenario can commonly cause flakiness when testing APIs with parallel requests?

    1. When shared test data is simultaneously modified
    2. When unique user tokens are always used
    3. When all tests are run only once
    4. When requests are sent to non-existent endpoints

    Explanation: Concurrent modifications to shared data can produce unpredictable results and flakiness. Unique tokens typically avoid data collisions. Running tests only once does not inherently cause flakiness, and targeting non-existent endpoints creates consistent errors rather than unpredictable test outcomes.