Skip to content

Commit

Permalink
Merge pull request #1275 from nock/feat/netconnect-1077
Browse files Browse the repository at this point in the history
test(test_intercept.js): Test enableNetConnect without AIRPLANE
  • Loading branch information
RichardLitt committed Dec 14, 2018
2 parents 588c03f + 46dde55 commit f72093c
Showing 1 changed file with 37 additions and 15 deletions.
52 changes: 37 additions & 15 deletions tests/test_intercept.js
Original file line number Diff line number Diff line change
Expand Up @@ -3143,40 +3143,62 @@ test('NetConnectNotAllowedError exposes the stack and has a code', function(t) {
nock.enableNetConnect();
});

// Do not copy tests that rely on the process.env.AIRPLANE, we are deprecating that via #1231
test('enable real HTTP request only for google.com, via string', {skip: process.env.AIRPLANE}, function(t) {
nock.enableNetConnect('google.com');
test('enable real HTTP request only for specified domain, via string', function(t) {
t.plan(1)

http.get('http:https://google.com.br/').on('error', function(err) {
throw err;
const server = http.createServer((request, response) => {
t.pass('server received a request')
response.writeHead(200)
response.end()
t.end()
});
t.once('end', () => server.close());

nock.enableNetConnect('localhost')
t.once('end', () => nock.enableNetConnect());

server.listen(() => mikealRequest(`http:https://localhost:${server.address().port}/`));
});

test('disallow request for other domains, via string', function(t) {
nock.enableNetConnect('localhost');
t.once('end', () => nock.enableNetConnect());

http.get('http:https://www.amazon.com', function(res) {
throw "should not deliver this request"
}).on('error', function (err) {
t.equal(err.message, 'Nock: Disallowed net connect for "www.amazon.com:80/"');
t.end();
});

nock.enableNetConnect();
});

// Do not copy tests that rely on the process.env.AIRPLANE, we are deprecating that via #1231
test('enable real HTTP request only for google.com, via regexp', {skip: process.env.AIRPLANE}, function(t) {
nock.enableNetConnect(/google\.com/);
test('enable real HTTP request only for specified domain, via regexp', function(t) {
t.plan(1)

http.get('http:https://google.com.br/').on('error', function(err) {
throw err;
const server = http.createServer((request, response) => {
t.pass('server received a request')
response.writeHead(200)
response.end()
t.end()
});
t.once('end', () => server.close());

nock.enableNetConnect(/ocalhos/);
t.once('end', () => nock.enableNetConnect());

server.listen(() => mikealRequest(`http:https://localhost:${server.address().port}/`));
});

test('disallow request for other domains, via regexp', function(t) {
nock.enableNetConnect(/ocalhos/);
t.once('end', () => nock.enableNetConnect());

http.get('http:https://www.amazon.com', function(res) {
throw "should not request this";
throw "should not deliver this request"
}).on('error', function (err) {
t.equal(err.message, 'Nock: Disallowed net connect for "www.amazon.com:80/"');
t.end();
});

nock.enableNetConnect();
});

test('repeating once', function(t) {
Expand Down

0 comments on commit f72093c

Please sign in to comment.