Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(test): use Mocha DSL for allow unmocked #1894

Merged
merged 2 commits into from Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
100 changes: 50 additions & 50 deletions tests/test_allow_unmocked.js
Expand Up @@ -2,14 +2,31 @@

const http = require('http')
const { expect } = require('chai')
const { test } = require('tap')
const nock = require('..')
const got = require('./got_client')

require('./cleanup_after_each')()
require('./setup')

test('with allowUnmocked, mocked request still works', async () => {
describe('allowUnmocked option', () => {
let _server

afterEach(() => {
if (_server) {
_server.close()
_server = null
}
})

async function createServer(requestListener) {
const server = http.createServer(requestListener)
await new Promise(resolve => server.listen(resolve))
_server = server

const url = `http://localhost:${server.address().port}`
return { server, url }
}

it('with allowUnmocked, mocked request still works', async () => {
const scope = nock('http://example.test', { allowUnmocked: true })
.post('/')
.reply(200, '99problems')
Expand All @@ -21,16 +38,11 @@ test('with allowUnmocked, mocked request still works', async () => {
scope.done()
})

test('allow unmocked works after one interceptor is removed', async t => {
const server = http.createServer((request, response) => {
it('allow unmocked works after one interceptor is removed', async () => {
const { url } = await createServer((request, response) => {
response.write('live')
response.end()
})
t.once('end', () => server.close())

await new Promise(resolve => server.listen(resolve))

const url = `http://localhost:${server.address().port}`

nock(url, { allowUnmocked: true })
.get('/')
Expand All @@ -40,8 +52,8 @@ test('allow unmocked works after one interceptor is removed', async t => {
expect((await got(url)).body).to.equal('live')
})

test('allow unmocked option allows traffic to server', async t => {
const server = http.createServer((request, response) => {
it('allow unmocked option allows traffic to server', async () => {
const { url } = await createServer((request, response) => {
switch (request.url) {
case '/':
response.writeHead(200)
Expand All @@ -59,43 +71,35 @@ test('allow unmocked option allows traffic to server', async t => {
response.end()
})

await new Promise(resolve => server.listen(resolve))
t.once('end', () => server.close())

const baseUrl = `http://localhost:${server.address().port}`
const scope = nock(baseUrl, { allowUnmocked: true })
const scope = nock(url, { allowUnmocked: true })
.get('/abc')
.reply(304, 'served from our mock')
.get('/wont/get/here')
.reply(304, 'served from our mock')
const client = got.extend({ baseUrl, throwHttpErrors: false })
const client = got.extend({ baseUrl: url, throwHttpErrors: false })

const response1 = await client(`${baseUrl}/abc`)
const response1 = await client(`${url}/abc`)
expect(response1.statusCode).to.equal(304)
expect(response1.body).to.equal('served from our mock')
expect(scope.isDone()).to.equal(false)

const response2 = await client(`${baseUrl}/not/available`)
const response2 = await client(`${url}/not/available`)
expect(response2.statusCode).to.equal(404)
expect(scope.isDone()).to.equal(false)

const response3 = await client(`${baseUrl}/`)
const response3 = await client(`${url}/`)
expect(response3.statusCode).to.equal(200)
expect(response3.body).to.equal('server served a response')
expect(scope.isDone()).to.equal(false)
})

test('allow unmocked post with json data', async t => {
const server = http.createServer((request, response) => {
it('allow unmocked post with json data', async () => {
const { url } = await createServer((request, response) => {
response.writeHead(200)
response.write('{"message":"server response"}')
response.end()
})

await new Promise(resolve => server.listen(resolve))
t.once('end', () => server.close())

const url = `http://localhost:${server.address().port}`
nock(url, { allowUnmocked: true })
.get('/not/accessed')
.reply(200, '{"message":"mocked response"}')
Expand All @@ -108,17 +112,13 @@ test('allow unmocked post with json data', async t => {
expect(body).to.deep.equal({ message: 'server response' })
})

test('allow unmocked passthrough with mismatched bodies', async t => {
const server = http.createServer((request, response) => {
it('allow unmocked passthrough with mismatched bodies', async () => {
const { url } = await createServer((request, response) => {
response.writeHead(200)
response.write('{"message":"server response"}')
response.end()
})

await new Promise(resolve => server.listen(resolve))
t.once('end', () => server.close())

const url = `http://localhost:${server.address().port}`
nock(url, { allowUnmocked: true })
.post('/post', { some: 'other data' })
.reply(404, '{"message":"server response"}')
Expand All @@ -131,32 +131,37 @@ test('allow unmocked passthrough with mismatched bodies', async t => {
expect(body).to.deep.equal({ message: 'server response' })
})

test('match path using regexp with allowUnmocked', async () => {
it('match path using regexp with allowUnmocked', async () => {
const scope = nock('http://example.test', { allowUnmocked: true })
.get(/regex$/)
.reply(200, 'Match regex')

const { body, statusCode } = await got('http://example.test/resources/regex')
const { body, statusCode } = await got(
'http://example.test/resources/regex'
)
expect(statusCode).to.equal(200)
expect(body).to.equal('Match regex')

scope.done()
})

test('match hostname using regexp with allowUnmocked (issue-1076)', async () => {
// https://github.com/nock/nock/issues/1076
it('match hostname using regexp with allowUnmocked', async () => {
const scope = nock(/localhost/, { allowUnmocked: true })
.get('/no/regex/here')
.reply(200, 'Match regex')

const { body, statusCode } = await got('http://localhost:3000/no/regex/here')
const { body, statusCode } = await got(
'http://localhost:3000/no/regex/here'
)
expect(statusCode).to.equal(200)
expect(body).to.equal('Match regex')

scope.done()
})

// https://github.com/nock/nock/issues/1867
test('match path using callback with allowUnmocked', async t => {
it('match path using callback with allowUnmocked', async () => {
const scope = nock('http://example.test', { allowUnmocked: true })
.get(uri => uri.endsWith('bar'))
.reply()
Expand All @@ -168,14 +173,11 @@ test('match path using callback with allowUnmocked', async t => {
})

// https://github.com/nock/nock/issues/835
test('match multiple paths to domain using regexp with allowUnmocked', async t => {
const server = http.createServer((request, response) => {
it('match multiple paths to domain using regexp with allowUnmocked', async () => {
const { url } = await createServer((request, response) => {
response.write('live')
response.end()
})
t.once('end', () => server.close())
await new Promise(resolve => server.listen(resolve))
const url = `http://localhost:${server.address().port}`

const scope1 = nock(/localhost/, { allowUnmocked: true })
.get(/alpha/)
Expand All @@ -193,7 +195,7 @@ test('match multiple paths to domain using regexp with allowUnmocked', async t =
scope2.done()
})

test('match domain and path with literal query params and allowUnmocked', async t => {
it('match domain and path with literal query params and allowUnmocked', async () => {
const scope = nock('http://example.test', { allowUnmocked: true })
.get('/foo?bar=baz')
.reply()
Expand All @@ -204,7 +206,7 @@ test('match domain and path with literal query params and allowUnmocked', async
scope.done()
})

test('match domain and path using regexp with query params and allowUnmocked', async t => {
it('match domain and path using regexp with query params and allowUnmocked', async () => {
const imgResponse = 'Matched Images Page'

const scope = nock(/example/, { allowUnmocked: true })
Expand All @@ -219,14 +221,11 @@ test('match domain and path using regexp with query params and allowUnmocked', a
})

// https://github.com/nock/nock/issues/490
test('match when query is specified with allowUnmocked', async t => {
const server = http.createServer((request, response) => {
it('match when query is specified with allowUnmocked', async () => {
const { url } = await createServer((request, response) => {
response.write('live')
response.end()
})
t.once('end', () => server.close())
await new Promise(resolve => server.listen(resolve))
const url = `http://localhost:${server.address().port}`

const scope = nock(url, { allowUnmocked: true })
.get('/search')
Expand All @@ -238,3 +237,4 @@ test('match when query is specified with allowUnmocked', async t => {

scope.done()
})
})
45 changes: 19 additions & 26 deletions tests/test_allow_unmocked_https.js
@@ -1,49 +1,43 @@
'use strict'

const { test } = require('tap')
const { expect } = require('chai')
const fs = require('fs')
const https = require('https')
const nock = require('..')
const ssl = require('./ssl')
const got = require('./got_client')

require('./cleanup_after_each')()
require('./setup')

test('Nock with allowUnmocked and an url match', async t => {
const server = https.createServer(
{
key: fs.readFileSync('tests/ssl/ca.key'),
cert: fs.readFileSync('tests/ssl/ca.crt'),
},
(req, res) => {
res.writeHead(200)
res.end({ status: 'default' })
}
)
t.on('end', () => server.close())
describe('allowUnmocked option (https)', () => {
let server

await new Promise(resolve => server.listen(resolve))
afterEach(() => {
if (server) {
server.close()
server = null
}
})

it('Nock with allowUnmocked and an url match', async () => {
server = await ssl.startServer((req, res) => {
res.writeHead(200)
res.end({ status: 'default' })
})
const url = `https://127.0.0.1:${server.address().port}`

const scope = nock(url, { allowUnmocked: true })
.get('/urlMatch')
.reply(201, JSON.stringify({ status: 'intercepted' }))

const { body, statusCode } = await got(`${url}/urlMatch`, {
rejectUnauthorized: false,
})
const { body, statusCode } = await got(`${url}/urlMatch`, { ca: ssl.ca })

expect(statusCode).to.equal(201)
expect(body).to.equal('{"status":"intercepted"}')

scope.done()
})

test('allow unmocked option works with https', async t => {
const server = await ssl.startServer((request, response) => {
it('allow unmocked option works with https', async () => {
server = await ssl.startServer((request, response) => {
if (request.url === '/does/not/exist') {
response.writeHead(404)
response.end()
Expand All @@ -54,7 +48,6 @@ test('allow unmocked option works with https', async t => {
response.write('server response')
response.end()
})
t.once('end', () => server.close())

const { port } = server.address()
const url = `https://localhost:${port}`
Expand Down Expand Up @@ -85,13 +78,13 @@ test('allow unmocked option works with https', async t => {
expect(scope.isDone()).to.equal(false)
})

test('allow unmocked option works with https for a partial match', async () => {
it('allow unmocked option works with https for a partial match', async () => {
// The `allowUnmocked` option is processed in two places. Once in the intercept when there
// are no interceptors that come close to matching the request. And again in the overrider when
// there are interceptors that partially match, eg just path, but don't completely match.
// This explicitly tests the later case in the overrider by making an HTTPS request for a path
// that has an interceptor but fails to match the query constraint.
const server = await ssl.startServer((request, response) => {
server = await ssl.startServer((request, response) => {
response.writeHead(201)
response.write('foo')
response.end()
Expand All @@ -110,5 +103,5 @@ test('allow unmocked option works with https for a partial match', async () => {

expect(statusCode).to.equal(201)
expect(body).to.equal('foo')
server.close()
})
})