Skip to content

Commit

Permalink
Support an array for the forward setting. (#55)
Browse files Browse the repository at this point in the history
Support an array for the `forward` config option so that requests to the
same domain could be routed to different servers based on paths.
  • Loading branch information
ballercat authored May 3, 2024
1 parent bf559a8 commit fd9d78c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.4.0 - Support a forward array in configuration

- feat: support an array in the `forward` setting

## 0.3.0 - Add auto CORS support in forwarded requests

- feat: add cors support in forwarded requests
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jambox",
"version": "0.3.0",
"version": "0.4.0",
"description": "Tool for recording and playing back HTTP requests.",
"bin": {
"jam": "./jam.mjs",
Expand Down
18 changes: 14 additions & 4 deletions src/Jambox.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,26 @@ export default class Jambox extends Emitter {
}

forward(setting) {
return Promise.all(
Object.entries(setting).map(async ([original, ...rest]) => {
let entries;
if (Array.isArray(setting)) {
entries = setting;
} else {
entries = Object.entries(setting).map(([match, ...rest]) => {
const options =
typeof rest[0] === 'object'
? rest[0]
: {
target: rest[0],
};

const originalURL = new URL(original);
return {
match,
...options,
};
});
}
return Promise.all(
entries.map(async (options) => {
const originalURL = new URL(options.match);
const targetURL = new URL(
options.target,
// If the first argument of new URL() is a path the second argument is
Expand Down
42 changes: 42 additions & 0 deletions src/__tests__/server.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,48 @@ test.serial('cors support', async (t) => {
});
});

test.serial('forward array', async (t) => {
t.assert(t.context.server, `Server init error: ${t.context.error?.stack}`);

const { body: config } = await supertest(t.context.server).get('/api/config');

await supertest(t.context.server)
.post('/api/config')
.send({
forward: [
{
match: 'http://jambox.test.com',
target: `http://localhost:${APP_PORT}`,
paths: ['**', '!**/foobar'],
},
{
match: 'http://jambox.test.com',
target: `http://localhost:${APP_PORT}`,
paths: ['**/foobar'],
cors: true,
},
],
});

const opts = {
agent: new HttpsProxyAgent(config.proxy.http),
method: 'OPTIONS',
};

// OPTIONS should 204 automatically
let response = await fetch(`http://jambox.test.com/foobar`, opts);
t.like(response, { status: 204, statusText: 'No Content' });

response = await fetch(`http://jambox.test.com/foobar`, {
...opts,
method: 'GET',
});

t.like(Object.fromEntries(response.headers.entries()), {
// This is set by jambox automatically when cors is enabled
'access-control-allow-origin': '*',
});
});
test.serial('auto mocks', async (t) => {
t.assert(t.context.server, `Server init error: ${t.context.error?.stack}`);
const { body: config } = await supertest(t.context.server).get('/api/config');
Expand Down

0 comments on commit fd9d78c

Please sign in to comment.