REST testing, contracts, auth, tools
Practise these as flashcards. One or two lines each; the ones you miss come back first.
Start freeI check for standard status codes like 200 for success, 201 for resource creation, 400 for bad requests, 401 for unauthorized, 404 for not found, and 500 for server errors. Each endpoint should return the appropriate code for the operation and error scenario.
I use schema validation tools or libraries to compare the response body against a defined JSON schema. This ensures all required fields are present, types are correct, and no unexpected fields are included.
Headers contain important metadata like content type, caching, and authentication info. Testing headers ensures the API follows protocol standards and clients can interpret responses correctly.
Negative testing involves sending invalid or unexpected inputs to the API to check if it handles errors gracefully. It helps ensure the API returns proper error codes and messages without exposing sensitive information.
I create collections in Postman, define requests, and add test scripts to validate status codes, response bodies, and headers. Collections can be run in the Postman app or via Newman in CI pipelines for automation.
REST Assured allows me to write fluent Java tests for APIs. I set up requests, define expectations for status codes and response bodies, and integrate these tests with JUnit or TestNG for automated runs in CI.
I automate token retrieval by calling the auth endpoint in setup steps, storing the token, and passing it in the Authorization header for subsequent requests. This avoids hardcoding credentials and keeps tests maintainable.
Contract testing verifies that the API provider and consumer agree on the data structure and endpoints. Pact lets me define contracts as code, run them against providers, and catch breaking changes before deployment.
I use fixtures or seed scripts to set up known data before tests, and clean up afterwards to avoid pollution. For external dependencies, I mock responses to ensure tests are repeatable and not affected by real data changes.
I send invalid requests, like missing required fields or unauthorized access, and check if the API returns the correct status code and error message. This helps ensure the API doesn't fail silently or expose sensitive details.
I test for unauthorized access, input validation, and proper error handling. For example, I try accessing protected endpoints without a token, check for SQL injection vulnerabilities, and verify sensitive data isn't exposed.
I add assertions in my test scripts to check that headers like Content-Type are set correctly, and Cache-Control is present when needed. This ensures clients can process responses and caching works as intended.