Skip to content

Commit

Permalink
wait for reset before config api resolve (#45)
Browse files Browse the repository at this point in the history
Resolves #44

---------

Co-authored-by: Arthur Buldauskas <[email protected]>
  • Loading branch information
ballercat and Arthur Buldauskas authored Nov 20, 2023
1 parent 76d01ad commit 5ab3498
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.2 - Fix config api route

- fix: wait on `reset` event during config api calls

## 0.1.1 - `--reset` cli arg, better messages

- feat: custom responses for cache misses while network access is disabled
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.1",
"version": "0.1.2",
"description": "Tool for recording and playing back HTTP requests.",
"bin": {
"jam": "./jam.mjs",
Expand Down
23 changes: 12 additions & 11 deletions src/Jambox.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export default class Jambox extends Emitter {

async reset() {
debug('Reset');

await this.proxy.reset();
await this.cache.reset({ ...this.config.cache });

Expand Down Expand Up @@ -93,21 +94,21 @@ export default class Jambox extends Emitter {
.thenPassThrough();
}

if (this.config.paused) {
return;
}
if (!this.config.paused) {
if (this.config.cache) {
await this.record(this.config.cache);
}

if (this.config.cache) {
await this.record(this.config.cache);
}
if (this.config.forward) {
await this.forward(this.config.forward);
}

if (this.config.forward) {
await this.forward(this.config.forward);
if (this.config.stub) {
await this.stub(this.config.stub);
}
}

if (this.config.stub) {
await this.stub(this.config.stub);
}
this.dispatch('reset');
}

record(setting) {
Expand Down
7 changes: 6 additions & 1 deletion src/server/routes/__tests__/config.route.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ test('get & post api', async (t) => {

const app = express();
app.use(bodyParser.json());
app.use((req, res, next) => enter({ jambox: { config, reset() {} } }, next));
app.use((req, res, next) =>
enter(
{ jambox: { config, reset() {}, once: () => Promise.resolve() } },
next
)
);
app.use('/api', router);

await supertest(app)
Expand Down
2 changes: 2 additions & 0 deletions src/server/routes/config.route.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ router.get('/config', async (_, res) => res.send(jambox().config.serialize()));
router.post('/config', async (req, res, next) => {
try {
jambox().config.update(req.body);
await jambox().once('jambox.reset');
res.sendStatus(200);
} catch (error) {
next(error);
Expand All @@ -21,6 +22,7 @@ router.post('/pause', async (req, res, next) => {
try {
const { paused } = req.body;
jambox().config.update({ paused });
await jambox().once('jambox.reset');
res.sendStatus(200);
} catch (error) {
next(error);
Expand Down

0 comments on commit 5ab3498

Please sign in to comment.