From c6b8a6460eaa4f764f0d23028a6e93b93f02bf49 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sat, 22 Sep 2018 14:39:08 +0800 Subject: [PATCH] test: avoid DNS pollution on local env closes https://github.com/eggjs/egg/issues/3033 --- package.json | 1 + test/lib/core/dnscache_httpclient.test.js | 29 +++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/package.json b/package.json index 11bd303778..21a2e14c0a 100644 --- a/package.json +++ b/package.json @@ -96,6 +96,7 @@ "lint": "eslint app config lib test *.js", "test": "npm run lint -- --fix && egg-bin pkgfiles && npm run test-local", "test-local": "egg-bin test", + "test-local-changed": "egg-bin test --changed", "cov": "egg-bin cov --timeout 100000", "ci": "npm run lint && egg-bin pkgfiles --check && npm run cov", "doc-server": "doctools server", diff --git a/test/lib/core/dnscache_httpclient.test.js b/test/lib/core/dnscache_httpclient.test.js index d95178cfe6..75bb43b238 100644 --- a/test/lib/core/dnscache_httpclient.test.js +++ b/test/lib/core/dnscache_httpclient.test.js @@ -11,6 +11,7 @@ describe('test/lib/core/dnscache_httpclient.test.js', () => { let app; let url; let host; + let originalDNSServers; before(async () => { app = utils.app('apps/dnscache_httpclient'); @@ -18,9 +19,13 @@ describe('test/lib/core/dnscache_httpclient.test.js', () => { url = await utils.startLocalServer(); url = url.replace('127.0.0.1', 'localhost'); host = urlparse(url).host; + originalDNSServers = dns.getServers(); }); afterEach(mm.restore); + afterEach(() => { + dns.setServers(originalDNSServers); + }); it('should ctx.curl work and set host', async () => { await app.httpRequest() @@ -38,6 +43,14 @@ describe('test/lib/core/dnscache_httpclient.test.js', () => { }); it('should throw error when the first dns lookup fail', async () => { + if (!process.env.CI) { + // Avoid Network service provider DNS pollution + // alidns http://www.alidns.com/node-distribution/ + dns.setServers([ + '223.5.5.5', + '223.6.6.6', + ]); + } await app.httpRequest() .get('/?url=' + encodeURIComponent('http://notexists-1111111local-domain.com')) .expect(500) @@ -78,6 +91,14 @@ describe('test/lib/core/dnscache_httpclient.test.js', () => { }); it('should callback style work on domain not exists', done => { + if (!process.env.CI) { + // Avoid Network service provider DNS pollution + // alidns http://www.alidns.com/node-distribution/ + dns.setServers([ + '223.5.5.5', + '223.6.6.6', + ]); + } app.httpclient.curl('http://notexists-1111111local-domain.com', err => { assert(err); assert(err.code === 'ENOTFOUND'); @@ -96,6 +117,14 @@ describe('test/lib/core/dnscache_httpclient.test.js', () => { }); it('should thunk style work on domain not exists', done => { + if (!process.env.CI) { + // Avoid Network service provider DNS pollution + // alidns http://www.alidns.com/node-distribution/ + dns.setServers([ + '223.5.5.5', + '223.6.6.6', + ]); + } app.httpclient.requestThunk('http://notexists-1111111local-domain.com')(err => { assert(err); assert(err.code === 'ENOTFOUND');