-
-
Notifications
You must be signed in to change notification settings - Fork 735
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(test_https_allowunmocked) Remove airplane mode (#1209)
- Loading branch information
Showing
1 changed file
with
55 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,69 @@ | ||
'use strict' | ||
|
||
const { test } = require('tap') | ||
const mikealRequest = require('request') | ||
const fs = require('fs') | ||
const https = require('https') | ||
const nock = require('../') | ||
const got = require('got') | ||
|
||
nock.enableNetConnect() | ||
|
||
// Do not copy tests that rely on the process.env.AIRPLANE, we are deprecating that via #1231 | ||
test('allowUnmocked for https', { skip: process.env.AIRPLANE }, function(t) { | ||
nock('https://www.google.com/', { allowUnmocked: true }) | ||
.get('/pathneverhit') | ||
.reply(200, { foo: 'bar' }) | ||
|
||
test('Nock with allowUnmocked and an url match', async t => { | ||
const options = { | ||
method: 'GET', | ||
uri: 'https://www.google.com', | ||
key: fs.readFileSync('tests/ssl/ca.key'), | ||
cert: fs.readFileSync('tests/ssl/ca.crt'), | ||
} | ||
|
||
mikealRequest(options, function(err, resp, body) { | ||
t.notOk(err, 'should be no error') | ||
t.true(typeof body !== 'undefined', 'body should not be undefined') | ||
t.true(body.length !== 0, 'body should not be empty') | ||
t.end() | ||
const server = https | ||
.createServer(options, (req, res) => { | ||
res.writeHead(200) | ||
res.end({ status: 'default' }) | ||
}) | ||
.listen(3000) | ||
|
||
const url = `https://127.0.0.1:${server.address().port}` | ||
|
||
nock(url, { allowUnmocked: true }) | ||
.get('/urlMatch') | ||
.reply(201, JSON.stringify({ status: 'intercepted' })) | ||
|
||
const { body, statusCode } = await got(`${url}/urlMatch`, { | ||
rejectUnauthorized: false, | ||
}) | ||
|
||
t.true(statusCode === 201) | ||
t.true(JSON.parse(body).status === 'intercepted') | ||
|
||
server.close() | ||
}) | ||
|
||
// Do not copy tests that rely on the process.env.AIRPLANE, we are deprecating that via #1231 | ||
test( | ||
'allowUnmocked for https with query test miss', | ||
{ skip: process.env.AIRPLANE }, | ||
function(t) { | ||
nock.cleanAll() | ||
nock('https://www.google.com', { allowUnmocked: true }) | ||
.get('/search') | ||
.query(function() { | ||
return false | ||
}) | ||
.reply(500) | ||
|
||
const options = { | ||
method: 'GET', | ||
uri: 'https://www.google.com/search', | ||
} | ||
|
||
mikealRequest(options, function(err, resp, body) { | ||
t.notOk(err, 'should be no error') | ||
t.true(typeof body !== 'undefined', 'body should not be undefined') | ||
t.true(body.length !== 0, 'body should not be empty') | ||
t.end() | ||
}) | ||
test('Nock with allowUnmocked, url match and query false', async t => { | ||
nock.cleanAll() | ||
|
||
const options = { | ||
key: fs.readFileSync('tests/ssl/ca.key'), | ||
cert: fs.readFileSync('tests/ssl/ca.crt'), | ||
} | ||
) | ||
|
||
const server = https | ||
.createServer(options, (req, res) => { | ||
res.writeHead(200) | ||
res.end(JSON.stringify({ status: 'default' })) | ||
}) | ||
.listen(3000) | ||
|
||
const url = `https://127.0.0.1:3000` | ||
|
||
nock(`${url}`, { allowUnmocked: true }) | ||
.get('/') | ||
.query(false) | ||
.reply(200, { status: 'intercepted' }) | ||
|
||
const { body } = await got(`${url}/otherpath`, { | ||
rejectUnauthorized: false, | ||
}) | ||
|
||
t.true(JSON.parse(body).status === 'default') | ||
|
||
server.close() | ||
}) |