Software Testing

Sample Restful Web services Test cases

Sample RESTful Web Services Test Cases

RESTful Web Services are widely used for communication between web applications, mobile applications, microservices, and external systems. Testing REST APIs is essential to verify that the services are reliable, secure, accurate, and capable of handling different types of requests and responses.

A good REST API test strategy should validate HTTP methods, request parameters, headers, authentication, response status codes, response data, error handling, performance, and security.

What is RESTful Web Services Testing?

RESTful Web Services Testing is the process of validating REST APIs to ensure that they behave according to the defined requirements and API specifications.

For example, consider an API used to retrieve customer information:

GET https://example.com/api/customers/101

The API test should verify whether the correct customer information is returned, the response status code is correct, the response structure is valid, and invalid requests are handled properly.

Common HTTP Methods Used in REST APIs

HTTP Method Purpose Typical Operation
GET Retrieve data Get customer details
POST Create data Create a new customer
PUT Update or replace data Update customer details
PATCH Partially update data Update customer email
DELETE Delete data Delete a customer

Sample REST API Test Case Structure

A REST API test case generally contains the following information:

Field Description
Test Case ID Unique identifier for the test case
Test Scenario What functionality is being tested
Endpoint API URL being tested
HTTP Method GET, POST, PUT, PATCH, or DELETE
Test Data Request parameters, headers, or body
Expected Result Expected API behavior

Sample RESTful Web Services Test Cases

1. Verify GET API with Valid Request

Endpoint:

GET /api/customers/101

Test Case: Send a GET request with a valid customer ID.

Expected Result:

  • API should return HTTP 200 OK.
  • Correct customer information should be returned.
  • Response should be in the expected format such as JSON.

2. Verify GET API with Invalid ID

Send a GET request using a customer ID that does not exist.

GET /api/customers/99999

Expected Result: The API should return an appropriate error status such as 404 Not Found, along with a meaningful error response.

3. Verify GET API Without Mandatory ID

Remove the required customer ID from the request.

GET /api/customers/

Expected Result: The API should reject the request or return a suitable validation response.

4. Verify GET API Response Headers

Verify important response headers such as:

  • Content-Type
  • Cache-Control
  • Content-Encoding
  • Authorization-related headers where applicable

Expected Result: Response headers should contain the expected values according to the API specification.

5. Verify POST API with Valid Request Body

Endpoint:

POST /api/customers

Request Body:

{
  "name": "John Smith",
  "email": "john@example.com",
  "phone": "9876543210"
}

Expected Result:

  • Customer should be created successfully.
  • API should return the expected success status, commonly 201 Created.
  • A unique customer ID should be generated when applicable.
  • Response data should match the submitted information.

6. Verify POST API with Missing Mandatory Field

Remove a mandatory field such as email.

{
  "name": "John Smith",
  "phone": "9876543210"
}

Expected Result: API should reject the request and return an appropriate validation error, commonly 400 Bad Request.

7. Verify POST API with Invalid Data Type

Send an incorrect data type for a field.

{
  "name": "John Smith",
  "age": "ABC"
}

Expected Result: API should validate the request and return an appropriate error message.

8. Verify POST API with Duplicate Data

Attempt to create a customer using data that must be unique, such as an existing email address.

Expected Result: API should prevent duplicate records and return the expected business validation error.

9. Verify PUT API with Valid Data

Endpoint:

PUT /api/customers/101

Send valid updated customer information.

Expected Result:

  • Existing record should be updated.
  • API should return the appropriate success status.
  • Updated values should be persisted in the database.

10. Verify PUT API for Non-Existing Resource

Send a PUT request for a customer ID that does not exist.

PUT /api/customers/99999

Expected Result: API should handle the request according to the defined behavior and return an appropriate status such as 404 Not Found.

11. Verify PATCH API with Partial Update

Update only one field of an existing customer.

PATCH /api/customers/101

{
  "phone": "9123456789"
}

Expected Result: Only the specified field should be updated. Other existing customer information should remain unchanged.

12. Verify DELETE API with Valid ID

Endpoint:

DELETE /api/customers/101

Expected Result:

  • Customer should be deleted according to the API’s deletion rules.
  • API should return the appropriate success status.
  • Subsequent retrieval should confirm the expected state.

13. Verify DELETE API with Non-Existing ID

Send a DELETE request using an ID that does not exist.

Expected Result: API should return the expected status and meaningful error information without affecting other records.

Status Code Validation Test Cases

One of the most important parts of REST API testing is validating HTTP status codes.

Status Code Meaning Test Scenario
200 OK Successful GET or update operation
201 Created Successful resource creation
204 No Content Successful operation without response body
400 Bad Request Invalid request or validation failure
401 Unauthorized Missing or invalid authentication
403 Forbidden User does not have permission
404 Not Found Resource does not exist
405 Method Not Allowed Unsupported HTTP method
409 Conflict Duplicate or conflicting resource
429 Too Many Requests Rate limit exceeded
500 Internal Server Error Unexpected server-side failure

Request Validation Test Cases

Test the API using different types of request data.

  • Valid values
  • Null values
  • Empty values
  • Boundary values
  • Maximum-length values
  • Minimum-length values
  • Special characters
  • Unicode characters
  • Unexpected data types
  • Extra fields

For example, test an email field with valid, invalid, empty, duplicate, and excessively long values.

JSON Response Validation Test Cases

When an API returns JSON, validate the response structure as well as the values.

For example:

{
  "id": 101,
  "name": "John Smith",
  "email": "john@example.com"
}

Verify that:

  • Required fields are present.
  • Field names are correct.
  • Values have the expected data types.
  • Nested objects and arrays follow the expected structure.
  • Null values are handled according to the API contract.
  • Unexpected sensitive information is not exposed.

Authentication Test Cases

Authentication testing is critical for APIs that protect business or customer data.

14. Verify API with Valid Authentication Token

Send a valid token in the authorization header.

Authorization: Bearer <valid-token>

Expected Result: Request should be processed successfully when the token has the required permissions.

15. Verify API Without Authentication Token

Remove the authorization header.

Expected Result: API should reject access to protected resources.

16. Verify API with Invalid Token

Send a malformed, expired, revoked, or otherwise invalid token.

Expected Result: API should deny access and return the appropriate authentication error.

17. Verify API with Insufficient Permissions

Use a valid authenticated user who does not have permission to perform a particular operation.

Expected Result: API should reject the operation according to the service’s authorization rules.

HTTP Method Validation Test Cases

Verify that each endpoint accepts only the HTTP methods intended by the API design.

For example, if an endpoint supports GET only, send POST, PUT, PATCH, and DELETE requests against that endpoint.

Expected Result: Unsupported methods should be rejected appropriately, often with 405 Method Not Allowed.

Negative Test Cases for REST APIs

Negative testing verifies how the service behaves when invalid or unexpected input is provided.

Scenario Expected Behavior
Invalid URL Appropriate error response
Invalid HTTP method Request should be rejected
Missing required parameter Validation error
Invalid parameter type Validation error
Invalid JSON Malformed request error
Missing authentication Authentication failure
Insufficient authorization Access denied
Non-existing resource Resource-not-found response
Duplicate request Business-rule validation or idempotent behavior
Oversized payload Request should be rejected or handled safely

API Response Time Test Cases

API performance should also be validated.

18. Verify Response Time Under Normal Load

Send a typical request and measure how long the service takes to respond.

Expected Result: Response time should meet the application’s defined performance requirement.

19. Verify Response Time Under High Load

Send a large number of concurrent requests.

Expected Result: The API should continue to perform within the agreed performance limits or fail gracefully when capacity is exceeded.

Pagination Test Cases

APIs that return large collections commonly use pagination.

Example:

GET /api/customers?page=1&limit=20

Verify:

  • Default page size.
  • Custom page size.
  • First page.
  • Last page.
  • Page beyond available records.
  • Zero or negative page numbers.
  • Maximum allowed page size.

Sorting and Filtering Test Cases

When APIs support sorting or filtering, verify both valid and invalid combinations.

GET /api/customers?status=active&sort=name

Verify that:

  • Correct records are returned.
  • Sorting is applied correctly.
  • Invalid filter values are handled properly.
  • Multiple filters work together correctly.
  • Unsupported sort fields are rejected or handled as documented.

API Security Test Cases

Security should be considered throughout REST API testing.

Common security test scenarios include:

  • Verify authentication and authorization.
  • Test expired and invalid tokens.
  • Verify that sensitive information is not returned unnecessarily.
  • Test input validation against malicious payloads.
  • Verify access control between different user roles.
  • Check rate-limiting behavior where applicable.
  • Verify that secrets and credentials are not exposed in logs or responses.

Database Validation Test Cases

For APIs that perform database operations, verify that the API response matches the underlying database state.

For example:

POST /api/customers

After the API creates a customer, verify that:

  • The record exists in the database.
  • The generated ID is correct.
  • Submitted values are stored correctly.
  • Default values are populated correctly.
  • Audit information is stored when required.

Data Consistency Test Cases

Verify consistency between related APIs.

For example:

POST /api/customers
GET  /api/customers/{id}
PUT  /api/customers/{id}
GET  /api/customers/{id}
DELETE /api/customers/{id}

The data returned by each API should reflect the latest valid state of the resource.

Idempotency Test Cases

Verify whether repeated execution of an API produces the expected behavior.

For example, repeated PUT requests with the same data should produce a consistent final resource state.

For operations that support idempotency keys, send the same request multiple times and verify that duplicate processing does not occur.

API Contract Testing

Verify that the implementation conforms to the API contract or specification.

Check:

  • Endpoint paths
  • HTTP methods
  • Request parameters
  • Request body schema
  • Response schema
  • Data types
  • Required and optional fields
  • Status codes
  • Error formats

End-to-End REST API Test Scenario

A practical end-to-end scenario for a customer API can be executed as follows:

  1. Create a new customer using POST.
  2. Capture the generated customer ID.
  3. Retrieve the customer using GET.
  4. Validate the returned customer details.
  5. Update the customer using PUT or PATCH.
  6. Retrieve the customer again.
  7. Verify that the updated information is returned.
  8. Delete the customer.
  9. Attempt to retrieve the deleted customer.
  10. Verify that the API returns the expected result for the deleted resource.

Sample REST API Test Case Table

TC ID Test Scenario Method Expected Result
TC01 Get customer with valid ID GET Successful response with correct customer
TC02 Get customer with invalid ID GET Resource-not-found response
TC03 Create customer with valid data POST Customer created successfully
TC04 Create customer without mandatory field POST Validation error
TC05 Update customer with valid data PUT Customer updated successfully
TC06 Partially update customer PATCH Specified field updated only
TC07 Delete existing customer DELETE Customer deleted successfully
TC08 Access protected API without token GET Access denied
TC09 Use invalid authentication token GET Authentication failure
TC10 Send unsupported HTTP method POST/PUT Method rejected appropriately
TC11 Verify response schema GET Schema matches API contract
TC12 Verify response time GET Performance requirement satisfied
TC13 Verify pagination GET Correct page and records returned
TC14 Verify filtering GET Only matching records returned
TC15 Verify duplicate creation POST Duplicate handled according to business rules

Important Checks While Testing RESTful Web Services

A REST API tester should not validate only the HTTP status code. A complete test should verify the entire request-response behavior.

Area What to Verify
URL Correct endpoint and path parameters
HTTP Method Correct method for the operation
Headers Content-Type, Authorization, Accept, and other required headers
Request Body Schema, mandatory fields, data types, and values
Status Code Expected HTTP response code
Response Body Data, structure, and business values
Response Headers Correct metadata and security headers
Performance Response time, throughput, and behavior under load
Security Authentication, authorization, input validation, and data exposure
Database Data persistence and consistency

Conclusion

RESTful Web Services testing involves much more than sending requests and checking whether the server returns a response. A strong test suite validates positive scenarios, negative scenarios, status codes, request and response data, authentication, authorization, error handling, database persistence, performance, and security.

Tools such as Postman, SoapUI, REST Assured, curl, JMeter, and similar API testing tools can be used to automate and execute these test cases. The exact test coverage should always be based on the API contract, business requirements, security requirements, and expected production usage.

By creating comprehensive REST API test cases, software teams can identify integration defects early and build reliable services for web, mobile, and distributed applications.