Skip to content

Commit 13c465d

Browse files
committed
Added http type & some fixes
1 parent 8b05ebd commit 13c465d

21 files changed

+2481
-450
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ fake-cli
66

77
dist/
88

9-
coverage.out
9+
coverage.*
1010
.DS_Store

Makefile

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ run:
2323
default: build
2424

2525
test:
26-
TESTING=1 go test ./tests -covermode=atomic -coverpkg=./app,./cache,./config,./handler -coverprofile=coverage.out
26+
TESTING=1 go test ./... -covermode=atomic -coverpkg=./app,./cache,./config,./handler -coverprofile=coverage.out
2727
@echo "\nCoverage by function/package:" && go tool cover -func=coverage.out | sed 's/^/ /'
28-
@echo "\nUncovered code blocks (file:line1.col-line2.col [statements]):"
29-
@awk 'NR>1 && $$3==0 {print " " $$1 " [" $$2 "]"}' coverage.out | sort
30-
@echo "Enforcing 100% coverage"
31-
@go tool cover -func=coverage.out | awk '/total:/ { if ($$3 != "100.0%") { print; exit 1 } }'
28+
@echo "\nEnforcing 100% coverage"
29+
@go tool cover -func=coverage.out | awk '/total:/ { if ($$3 != "100.0%") { print "ERROR: Coverage is not 100%"; exit 1 } }'

README.md

Lines changed: 240 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,37 @@ make run # runs the program
2525

2626
## Usage
2727

28-
To generate fake API responses, you must create a configuration file in JSON format that defines the endpoints and fields for each endpoint. We recommended use `.json` type for files. Here's an example configuration file `config.json`:
28+
To generate fake API responses, you must create a configuration file in JSON format that defines the endpoints and response template for each endpoint. We recommended use `.json` type for files. Here's an example configuration file `config.json`:
2929

3030
```json
3131
{
32-
"cache": 5,
3332
"endpoints": [
3433
{
3534
"url": "/users",
36-
"fields": {
37-
"id": "uuid",
38-
"name": "name",
39-
"email": "email"
40-
},
41-
"response": "list"
35+
"type": "GET", // default is GET if omitted
36+
"cache": 5, // individual cache for this endpoint (5 requests)
37+
"response": [
38+
{
39+
"id": "uuid",
40+
"name": "name",
41+
"email": "email"
42+
}
43+
]
4244
},
4345
{
4446
"url": "/products",
45-
"fields": {
47+
"cache": 10, // different cache size for this endpoint
48+
// single object response when response is an object
49+
"response": {
4650
"id": "uuid",
4751
"name": "word",
48-
"price": "price"
52+
"price": "float"
4953
}
5054
},
5155
{
5256
"url": "/products/{id}",
53-
"fields": {
57+
"cache": 3, // smaller cache for individual product
58+
"response": {
5459
"id": "uuid",
5560
"tags": [{
5661
"id": "uuid",
@@ -66,56 +71,130 @@ To generate fake API responses, you must create a configuration file in JSON for
6671
},
6772
{
6873
"url": "/list",
69-
"fields": {
74+
// no cache field - this endpoint won't be cached
75+
"response": {
7076
"names": ["name", "name", "name"]
7177
}
78+
},
79+
{
80+
"url": "/submit",
81+
"type": "POST",
82+
// POST endpoints typically don't need caching
83+
"response": {"status": "word"}
84+
},
85+
{
86+
"url": "/update/{id}",
87+
"type": "PATCH",
88+
"cache": 1, // cache only 1 response for updates
89+
"response": {"updated": "word"}
90+
},
91+
{
92+
"url": "/replace/{id}",
93+
"type": "PUT",
94+
"response": {"replaced": "word"}
95+
},
96+
{
97+
"url": "/remove/{id}",
98+
"type": "DELETE",
99+
"response": {"removed": "word"}
72100
}
73101
]
74102
}
75103
```
76-
This configuration file defines two endpoints: /users and /products. The /users endpoint returns a list of users with pagination (by default page = 1 and per_page = 10), while the /products endpoint returns a single product.
77-
78-
Cache is not required, but if existed it is count of request for caching. By default cache settings is 0 and every request will generate new data. If you specify -1, this will turn off further generation and all data will be used from the cache.
79-
80-
You can also specify the type Array, Object as a value. Each of these types can have nested values.
81-
82-
List of types:
83-
- uuid
84-
- city
85-
- state
86-
- country
87-
- latitude
88-
- longitude
89-
- name
90-
- name_prefix
91-
- name_suffix
92-
- first_name
93-
- last_name
94-
- gender
95-
- ssn
96-
- hobby
97-
- email
98-
- phone
99-
- username
100-
- password
101-
- paragraph
102-
- sentence
103-
- phrase
104-
- quote
105-
- word
106-
- date
107-
- second
108-
- minute
109-
- hour
110-
- month
111-
- day
112-
- year
113-
- url
114-
- domain
115-
- ip
116-
- int
117-
- float
104+
This configuration file defines several endpoints. The `/users` endpoint returns a list (top-level array in `response`) with pagination (by default `page=1` and `per_page=10`). The `/products` endpoint returns a single object (when `response` is an object). Endpoints may specify an HTTP method using `type` and support: `GET` (default), `POST`, `PATCH`, `PUT`, `DELETE`.
118105

106+
## Caching
107+
108+
Each endpoint can have its own individual cache configuration:
109+
110+
- **`cache: 5`** - Cache responses for 5 requests, then generate new data
111+
- **`cache: 10`** - Cache responses for 10 requests, then generate new data
112+
- **`cache: 1`** - Cache only 1 response, then generate new data
113+
- **No `cache` field** - No caching, generate new data on every request
114+
115+
**Important notes:**
116+
- Caching only works for `GET` requests
117+
- Each endpoint has its own separate cache instance
118+
- Cache is based on request URL and query parameters
119+
- If `cache` is not specified, the endpoint will not use caching
120+
121+
You can specify arrays or objects inside `response`. A top-level object means a single-object response; a top-level array (e.g., `[ { ... } ]`) means a list response where the first item defines the item template. Nested arrays/objects are supported.
122+
123+
## Available Data Types
124+
125+
| Category | Type | Description |
126+
|----------|------|-------------|
127+
| **Identifiers** | `uuid` | Universally unique identifier |
128+
| | `ssn` | Social Security Number |
129+
| **Geographic** | `city` | City name |
130+
| | `state` | State/Province name |
131+
| | `country` | Country name |
132+
| | `latitude` | Geographic latitude |
133+
| | `longitude` | Geographic longitude |
134+
| **Personal** | `name` | Full name |
135+
| | `name_prefix` | Name prefix (Mr., Mrs., Dr.) |
136+
| | `name_suffix` | Name suffix (Jr., Sr., III) |
137+
| | `first_name` | First name |
138+
| | `last_name` | Last name |
139+
| | `gender` | Gender |
140+
| **Contact** | `email` | Email address |
141+
| | `phone` | Phone number |
142+
| **Authentication** | `username` | Username |
143+
| | `password` | Password |
144+
| **Text Content** | `paragraph` | Multi-sentence paragraph |
145+
| | `sentence` | Single sentence |
146+
| | `phrase` | Short phrase |
147+
| | `quote` | Famous quote |
148+
| | `word` | Single word |
149+
| **Time & Date** | `date` | Date (YYYY-MM-DD) |
150+
| | `second` | Second (0-59) |
151+
| | `minute` | Minute (0-59) |
152+
| | `hour` | Hour (0-23) |
153+
| | `month` | Month (1-12) |
154+
| | `day` | Day of month (1-31) |
155+
| | `year` | Year |
156+
| **Web & Network** | `url` | URL |
157+
| | `domain` | Domain name |
158+
| | `ip` | IP address |
159+
| **Numbers** | `int` | Integer |
160+
| | `float` | Floating point number |
161+
| **Other** | `hobby` | Hobby/Interest |
162+
163+
### Type Usage Examples
164+
165+
```json
166+
{
167+
"endpoints": [
168+
{
169+
"url": "/user-profile",
170+
"response": {
171+
"id": "uuid", // Unique identifier
172+
"name": "name", // Full name
173+
"email": "email", // Email address
174+
"phone": "phone", // Phone number
175+
"location": {
176+
"city": "city", // City name
177+
"country": "country" // Country name
178+
},
179+
"created_at": "date", // Creation date
180+
"age": "int", // Age as integer
181+
"bio": "paragraph" // Biography text
182+
}
183+
},
184+
{
185+
"url": "/product",
186+
"response": {
187+
"id": "uuid",
188+
"title": "word", // Product title
189+
"description": "sentence", // Product description
190+
"price": "float", // Price as decimal
191+
"tags": ["word", "word"], // Array of tags
192+
"website": "url" // Product website
193+
}
194+
}
195+
]
196+
}
197+
```
119198

120199
To generate fake API responses using this configuration file, you can run the fake command:
121200

@@ -130,6 +209,91 @@ By default, the fake command starts a web server on port 8000 that responds to r
130209
fake-cli -c path/to/config.json -p 8080
131210
```
132211

212+
## Migration from Global Cache
213+
214+
If you're upgrading from a version with global cache, here's how to migrate:
215+
216+
**Old configuration (global cache):**
217+
```json
218+
{
219+
"cache": 5,
220+
"endpoints": [...]
221+
}
222+
```
223+
224+
**New configuration (individual cache):**
225+
```json
226+
{
227+
"endpoints": [
228+
{
229+
"url": "/users",
230+
"cache": 5, // Move cache setting to each endpoint
231+
"response": {...}
232+
},
233+
{
234+
"url": "/products",
235+
"cache": 5, // Apply same cache to all endpoints
236+
"response": {...}
237+
}
238+
]
239+
}
240+
```
241+
242+
**Benefits of individual cache:**
243+
- Different cache sizes for different endpoints
244+
- Better memory management
245+
- More granular control
246+
- No global cache conflicts
247+
248+
## Examples
249+
250+
### Individual Cache per Endpoint
251+
252+
Here's how different cache configurations work in practice:
253+
254+
```json
255+
{
256+
"endpoints": [
257+
{
258+
"url": "/api/users",
259+
"cache": 5,
260+
"response": {"id": "uuid", "name": "name"}
261+
},
262+
{
263+
"url": "/api/products",
264+
"cache": 10,
265+
"response": {"id": "uuid", "title": "word"}
266+
},
267+
{
268+
"url": "/api/orders",
269+
"response": {"id": "uuid", "status": "word"}
270+
}
271+
]
272+
}
273+
```
274+
275+
**Behavior:**
276+
- `/api/users` - Same response for 5 requests, then new data
277+
- `/api/products` - Same response for 10 requests, then new data
278+
- `/api/orders` - New data on every request (no caching)
279+
280+
### Cache Testing
281+
282+
Test your cache configuration:
283+
284+
```bash
285+
# Start the server
286+
./fake-cli config.json 8080
287+
288+
# Test cached endpoint (should return same data)
289+
curl http://localhost:8080/api/users
290+
curl http://localhost:8080/api/users # Same response
291+
292+
# Test non-cached endpoint (should return different data)
293+
curl http://localhost:8080/api/orders
294+
curl http://localhost:8080/api/orders # Different response
295+
```
296+
133297
## Customization
134298

135299
You can customize the types of fake data generated by editing the handler/handler.go file. The MakeHandler function generates fake data based on the fields and response type defined in the configuration file.
@@ -170,6 +334,28 @@ services:
170334
- ./config.json:/app/config.json
171335
```
172336
337+
**Example config.json for Docker:**
338+
```json
339+
{
340+
"endpoints": [
341+
{
342+
"url": "/api/users",
343+
"cache": 5,
344+
"response": {"id": "uuid", "name": "name", "email": "email"}
345+
},
346+
{
347+
"url": "/api/products",
348+
"cache": 10,
349+
"response": {"id": "uuid", "title": "word", "price": "float"}
350+
},
351+
{
352+
"url": "/api/health",
353+
"response": {"status": "ok", "timestamp": "date"}
354+
}
355+
]
356+
}
357+
```
358+
173359
## Contributing
174360

175361
If you find a bug or would like to suggest a new feature, you can create an issue on the GitHub repository for this project. If you'd like to contribute code, you can fork the repository and submit a pull request with your changes.

0 commit comments

Comments
 (0)