A sample HTTP server application designed to test and validate JSON schema matching capabilities with Keploy. This application serves multiple diverse endpoints with various response schemas to comprehensively test schema structure validation and compatibility.
This application is a simple socket-based HTTP server written in Python that provides 10 endpoints covering different schema patterns including:
- Nested Objects: User profile with complex nested data structures
- Arrays: Different array formats and mixed-type arrays
- Edge Cases: Null values, empty responses, special characters, and large payloads
- Data Types: Strings, numbers, booleans, objects, and null values
The application supports schema validation testing to ensure API responses maintain consistent structure across different versions or implementations.
GET /user/profile- Returns user profile with nested preferencesGET /user/history- Returns user login history with array of objectsGET /product/search- Returns product search results with items arrayGET /admin/config- Returns admin configuration with feature flagsGET /data/matrix- Returns a matrix data structureGET /data/mixed_array- Returns an array with mixed data typesGET /edge/empty_response- Returns an empty JSON objectGET /edge/null_root- Returns a null value at rootGET /edge/special_chars- Returns special characters and escape sequencesGET /edge/nested_null- Returns an object with null values
- Python 3.7 or higher
- Keploy CLI installed on your system
- Linux or WSL (for Windows users)
curl -O -L https://keploy.io/install.sh && source install.shgit clone https://github.com/keploy/samples-python.git && cd samples-python/python-schema-matchThis server returns standard responses and is used for recording test cases:
python3 app.pyThe server will start on http://localhost:5000
This server returns modified responses with schema variations to test compatibility:
python3 app-test.pyKeploy will capture API calls and generate test cases with mocked responses.
1. Start Recording:
keploy record -c "python3 app.py"The server will start and be ready to capture API interactions.
2. Generate Test Cases by Making API Calls:
Use curl, Postman, or Hoppscotch to make requests to the endpoints. Here are some examples:
# Check endpoint 1: User Profile
curl http://localhost:5000/user/profile
# Check endpoint 2: User History
curl http://localhost:5000/user/history
# Check endpoint 3: Product Search
curl http://localhost:5000/product/search
# Check endpoint 4: Admin Config
curl http://localhost:5000/admin/config
# Check endpoint 5: Data Matrix
curl http://localhost:5000/data/matrix
# Check endpoint 6: Mixed Array
curl http://localhost:5000/data/mixed_array
# Check endpoint 7: Empty Response
curl http://localhost:5000/edge/empty_response
# Check endpoint 8: Null Root
curl http://localhost:5000/edge/null_root
# Check endpoint 9: Special Characters
curl http://localhost:5000/edge/special_chars
# Check endpoint 10: Nested Null
curl http://localhost:5000/edge/nested_nullOr use the provided endpoint checker script:
python3 check-endpoints.pyThis script will automatically test all 10 endpoints and provide a summary.
3. Keploy Test Artifacts
After making API calls, Keploy will generate test cases in the keploy/ directory:
- Test Files:
keploy/tests/test-*.yml- Contains HTTP request/response pairs - Mock Files:
keploy/mocks/mocks.yml- Contains any external service interactions
Each test file captures:
- Request: HTTP method, headers, URL, and body
- Response: Status code, headers, and response body
- Assertions: Validation rules including noise fields (like timestamps)
Example test output:
version: api.keploy.io/v1beta2
kind: Http
name: test-1
spec:
metadata: {}
req:
method: GET
url: http://localhost:5000/user/profile
header:
Accept: "*/*"
Host: localhost:5000
User-Agent: curl/7.68.0
resp:
status_code: 200
header:
Content-Type: application/json
Content-Length: "245"
body: |
{
"id": 101,
"username": "keploy_user",
"active": true,
"profile": {
"age": 25,
"city": "San Francisco",
"preferences": {"theme": "dark", "notifications": true}
},
"roles": ["admin", "editor"]
}
assertions:
noise:
- header.Date
created: 1694000000Test your application against the captured test cases using the test server:
keploy test -c "python3 app-test.py" --delay 5The --delay flag gives your application time to start before tests begin (in seconds).
Expected Results:
- Total Endpoints: 10
- Expected PASS: 7 (Same schema structure, different values)
- Expected FAIL: 3 (Schema modifications testing)
- Missing fields
- Type mismatches
- Hierarchy mismatches
The application validates that:
- Extra fields are allowed (superset schema)
- Array length variations are acceptable
- Different values with same types pass
- Type changes are detected
- Missing required fields are detected
- Nested structure changes are caught
After running tests, Keploy will display:
- Test summary (passed/failed count)
- Coverage metrics
- Detailed failure reports with diff information
python-schema-match/
├── app.py # Original HTTP server (for recording)
├── app-test.py # Modified HTTP server (for testing)
├── check-endpoints.py # Utility script to test all endpoints
├── README.md # This file
└── keploy/ # Keploy artifacts (generated)
├── tests/
│ ├── test-1.yml
│ ├── test-2.yml
│ └── ...
└── mocks/
└── mocks.yml
This sample application demonstrates:
- Schema Validation: Ensure API responses maintain consistent JSON structure
- API Contract Testing: Validate that responses match expected schemas
- Backward Compatibility: Test if new versions break schema contracts
- Data Type Verification: Ensure fields have correct data types
- Response Structure Integrity: Validate nested objects and arrays
If port 5000 is already in use, modify the PORT variable in app.py or app-test.py.
Ensure the server is running before making API calls or running tests.
Expected failures are intentional to demonstrate schema mismatch detection. Review the diff information in Keploy output to understand the schema differences.
Feel free to extend this sample by:
- Adding more diverse schema patterns
- Testing additional data types
- Creating more complex nested structures
- Adding custom validation rules
MIT License - See LICENSE.md for details
Happy Testing! 🚀