it('.recordingName()', async function() { const { server } = this.polly; let recordingName; server .get('/ping') .recordingName('Override') .intercept((req, res) => { recordingName = req.recordingName; res.sendStatus(200); }); expect((await fetch('/ping')).status).to.equal(200); expect(recordingName).to.equal('Override'); });
it('can register multiple event handlers', async function() { const { server } = this.polly; const stack = []; server .get('/ping') .intercept((req, res) => res.sendStatus(200)) .on('request', () => stack.push(1)) .on('request', () => stack.push(2)) .on('beforeResponse', () => stack.push(3)) .on('beforeResponse', () => stack.push(4)) .on('response', () => stack.push(5)) .on('response', () => stack.push(6)); expect((await fetch('/ping')).status).to.equal(200); expect(stack).to.deep.equal([1, 2, 3, 4, 5, 6]); });
it('.times()', async function() { const { server } = this.polly; let callCount = 0; server .get('/ping') .times(1) .on('request', () => callCount++) .times() .intercept((req, res) => res.sendStatus(200)); expect((await fetch('/ping')).status).to.equal(200); expect(callCount).to.equal(1); expect((await fetch('/ping')).status).to.equal(200); expect(callCount).to.equal(1); });
it('.recordingName() - should reset when called with no arguments', async function() { const { server } = this.polly; let recordingName; server.any().recordingName('Override'); server .get('/ping') .recordingName() .intercept((req, res) => { recordingName = req.recordingName; res.sendStatus(200); }); expect((await fetch('/ping')).status).to.equal(200); expect(recordingName).to.not.equal('Override'); });
it('.intercept(_, { times }) & .on(_, { times })', async function() { const { server } = this.polly; let callCount = 0; const handler = server .get('/ping') .on('request', () => callCount++, { times: 1 }) .intercept((req, res) => res.sendStatus(200), { times: 2 }); expect((await fetch('/ping')).status).to.equal(200); expect(callCount).to.equal(1); expect((await fetch('/ping')).status).to.equal(200); expect(callCount).to.equal(1); expect(handler.has('intercept')).to.be.false; });
it('.filter() + events', async function() { const { server } = this.polly; let num; server .get('/ping') .filter(req => req.query.num === '1') .on('request', req => (num = req.query.num)) .intercept((req, res) => res.sendStatus(201)); server .get('/ping') .filter(req => req.query.num === '2') .on('request', req => (num = req.query.num)) .intercept((req, res) => res.sendStatus(202)); expect((await fetch('/ping?num=1')).status).to.equal(201); expect(num).to.equal('1'); });
it('.configure() - merges all configs', async function() { const { server } = this.polly; let config; server.any().configure({ foo: 'foo' }); server.any().configure({ bar: 'bar' }); server .get('/ping') .configure({ foo: 'baz' }) .intercept((req, res) => { config = req.config; res.sendStatus(200); }); expect((await fetch('/ping')).status).to.equal(200); expect(config).to.include({ foo: 'baz', bar: 'bar' }); });
it('.filter()', async function() { const { server } = this.polly; server .get('/ping') .filter(req => req.query.num === '1') .intercept((req, res) => res.sendStatus(201)); server .get('/ping') .filter(req => req.query.num === '2') .intercept((req, res) => res.sendStatus(202)); expect((await fetch('/ping?num=1')).status).to.equal(201); expect((await fetch('/ping?num=2')).status).to.equal(202); });
it('.filter() - can access params', async function() { const { server } = this.polly; let id; server .get('/ping/:id') .filter(req => { id = req.params.id; return true; }) .intercept((req, res) => res.sendStatus(201)); expect((await fetch('/ping/1')).status).to.equal(201); expect(id).to.equal('1'); });
it('.filter() - can access params', async function() { const { server } = this.polly; let id; server .get('/ping/:id') .filter(req => { id = req.params.id; return true; }) .intercept((req, res) => res.sendStatus(201)); expect((await fetch('/ping/1')).status).to.equal(201); expect(id).to.equal('1'); });
it('.recordingName() - should reset when called with no arguments', async function() { const { server } = this.polly; let recordingName; server.any().recordingName('Override'); server .get('/ping') .recordingName() .intercept((req, res) => { recordingName = req.recordingName; res.sendStatus(200); }); expect((await fetch('/ping')).status).to.equal(200); expect(recordingName).to.not.equal('Override'); });
it('.times()', async function() { const { server } = this.polly; let callCount = 0; server .get('/ping') .times(1) .on('request', () => callCount++) .times() .intercept((req, res) => res.sendStatus(200)); expect((await fetch('/ping')).status).to.equal(200); expect(callCount).to.equal(1); expect((await fetch('/ping')).status).to.equal(200); expect(callCount).to.equal(1); });
it('.recordingName()', async function() { const { server } = this.polly; let recordingName; server .get('/ping') .recordingName('Override') .intercept((req, res) => { recordingName = req.recordingName; res.sendStatus(200); }); expect((await fetch('/ping')).status).to.equal(200); expect(recordingName).to.equal('Override'); });
it('.intercept(_, { times }) & .on(_, { times })', async function() { const { server } = this.polly; let callCount = 0; const handler = server .get('/ping') .on('request', () => callCount++, { times: 1 }) .intercept((req, res) => res.sendStatus(200), { times: 2 }); expect((await fetch('/ping')).status).to.equal(200); expect(callCount).to.equal(1); expect((await fetch('/ping')).status).to.equal(200); expect(callCount).to.equal(1); expect(handler.has('intercept')).to.be.false; });
it('.filter()', async function() { const { server } = this.polly; server .get('/ping') .filter(req => req.query.num === '1') .intercept((req, res) => res.sendStatus(201)); server .get('/ping') .filter(req => req.query.num === '2') .intercept((req, res) => res.sendStatus(202)); expect((await fetch('/ping?num=1')).status).to.equal(201); expect((await fetch('/ping?num=2')).status).to.equal(202); });