|
| 1 | +# Internal API Test Server |
| 2 | + |
| 3 | +This directory contains the internal API test server implementation that replaces the third-party `json-server` dependency. |
| 4 | + |
| 5 | +## Files |
| 6 | + |
| 7 | +- `lib/test-server.js` - Main TestServer class implementation |
| 8 | +- `bin/test-server.js` - CLI script to run the server standalone |
| 9 | + |
| 10 | +## Usage |
| 11 | + |
| 12 | +### As npm script: |
| 13 | + |
| 14 | +```bash |
| 15 | +npm run test-server |
| 16 | +``` |
| 17 | + |
| 18 | +### Directly: |
| 19 | + |
| 20 | +```bash |
| 21 | +node bin/test-server.js [options] [db-file] |
| 22 | +``` |
| 23 | + |
| 24 | +### Options: |
| 25 | + |
| 26 | +- `-p, --port <port>` - Port to listen on (default: 8010) |
| 27 | +- `--host <host>` - Host to bind to (default: 0.0.0.0) |
| 28 | +- `db-file` - Path to JSON database file (default: test/data/rest/db.json) |
| 29 | + |
| 30 | +## Features |
| 31 | + |
| 32 | +- **Full REST API compatibility** with json-server |
| 33 | +- **Automatic file watching** - Reloads data when db.json file changes |
| 34 | +- **CORS support** - Allows cross-origin requests for testing |
| 35 | +- **Custom headers support** - Handles special headers like X-Test |
| 36 | +- **File upload endpoints** - Basic file upload simulation |
| 37 | +- **Express.js based** - Uses familiar Express.js framework |
| 38 | + |
| 39 | +## API Endpoints |
| 40 | + |
| 41 | +The server provides the same API endpoints as json-server: |
| 42 | + |
| 43 | +### Users |
| 44 | + |
| 45 | +- `GET /user` - Get user data |
| 46 | +- `POST /user` - Create/update user |
| 47 | +- `PATCH /user` - Partially update user |
| 48 | +- `PUT /user` - Replace user |
| 49 | + |
| 50 | +### Posts |
| 51 | + |
| 52 | +- `GET /posts` - Get all posts |
| 53 | +- `GET /posts/:id` - Get specific post |
| 54 | +- `POST /posts` - Create new post |
| 55 | +- `PUT /posts/:id` - Replace specific post |
| 56 | +- `PATCH /posts/:id` - Partially update specific post |
| 57 | +- `DELETE /posts/:id` - Delete specific post |
| 58 | + |
| 59 | +### Comments |
| 60 | + |
| 61 | +- `GET /comments` - Get all comments |
| 62 | +- `POST /comments` - Create new comment |
| 63 | +- `DELETE /comments/:id` - Delete specific comment |
| 64 | + |
| 65 | +### Utility |
| 66 | + |
| 67 | +- `GET /headers` - Return request headers (for testing) |
| 68 | +- `POST /headers` - Return request headers (for testing) |
| 69 | +- `POST /upload` - File upload simulation |
| 70 | +- `POST /_reload` - Manually reload database file |
| 71 | + |
| 72 | +## Migration from json-server |
| 73 | + |
| 74 | +This server is designed as a drop-in replacement for json-server. The key differences: |
| 75 | + |
| 76 | +1. **No CLI options** - Configuration is done through constructor options or CLI args |
| 77 | +2. **Automatic file watching** - No need for `--watch` flag |
| 78 | +3. **Built-in middleware** - Headers and CORS are handled automatically |
| 79 | +4. **Simpler file upload** - Basic implementation without full multipart support |
| 80 | + |
| 81 | +## Testing |
| 82 | + |
| 83 | +The server is used by the following test suites: |
| 84 | + |
| 85 | +- `test/rest/REST_test.js` - REST helper tests |
| 86 | +- `test/rest/ApiDataFactory_test.js` - API data factory tests |
| 87 | +- `test/helper/JSONResponse_test.js` - JSON response helper tests |
| 88 | + |
| 89 | +All tests pass with the internal server, proving full compatibility. |
0 commit comments