Skip to content

Commit e7e43d3

Browse files
committed
make sure to check stubs with arguments
1 parent 9b5b0a8 commit e7e43d3

3 files changed

Lines changed: 40 additions & 48 deletions

File tree

test/integration/initial-dns-seedlist-discovery/dns_seedlist.test.ts

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ describe('DNS timeout errors', () => {
2323

2424
beforeEach(async function () {
2525
client = new MongoClient(CONNECTION_STRING, { serverSelectionTimeoutMS: 2000, tls: false });
26+
stub = sinon.stub(dns.promises, 'resolve').callThrough();
2627
});
2728

2829
afterEach(async function () {
@@ -38,8 +39,6 @@ describe('DNS timeout errors', () => {
3839

3940
describe('when SRV record look up times out', () => {
4041
beforeEach(() => {
41-
stub = sinon.stub(dns.promises, 'resolve').callThrough();
42-
4342
stub
4443
.withArgs(sinon.match.string, 'SRV')
4544
.onFirstCall()
@@ -50,14 +49,12 @@ describe('DNS timeout errors', () => {
5049

5150
it('retries timeout error', metadata, async () => {
5251
await client.connect();
53-
expect(stub).to.have.been.calledTwice;
52+
expect(stub.withArgs(sinon.match.string, 'SRV')).to.have.been.calledTwice;
5453
});
5554
});
5655

5756
describe('when TXT record look up times out', () => {
5857
beforeEach(() => {
59-
stub = sinon.stub(dns.promises, 'resolve').callThrough();
60-
6158
stub
6259
.withArgs(sinon.match.string, 'TXT')
6360
.onFirstCall()
@@ -68,14 +65,12 @@ describe('DNS timeout errors', () => {
6865

6966
it('retries timeout error', metadata, async () => {
7067
await client.connect();
71-
expect(stub).to.have.been.calledTwice;
68+
expect(stub.withArgs(sinon.match.string, 'TXT')).to.have.been.calledTwice;
7269
});
7370
});
7471

7572
describe('when SRV record look up times out twice', () => {
7673
beforeEach(() => {
77-
stub = sinon.stub(dns.promises, 'resolve').callThrough();
78-
7974
stub
8075
.withArgs(sinon.match.string, 'SRV')
8176
.onFirstCall()
@@ -87,14 +82,12 @@ describe('DNS timeout errors', () => {
8782
it('throws timeout error', metadata, async () => {
8883
const error = await client.connect().catch(error => error);
8984
expect(error).to.be.instanceOf(DNSTimeoutError);
90-
expect(stub).to.have.been.calledTwice;
85+
expect(stub.withArgs(sinon.match.string, 'SRV')).to.have.been.calledTwice;
9186
});
9287
});
9388

9489
describe('when TXT record look up times out twice', () => {
9590
beforeEach(() => {
96-
stub = sinon.stub(dns.promises, 'resolve').callThrough();
97-
9891
stub
9992
.withArgs(sinon.match.string, 'TXT')
10093
.onFirstCall()
@@ -106,14 +99,12 @@ describe('DNS timeout errors', () => {
10699
it('throws timeout error', metadata, async () => {
107100
const error = await client.connect().catch(error => error);
108101
expect(error).to.be.instanceOf(DNSTimeoutError);
109-
expect(stub).to.have.been.calledTwice;
102+
expect(stub.withArgs(sinon.match.string, 'TXT')).to.have.been.calledTwice;
110103
});
111104
});
112105

113106
describe('when SRV record look up throws a non-timeout error', () => {
114107
beforeEach(() => {
115-
stub = sinon.stub(dns.promises, 'resolve').callThrough();
116-
117108
stub
118109
.withArgs(sinon.match.string, 'SRV')
119110
.onFirstCall()
@@ -125,14 +116,12 @@ describe('DNS timeout errors', () => {
125116
it('throws that error', metadata, async () => {
126117
const error = await client.connect().catch(error => error);
127118
expect(error).to.be.instanceOf(DNSSomethingError);
128-
expect(stub).to.have.been.calledOnce;
119+
expect(stub.withArgs(sinon.match.string, 'TXT')).to.have.been.calledOnce;
129120
});
130121
});
131122

132123
describe('when TXT record look up throws a non-timeout error', () => {
133124
beforeEach(() => {
134-
stub = sinon.stub(dns.promises, 'resolve').callThrough();
135-
136125
stub
137126
.withArgs(sinon.match.string, 'TXT')
138127
.onFirstCall()
@@ -144,7 +133,7 @@ describe('DNS timeout errors', () => {
144133
it('throws that error', metadata, async () => {
145134
const error = await client.connect().catch(error => error);
146135
expect(error).to.be.instanceOf(DNSSomethingError);
147-
expect(stub).to.have.been.calledOnce;
136+
expect(stub.withArgs(sinon.match.string, 'TXT')).to.have.been.calledOnce;
148137
});
149138
});
150139
});

test/integration/initial-dns-seedlist-discovery/initial_dns_seedlist_discovery.prose.test.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {
2424
// this fn stubs DNS resolution to always pass - so we are only checking pre-DNS validation
2525

2626
const stub = sinon.stub(dns.promises, 'resolve');
27-
stub.withArgs(sinon.match.any, 'SRV').callsFake(async () => {
27+
stub.withArgs(sinon.match.string, 'SRV').callsFake(async () => {
2828
return [
2929
{
3030
name: 'resolved.mongo.localhost',
@@ -35,7 +35,7 @@ describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {
3535
];
3636
});
3737

38-
stub.withArgs(sinon.match.any, 'TXT').callsFake(async () => {
38+
stub.withArgs(sinon.match.string, 'TXT').callsFake(async () => {
3939
throw { code: 'ENODATA' };
4040
});
4141

@@ -89,7 +89,7 @@ describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {
8989

9090
beforeEach(async function () {
9191
stub = sinon.stub(dns.promises, 'resolve');
92-
stub.withArgs(sinon.match.any, 'TXT').callsFake(async () => {
92+
stub.withArgs(sinon.match.string, 'TXT').callsFake(async () => {
9393
throw { code: 'ENODATA' };
9494
});
9595
});
@@ -99,7 +99,7 @@ describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {
9999
});
100100

101101
it('an SRV with one domain level causes a runtime error', async function () {
102-
stub.withArgs(sinon.match.any, 'SRV').callsFake(async () => {
102+
stub.withArgs(sinon.match.string, 'SRV').callsFake(async () => {
103103
return [
104104
{
105105
name: 'localhost.mongodb',
@@ -118,7 +118,7 @@ describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {
118118
});
119119

120120
it('an SRV with two domain levels causes a runtime error', async function () {
121-
stub.withArgs(sinon.match.any, 'SRV').callsFake(async () => {
121+
stub.withArgs(sinon.match.string, 'SRV').callsFake(async () => {
122122
return [
123123
{
124124
name: 'test_1.evil.local', // this string only ends with part of the domain, not all of it!
@@ -137,7 +137,7 @@ describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {
137137
});
138138

139139
it('an SRV with three or more domain levels causes a runtime error', async function () {
140-
stub.withArgs(sinon.match.any, 'SRV').callsFake(async () => {
140+
stub.withArgs(sinon.match.string, 'SRV').callsFake(async () => {
141141
return [
142142
{
143143
name: 'blogs.evil.com',
@@ -173,7 +173,7 @@ describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {
173173

174174
beforeEach(async function () {
175175
stub = sinon.stub(dns.promises, 'resolve');
176-
stub.withArgs(sinon.match.any, 'TXT').callsFake(async () => {
176+
stub.withArgs(sinon.match.string, 'TXT').callsFake(async () => {
177177
throw { code: 'ENODATA' };
178178
});
179179
});
@@ -183,7 +183,7 @@ describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {
183183
});
184184

185185
it('an SRV with one domain level causes a runtime error', async function () {
186-
stub.withArgs(sinon.match.any, 'SRV').callsFake(async () => {
186+
stub.withArgs(sinon.match.string, 'SRV').callsFake(async () => {
187187
return [
188188
{
189189
name: 'localhost',
@@ -204,7 +204,7 @@ describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {
204204
});
205205

206206
it('an SRV with two domain levels causes a runtime error', async function () {
207-
stub.withArgs(sinon.match.any, 'SRV').callsFake(async () => {
207+
stub.withArgs(sinon.match.string, 'SRV').callsFake(async () => {
208208
return [
209209
{
210210
name: 'mongo.local',
@@ -243,7 +243,7 @@ describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {
243243

244244
beforeEach(async function () {
245245
stub = sinon.stub(dns.promises, 'resolve');
246-
stub.withArgs(sinon.match.any, 'TXT').callsFake(async () => {
246+
stub.withArgs(sinon.match.string, 'TXT').callsFake(async () => {
247247
throw { code: 'ENODATA' };
248248
});
249249
});
@@ -253,7 +253,7 @@ describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {
253253
});
254254

255255
it('an SRV with one domain level causes a runtime error', async function () {
256-
stub.withArgs(sinon.match.any, 'SRV').callsFake(async () => {
256+
stub.withArgs(sinon.match.string, 'SRV').callsFake(async () => {
257257
return [
258258
{
259259
name: 'test_1.cluster_1localhost',
@@ -272,7 +272,7 @@ describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {
272272
});
273273

274274
it('an SRV with two domain levels causes a runtime error', async function () {
275-
stub.withArgs(sinon.match.any, 'SRV').callsFake(async () => {
275+
stub.withArgs(sinon.match.string, 'SRV').callsFake(async () => {
276276
return [
277277
{
278278
name: 'test_1.my_hostmongo.local',
@@ -291,7 +291,7 @@ describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {
291291
});
292292

293293
it('an SRV with three domain levels causes a runtime error', async function () {
294-
stub.withArgs(sinon.match.any, 'SRV').callsFake(async () => {
294+
stub.withArgs(sinon.match.string, 'SRV').callsFake(async () => {
295295
return [
296296
{
297297
name: 'cluster.testmongodb.com',

test/unit/cmap/auth/gssapi.test.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ describe('GSSAPI', () => {
3232
});
3333
expect(host).to.equal(hostName);
3434
expect(dns.lookup).to.not.be.called;
35-
expect(dns.resolve.withArgs(sinon.match.any, 'PTR')).to.not.be.called;
36-
expect(dns.resolve.withArgs(sinon.match.any, 'CNAME')).to.not.be.called;
35+
expect(dns.resolve.withArgs(sinon.match.string, 'PTR')).to.not.be.called;
36+
expect(dns.resolve.withArgs(sinon.match.string, 'CNAME')).to.not.be.called;
3737
});
3838
});
3939
}
@@ -43,7 +43,7 @@ describe('GSSAPI', () => {
4343

4444
beforeEach(() => {
4545
resolveSpy.restore();
46-
sinon.stub(dns, 'resolve').withArgs(sinon.match.any, 'CNAME').resolves([resolved]);
46+
sinon.stub(dns, 'resolve').withArgs(sinon.match.string, 'CNAME').resolves([resolved]);
4747
});
4848

4949
it('performs a cname lookup', async () => {
@@ -52,7 +52,7 @@ describe('GSSAPI', () => {
5252
});
5353
expect(host).to.equal(resolved);
5454
expect(dns.lookup).to.not.be.called;
55-
expect(dns.resolve.withArgs(sinon.match.any, 'PTR')).to.not.be.called;
55+
expect(dns.resolve.withArgs(sinon.match.string, 'PTR')).to.not.be.called;
5656
expect(dns.resolve).to.be.calledOnceWith(hostName, 'CNAME');
5757
});
5858
});
@@ -73,7 +73,7 @@ describe('GSSAPI', () => {
7373
lookupSpy.restore();
7474
resolveSpy.restore();
7575
sinon.stub(dns, 'lookup').resolves(lookedUp);
76-
sinon.stub(dns, 'resolve').withArgs(sinon.match.any, 'PTR').resolves([resolved]);
76+
sinon.stub(dns, 'resolve').withArgs(sinon.match.string, 'PTR').resolves([resolved]);
7777
});
7878

7979
it('uses the reverse lookup host', async () => {
@@ -96,7 +96,7 @@ describe('GSSAPI', () => {
9696
sinon.stub(dns, 'lookup').resolves(lookedUp);
9797
sinon
9898
.stub(dns, 'resolve')
99-
.withArgs(sinon.match.any, 'PTR')
99+
.withArgs(sinon.match.string, 'PTR')
100100
.resolves([resolved, 'example.com']);
101101
});
102102

@@ -107,7 +107,7 @@ describe('GSSAPI', () => {
107107
expect(host).to.equal(resolved);
108108
expect(dns.lookup).to.be.calledOnceWith(hostName);
109109
expect(dns.resolve).to.be.calledOnceWith(lookedUp.address, 'PTR');
110-
expect(dns.resolve).to.not.be.calledOnceWith(sinon.match.any, 'CNAME');
110+
expect(dns.resolve).to.not.be.calledOnceWith(sinon.match.string, 'CNAME');
111111
});
112112
});
113113
});
@@ -120,8 +120,8 @@ describe('GSSAPI', () => {
120120
resolveSpy.restore();
121121
sinon.stub(dns, 'lookup').resolves(lookedUp);
122122
const stub = sinon.stub(dns, 'resolve');
123-
stub.withArgs(sinon.match.any, 'PTR').rejects(new Error('failed'));
124-
stub.withArgs(sinon.match.any, 'CNAME').resolves([cname]);
123+
stub.withArgs(sinon.match.string, 'PTR').rejects(new Error('failed'));
124+
stub.withArgs(sinon.match.string, 'CNAME').resolves([cname]);
125125
});
126126

127127
it('falls back to a cname lookup', async () => {
@@ -141,7 +141,7 @@ describe('GSSAPI', () => {
141141
lookupSpy.restore();
142142
resolveSpy.restore();
143143
sinon.stub(dns, 'lookup').resolves(lookedUp);
144-
sinon.stub(dns, 'resolve').withArgs(sinon.match.any, 'PTR').resolves([]);
144+
sinon.stub(dns, 'resolve').withArgs(sinon.match.string, 'PTR').resolves([]);
145145
});
146146

147147
it('uses the provided host', async () => {
@@ -151,7 +151,7 @@ describe('GSSAPI', () => {
151151
expect(host).to.equal(hostName);
152152
expect(dns.lookup).to.be.calledOnceWith(hostName);
153153
expect(dns.resolve).to.be.calledOnceWith(lookedUp.address, 'PTR');
154-
expect(dns.resolve).to.not.be.calledWith(sinon.match.any, 'CNAME');
154+
expect(dns.resolve).to.not.be.calledWith(sinon.match.string, 'CNAME');
155155
});
156156
});
157157
});
@@ -169,8 +169,8 @@ describe('GSSAPI', () => {
169169

170170
expect(error.message).to.equal('failed');
171171
expect(dns.lookup).to.be.calledOnceWith(hostName);
172-
expect(dns.resolve).to.not.be.calledWith(sinon.match.any, 'PTR');
173-
expect(dns.resolve).to.not.be.calledWith(sinon.match.any, 'CNAME');
172+
expect(dns.resolve).to.not.be.calledWith(sinon.match.string, 'PTR');
173+
expect(dns.resolve).to.not.be.calledWith(sinon.match.string, 'CNAME');
174174
});
175175
});
176176
});
@@ -183,7 +183,10 @@ describe('GSSAPI', () => {
183183

184184
beforeEach(() => {
185185
resolveSpy.restore();
186-
sinon.stub(dns, 'resolve').withArgs(sinon.match.any, 'CNAME').rejects(new Error('failed'));
186+
sinon
187+
.stub(dns, 'resolve')
188+
.withArgs(sinon.match.string, 'CNAME')
189+
.rejects(new Error('failed'));
187190
});
188191

189192
it('falls back to the provided host name', async () => {
@@ -200,7 +203,7 @@ describe('GSSAPI', () => {
200203

201204
beforeEach(() => {
202205
resolveSpy.restore();
203-
sinon.stub(dns, 'resolve').withArgs(sinon.match.any, 'CNAME').resolves([resolved]);
206+
sinon.stub(dns, 'resolve').withArgs(sinon.match.string, 'CNAME').resolves([resolved]);
204207
});
205208

206209
it('uses the result', async () => {
@@ -218,7 +221,7 @@ describe('GSSAPI', () => {
218221
resolveSpy.restore();
219222
sinon
220223
.stub(dns, 'resolve')
221-
.withArgs(sinon.match.any, 'CNAME')
224+
.withArgs(sinon.match.string, 'CNAME')
222225
.resolves([resolved, hostName]);
223226
});
224227

@@ -235,7 +238,7 @@ describe('GSSAPI', () => {
235238

236239
beforeEach(() => {
237240
resolveSpy.restore();
238-
sinon.stub(dns, 'resolve').withArgs(sinon.match.any, 'CNAME').resolves([]);
241+
sinon.stub(dns, 'resolve').withArgs(sinon.match.string, 'CNAME').resolves([]);
239242
});
240243

241244
it('falls back to using the provided host', async () => {

0 commit comments

Comments
 (0)