Skip to content

Commit fbf2d10

Browse files
committed
Tests moved to /test, routes logic improved
1 parent 1bae4e3 commit fbf2d10

22 files changed

+61
-35
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ Chaos Proxy uses Koa Router for path matching, supporting named parameters (e.g.
100100
- There is no inheritance between global and route-specific middleware.
101101
- Global middlewares apply to every request.
102102
- Route middlewares only apply to requests matching that route.
103+
- Routes can be defined with or without HTTP methods. If a method is specified (e.g., `GET /path`), the rule only applies to that method. If no method is specified (e.g., `/path`), the rule applies to all methods for that path.
103104
- If a request matches a route, only the middlewares for that route (plus global) are applied. Route rules do not inherit or merge from parent routes or wildcards.
104105
- If multiple routes match, the most specific one is chosen (e.g., `/users/:id` over `/users/*`).
105106
- If no route matches, only global middlewares are applied.
@@ -210,6 +211,26 @@ This configuration adds a `foo: 'bar'` property to every JSON request body befor
210211
**Note:**
211212
For maximum flexibility, the `transform` option in `bodyTransform` can be specified as a JavaScript function string in your YAML config. This allows you to define custom transformation logic directly in the config file. Be aware that evaluating JS from config can introduce security and syntax risks. Use with care and only in trusted environments.
212213

214+
215+
If you call `startServer` programmatically, you can also pass a real function instead of a string:
216+
217+
```ts
218+
import { startServer, bodyTransform } from 'chaos-proxy';
219+
220+
startServer({
221+
target: 'http://localhost:4000',
222+
port: 5000,
223+
global: [
224+
bodyTransform({
225+
transform: (body, ctx) => {
226+
body.foo = 'bar';
227+
return body;
228+
}
229+
})
230+
]
231+
});
232+
```
233+
213234
---
214235

215236
## Extensibility

src/cli.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#!/usr/bin/env node
22

3-
import { loadConfig, startServer, registerBuiltins } from './index';
4-
5-
registerBuiltins();
3+
import { loadConfig, startServer } from './index';
64

75
const args = process.argv.slice(2);
86
let configPath = 'chaos.yaml';

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ import { registerBuiltins } from './registry/builtin';
55

66
registerBuiltins();
77

8-
export { loadConfig, startServer, registerMiddleware, registerBuiltins };
8+
export { loadConfig, startServer, registerMiddleware };

src/registry/builtin.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { dropConnection } from '../middlewares/dropConnection';
88
import { fail } from '../middlewares/fail';
99
import { rateLimit } from '../middlewares/rateLimit';
1010
import type { RateLimitOptions } from '../middlewares/rateLimit';
11+
import { throttle } from '../middlewares/throttle';
12+
import type { ThrottleOptions } from '../middlewares/throttle';
1113

1214
export function registerBuiltins() {
1315
// Register built-in middleware primitives
@@ -24,4 +26,5 @@ export function registerBuiltins() {
2426
cors(opts as { origin?: string; methods?: string; headers?: string })
2527
);
2628
registerMiddleware('rateLimit', (opts) => rateLimit(opts as unknown as RateLimitOptions));
29+
registerMiddleware('throttle', (opts) => throttle(opts as unknown as ThrottleOptions));
2730
}

src/server.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ export function startServer(config: ChaosConfig, options?: { verbose?: boolean }
4242
throw new Error(`Unsupported HTTP method: ${method}`);
4343
}
4444
} else {
45-
router.use(routeKey, ...middlewares);
45+
// Mount for all HTTP methods if no method is specified
46+
const methods = ['get', 'post', 'put', 'delete', 'patch', 'head', 'options'];
47+
for (const method of methods) {
48+
(router[method as keyof typeof router] as (path: string, ...middleware: Array<Koa.Middleware>) => Router)(routeKey, ...middlewares);
49+
}
4650
}
4751
}
4852
app.use(router.routes());

src/cli.test.ts renamed to test/cli.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, it, expect } from 'vitest';
2-
import { loadConfig } from './config/loader';
3-
import { startServer } from './server';
4-
import { parseConfig } from './config/parser';
2+
import { loadConfig } from '../src/config/loader';
3+
import { startServer } from '../src/server';
4+
import { parseConfig } from '../src/config/parser';
55
// No longer needed: import express from 'express';
66

77
describe('chaos-proxy CLI logic', () => {

src/config/loader.test.ts renamed to test/config/loader.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it, expect } from 'vitest';
2-
import { loadConfig } from './loader';
2+
import { loadConfig } from '../../src/config/loader';
33

44
describe('loadConfig', () => {
55
it('throws if config file is missing', () => {

src/config/parser.test.ts renamed to test/config/parser.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { describe, it, expect } from 'vitest';
2-
import { parseConfig, resolveConfigMiddlewares } from './parser';
2+
import { parseConfig, resolveConfigMiddlewares } from '../../src/config/parser';
33
// ...existing code...
44
// ...existing code...
55
// ...existing code...
6-
import type { ChaosConfig } from './loader';
6+
import type { ChaosConfig } from '../../src/config/loader';
77

88
describe('parseConfig', () => {
99
it('parses a valid config with default port', () => {

src/middlewares/bodyTransform.test.ts renamed to test/middlewares/bodyTransform.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
import { describe, it, expect } from 'vitest';
3-
import { bodyTransform } from './bodyTransform';
3+
import { bodyTransform } from '../../src/middlewares/bodyTransform';
44
import type { Context } from 'koa';
55

66
function createMockCtx(body: unknown, contentType = 'application/json'): Context {

src/middlewares/cors.test.ts renamed to test/middlewares/cors.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it, expect, vi } from 'vitest';
2-
import { cors } from './cors';
2+
import { cors } from '../../src/middlewares/cors';
33
import type { Context } from 'koa';
44

55
describe('cors middleware', () => {

0 commit comments

Comments
 (0)