Skip to content

Commit

Permalink
Publish v0.1.1
Browse files Browse the repository at this point in the history
- feat: custom responses for cache misses while network access is disabled
- feat: custom responses for unmocked requests while network access is disabled
- feat: `--reset`/`-r` reset flag to restart jambox
- fix: allow `localhost` requests even if network access is disabled #42
  • Loading branch information
ballercat authored Nov 17, 2023
1 parent ffdcc82 commit 76d01ad
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
## 0.1.1 - `--reset` cli arg, better messages

- Add custom responses for cache misses while network access is disabled
- Add custom responses for unmocked requests while network access is disabled
- Add `--reset`/`-r` reset flag to restart jambox
- feat: custom responses for cache misses while network access is disabled
- feat: custom responses for unmocked requests while network access is disabled
- feat: `--reset`/`-r` reset flag to restart jambox
- fix: allow `localhost` requests even if network access is disabled #42

## 0.1.0 - Refactor

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.1.0",
"version": "0.1.1",
"description": "Tool for recording and playing back HTTP requests.",
"bin": {
"jam": "./jam.mjs",
Expand Down
16 changes: 16 additions & 0 deletions src/Jambox.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,24 @@ export default class Jambox extends Emitter {
} else {
await this.proxy
.forAnyRequest()
.matching((req) => {
const url = new URL(req.url);
return url.hostname !== 'localhost';
})
.asPriority(98)
.thenReply(418, 'Network access disabled', '');

// Even if network access is disabled, allow localhost requests (for any port).
// Priority set to 1 in-case the user want's to override this behavior
// See https://github.com/ballercat/jambox/issues/42
await this.proxy
.forAnyRequest()
.matching((req) => {
const url = new URL(req.url);
return url.hostname === 'localhost';
})
.asPriority(1)
.thenPassThrough();
}

if (this.config.paused) {
Expand Down
28 changes: 28 additions & 0 deletions src/__tests__/server.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,34 @@ test.serial('pause', async (t) => {
t.is(res.statusText, 'jambox stub');
});

test.serial('blocked network still allows localhost requests', 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');

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

// works prior to blocking network requests
let response = await fetch(`http://localhost:${APP_PORT}/foobar`, opts);

t.is(response.status, 200);

let json = await response.json();
t.like(json, { path: '/foobar' });

await supertest(t.context.server)
.post('/api/config')
.send({ blockNetworkRequests: true })
.expect(200);

response = await fetch(`http://localhost:${APP_PORT}/foobar`, opts);

t.is(response.status, 200);

json = await response.json();
t.like(json, { path: '/foobar' });
});

// NOTE: This does work but needs a better cache mock
test.serial('server - reset', async (t) => {
t.assert(t.context.server, `Server init error: ${t.context.error?.stack}`);
Expand Down

0 comments on commit 76d01ad

Please sign in to comment.