Skip to content

Commit

Permalink
test(test_https_allowunmocked) Remove airplane mode (#1209)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlilja authored and gr2m committed Sep 4, 2019
1 parent 3994f4b commit ec8cd96
Showing 1 changed file with 55 additions and 39 deletions.
94 changes: 55 additions & 39 deletions tests/test_https_allowunmocked.js
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()
})

0 comments on commit ec8cd96

Please sign in to comment.