Skip to content

Commit 34f963f

Browse files
committed
feat: added read me file
Signed-off-by: Harshit Pathak <harshit07pathak@gmail.com>
1 parent c266e9a commit 34f963f

1 file changed

Lines changed: 269 additions & 0 deletions

File tree

python-schema-match/README.md

Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
1+
# Python Schema Match
2+
3+
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.
4+
5+
## Overview
6+
7+
This application is a simple socket-based HTTP server written in Python that provides 10 endpoints covering different schema patterns including:
8+
9+
- **Nested Objects**: User profile with complex nested data structures
10+
- **Arrays**: Different array formats and mixed-type arrays
11+
- **Edge Cases**: Null values, empty responses, special characters, and large payloads
12+
- **Data Types**: Strings, numbers, booleans, objects, and null values
13+
14+
The application supports schema validation testing to ensure API responses maintain consistent structure across different versions or implementations.
15+
16+
### API Endpoints
17+
18+
1. `GET /user/profile` - Returns user profile with nested preferences
19+
2. `GET /user/history` - Returns user login history with array of objects
20+
3. `GET /product/search` - Returns product search results with items array
21+
4. `GET /admin/config` - Returns admin configuration with feature flags
22+
5. `GET /data/matrix` - Returns a matrix data structure
23+
6. `GET /data/mixed_array` - Returns an array with mixed data types
24+
7. `GET /edge/empty_response` - Returns an empty JSON object
25+
8. `GET /edge/null_root` - Returns a null value at root
26+
9. `GET /edge/special_chars` - Returns special characters and escape sequences
27+
10. `GET /edge/nested_null` - Returns an object with null values
28+
29+
## Installation & Setup
30+
31+
### Prerequisites
32+
33+
- Python 3.7 or higher
34+
- Keploy CLI installed on your system
35+
- Linux or WSL (for Windows users)
36+
37+
### Install Keploy
38+
39+
```bash
40+
curl -O -L https://keploy.io/install.sh && source install.sh
41+
```
42+
43+
### Clone the Repository
44+
45+
```bash
46+
git clone https://github.com/keploy/samples-python.git && cd samples-python/python-schema-match
47+
```
48+
49+
## Running the Application
50+
51+
### Start the Server (Original Version - for Recording)
52+
53+
This server returns standard responses and is used for recording test cases:
54+
55+
```bash
56+
python3 app.py
57+
```
58+
59+
The server will start on `http://localhost:5000`
60+
61+
### Start the Test Server (Modified Version - for Testing)
62+
63+
This server returns modified responses with schema variations to test compatibility:
64+
65+
```bash
66+
python3 app-test.py
67+
```
68+
69+
## Keploy Integration
70+
71+
### Recording Test Cases
72+
73+
Keploy will capture API calls and generate test cases with mocked responses.
74+
75+
**1. Start Recording:**
76+
77+
```bash
78+
keploy record -c "python3 app.py"
79+
```
80+
81+
The server will start and be ready to capture API interactions.
82+
83+
**2. Generate Test Cases by Making API Calls:**
84+
85+
Use curl, Postman, or Hoppscotch to make requests to the endpoints. Here are some examples:
86+
87+
```bash
88+
# Check endpoint 1: User Profile
89+
curl http://localhost:5000/user/profile
90+
91+
# Check endpoint 2: User History
92+
curl http://localhost:5000/user/history
93+
94+
# Check endpoint 3: Product Search
95+
curl http://localhost:5000/product/search
96+
97+
# Check endpoint 4: Admin Config
98+
curl http://localhost:5000/admin/config
99+
100+
# Check endpoint 5: Data Matrix
101+
curl http://localhost:5000/data/matrix
102+
103+
# Check endpoint 6: Mixed Array
104+
curl http://localhost:5000/data/mixed_array
105+
106+
# Check endpoint 7: Empty Response
107+
curl http://localhost:5000/edge/empty_response
108+
109+
# Check endpoint 8: Null Root
110+
curl http://localhost:5000/edge/null_root
111+
112+
# Check endpoint 9: Special Characters
113+
curl http://localhost:5000/edge/special_chars
114+
115+
# Check endpoint 10: Nested Null
116+
curl http://localhost:5000/edge/nested_null
117+
```
118+
119+
Or use the provided endpoint checker script:
120+
121+
```bash
122+
python3 check-endpoints.py
123+
```
124+
125+
This script will automatically test all 10 endpoints and provide a summary.
126+
127+
**3. Keploy Test Artifacts**
128+
129+
After making API calls, Keploy will generate test cases in the `keploy/` directory:
130+
131+
- **Test Files**: `keploy/tests/test-*.yml` - Contains HTTP request/response pairs
132+
- **Mock Files**: `keploy/mocks/mocks.yml` - Contains any external service interactions
133+
134+
Each test file captures:
135+
- **Request**: HTTP method, headers, URL, and body
136+
- **Response**: Status code, headers, and response body
137+
- **Assertions**: Validation rules including noise fields (like timestamps)
138+
139+
Example test output:
140+
141+
```yaml
142+
version: api.keploy.io/v1beta2
143+
kind: Http
144+
name: test-1
145+
spec:
146+
metadata: {}
147+
req:
148+
method: GET
149+
url: http://localhost:5000/user/profile
150+
header:
151+
Accept: "*/*"
152+
Host: localhost:5000
153+
User-Agent: curl/7.68.0
154+
resp:
155+
status_code: 200
156+
header:
157+
Content-Type: application/json
158+
Content-Length: "245"
159+
body: |
160+
{
161+
"id": 101,
162+
"username": "keploy_user",
163+
"active": true,
164+
"profile": {
165+
"age": 25,
166+
"city": "San Francisco",
167+
"preferences": {"theme": "dark", "notifications": true}
168+
},
169+
"roles": ["admin", "editor"]
170+
}
171+
assertions:
172+
noise:
173+
- header.Date
174+
created: 1694000000
175+
```
176+
177+
### Running Test Cases
178+
179+
Test your application against the captured test cases using the test server:
180+
181+
```bash
182+
keploy test -c "python3 app-test.py" --delay 5
183+
```
184+
185+
The `--delay` flag gives your application time to start before tests begin (in seconds).
186+
187+
**Expected Results:**
188+
189+
- **Total Endpoints**: 10
190+
- **Expected PASS**: 7 (Same schema structure, different values)
191+
- **Expected FAIL**: 3 (Schema modifications testing)
192+
- Missing fields
193+
- Type mismatches
194+
- Hierarchy mismatches
195+
196+
The application validates that:
197+
1. Extra fields are allowed (superset schema)
198+
2. Array length variations are acceptable
199+
3. Different values with same types pass
200+
4. Type changes are detected
201+
5. Missing required fields are detected
202+
6. Nested structure changes are caught
203+
204+
### Output and Coverage
205+
206+
After running tests, Keploy will display:
207+
- Test summary (passed/failed count)
208+
- Coverage metrics
209+
- Detailed failure reports with diff information
210+
211+
## Project Structure
212+
213+
```
214+
python-schema-match/
215+
├── app.py # Original HTTP server (for recording)
216+
├── app-test.py # Modified HTTP server (for testing)
217+
├── check-endpoints.py # Utility script to test all endpoints
218+
├── README.md # This file
219+
└── keploy/ # Keploy artifacts (generated)
220+
├── tests/
221+
│ ├── test-1.yml
222+
│ ├── test-2.yml
223+
│ └── ...
224+
└── mocks/
225+
└── mocks.yml
226+
```
227+
228+
## Use Cases
229+
230+
This sample application demonstrates:
231+
232+
1. **Schema Validation**: Ensure API responses maintain consistent JSON structure
233+
2. **API Contract Testing**: Validate that responses match expected schemas
234+
3. **Backward Compatibility**: Test if new versions break schema contracts
235+
4. **Data Type Verification**: Ensure fields have correct data types
236+
5. **Response Structure Integrity**: Validate nested objects and arrays
237+
238+
## Troubleshooting
239+
240+
### Port Already in Use
241+
If port 5000 is already in use, modify the `PORT` variable in `app.py` or `app-test.py`.
242+
243+
### Connection Refused
244+
Ensure the server is running before making API calls or running tests.
245+
246+
### Test Failures
247+
Expected failures are intentional to demonstrate schema mismatch detection. Review the diff information in Keploy output to understand the schema differences.
248+
249+
## Additional Resources
250+
251+
- [Keploy Documentation](https://docs.keploy.io)
252+
- [JSON Schema Guide](https://json-schema.org/)
253+
- [Python HTTP Servers](https://docs.python.org/3/library/http.server.html)
254+
255+
## Contributing
256+
257+
Feel free to extend this sample by:
258+
- Adding more diverse schema patterns
259+
- Testing additional data types
260+
- Creating more complex nested structures
261+
- Adding custom validation rules
262+
263+
## License
264+
265+
MIT License - See LICENSE.md for details
266+
267+
---
268+
269+
**Happy Testing! 🚀**

0 commit comments

Comments
 (0)