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

test: Update got to the latest version and fill in missing coverage #1825

Merged
merged 23 commits into from
Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
da7f8aa
chore(package): update got to version 10.0.0
greenkeeper[bot] Dec 1, 2019
c64c9fe
chore(package): update lockfile package-lock.json
greenkeeper[bot] Dec 1, 2019
6f12a73
Merge branch 'master' into greenkeeper/got-10.0.0
paulmelnikow Feb 8, 2020
f31b69e
Bump got again and run npm install
paulmelnikow Feb 8, 2020
8f25142
query -> searchParams
paulmelnikow Feb 8, 2020
c5bb144
encoding -> responseType
paulmelnikow Feb 8, 2020
f6af1ca
`{body: …}` no longer implies `.post()`
paulmelnikow Feb 8, 2020
42e76bf
auth -> username + password
paulmelnikow Feb 8, 2020
98dc792
query -> searchParams
paulmelnikow Feb 8, 2020
afe2b6a
Error now has a `response` property
paulmelnikow Feb 8, 2020
24003dd
Couple other fixes
paulmelnikow Feb 8, 2020
132d7e4
Restore 100% coverage
paulmelnikow Feb 8, 2020
9fb6f24
Merge branch 'beta' into greenkeeper/got-10.0.0
paulmelnikow Feb 11, 2020
3074100
Update lockfile
paulmelnikow Feb 11, 2020
1fae725
refactor(test): use Mocha DSL for allow unmocked (#1894)
mastermatt Feb 11, 2020
a10aced
Fix format
paulmelnikow Feb 11, 2020
2348eeb
baseUrl -> prefixUrl
paulmelnikow Feb 11, 2020
d7b00f8
Update json’s for breaking changes
paulmelnikow Feb 11, 2020
6a655b0
Merge branch 'beta' into greenkeeper/got-10.0.0
mastermatt Feb 11, 2020
69f5935
Merge remote-tracking branch 'upstream/master' into greenkeeper/got-1…
mastermatt Feb 11, 2020
31b13d1
update usage of prefixUrl.
mastermatt Feb 11, 2020
745f85e
Merge branch 'beta' into greenkeeper/got-10.0.0
paulmelnikow Feb 11, 2020
07fedc9
Merge branch 'beta' into greenkeeper/got-10.0.0
paulmelnikow Feb 11, 2020
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
239 changes: 155 additions & 84 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"eslint-plugin-node": "^11.0.0",
"eslint-plugin-promise": "^4.1.1",
"eslint-plugin-standard": "^4.0.0",
"got": "^9.6.0",
"got": "^10.5.2",
"@sinonjs/fake-timers": "^6.0.0",
"mocha": "^7.0.1",
"npm-run-all": "^4.1.5",
Expand Down
16 changes: 8 additions & 8 deletions tests/test_allow_unmocked.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,18 @@ describe('allowUnmocked option', () => {
.reply(304, 'served from our mock')
.get('/wont/get/here')
.reply(304, 'served from our mock')
const client = got.extend({ baseUrl: url, throwHttpErrors: false })
const client = got.extend({ prefixUrl: url, throwHttpErrors: false })

const response1 = await client(`${url}/abc`)
const response1 = await client('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(`${url}/not/available`)
const response2 = await client('not/available')
expect(response2.statusCode).to.equal(404)
expect(scope.isDone()).to.equal(false)

const response3 = await client(`${url}/`)
const response3 = await client('')
expect(response3.statusCode).to.equal(200)
expect(response3.body).to.equal('server served a response')
expect(scope.isDone()).to.equal(false)
Expand All @@ -105,8 +105,8 @@ describe('allowUnmocked option', () => {
.reply(200, '{"message":"mocked response"}')

const { body, statusCode } = await got.post(url, {
json: true,
body: { some: 'data' },
json: { some: 'data' },
responseType: 'json',
})
expect(statusCode).to.equal(200)
expect(body).to.deep.equal({ message: 'server response' })
Expand All @@ -124,8 +124,8 @@ describe('allowUnmocked option', () => {
.reply(404, '{"message":"server response"}')

const { body, statusCode } = await got.post(`${url}/post`, {
json: true,
body: { some: 'data' },
json: { some: 'data' },
responseType: 'json',
})
expect(statusCode).to.equal(200)
expect(body).to.deep.equal({ message: 'server response' })
Expand Down
8 changes: 4 additions & 4 deletions tests/test_allow_unmocked_https.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('allowUnmocked option (https)', () => {
const { port } = server.address()
const url = `https://localhost:${port}`
const client = got.extend({
baseUrl: url,
prefixUrl: url,
ca: ssl.ca,
throwHttpErrors: false,
})
Expand All @@ -63,15 +63,15 @@ describe('allowUnmocked option (https)', () => {
.get('/wont/get/here')
.reply(500)

const response1 = await client('/abc')
const response1 = await client('abc')
expect(response1.statusCode).to.equal(200)
expect(response1.body).to.equal('mocked response')
expect(scope.isDone()).to.equal(false)
const response2 = await client('/does/not/exist')
const response2 = await client('does/not/exist')

expect(response2.statusCode).to.equal(404)
expect(scope.isDone()).to.equal(false)
const response3 = await client('/')
const response3 = await client('')

expect(response3.statusCode).to.equal(200)
expect(response3.body).to.equal('server response')
Expand Down
8 changes: 6 additions & 2 deletions tests/test_basic_auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ test('basic auth with username and password', async t => {

await t.test('succeeds when it matches', async tt => {
const response = await got('http://example.test/test', {
auth: 'foo:bar',
username: 'foo',
password: 'bar',
})
tt.equal(response.statusCode, 200)
tt.equal(response.body, 'Here is the content')
Expand All @@ -42,7 +43,10 @@ test('basic auth with username only', async t => {
})

await t.test('succeeds when it matches', async tt => {
const response = await got('http://example.test/test', { auth: 'foo:' })
const response = await got('http://example.test/test', {
username: 'foo',
password: '',
})
tt.equal(response.statusCode, 200)
tt.equal(response.body, 'Here is the content')
})
Expand Down
3 changes: 1 addition & 2 deletions tests/test_body_match.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ test('match form body reagardless of field ordering', async t => {
.reply(200, 'Heyyyy!')

const { body } = await got.post('http://example.test/', {
form: true,
body: { bar: 'foo', foo: 'bar' },
form: { bar: 'foo', foo: 'bar' },
})

t.equal(body, 'Heyyyy!')
Expand Down
19 changes: 11 additions & 8 deletions tests/test_define.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ test('define() is backward compatible', async t => {
])
)

await assertRejects(got('http://example.test:12345/'), ({ statusCode }) => {
t.is(statusCode, 500)
return true
})
await assertRejects(
got('http://example.test:12345/'),
({ response: { statusCode } }) => {
t.is(statusCode, 500)
return true
}
)
})

test('define() throws when reply is not a numeric string', t => {
Expand Down Expand Up @@ -150,19 +153,19 @@ test('define() works with non-JSON responses', async t => {
)

const { statusCode, body } = await got.post('http://example.test/', {
encoding: false,
body: exampleBody,
responseType: 'buffer',
})

t.equal(statusCode, 200)
// TODO: because `{ encoding: false }` is passed to `got`, `body` should be
// a buffer, but it's a string. Is this a bug in nock or got?
t.equal(body, exampleResponseBody)
t.type(body, Buffer)
t.equal(body.toString(), exampleResponseBody)
})

// TODO: There seems to be a bug here. When testing via `got` with
// `{ encoding: false }` the body that comes back should be a buffer, but is
// not. It's difficult to get this test to pass after porting it.
// I think this bug has been fixed in Got v10, so this should be unblocked.
test('define() works with binary buffers', t => {
const exampleBody = '8001'
const exampleResponse = '8001'
Expand Down
24 changes: 13 additions & 11 deletions tests/test_intercept.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ test('get gets mocked', async t => {
.reply(200, 'Hello World!')

const { statusCode, body } = await got('http://example.test/', {
encoding: null,
responseType: 'buffer',
})

t.equal(statusCode, 200)
Expand All @@ -58,7 +58,7 @@ test('get gets mocked with relative base path', async t => {
.reply(200, 'Hello World!')

const { statusCode, body } = await got('http://example.test/abc/def', {
encoding: null,
responseType: 'buffer',
})

t.equal(statusCode, 200)
Expand All @@ -73,7 +73,7 @@ test('post', async t => {
.reply(201, 'OK!')

const { statusCode, body } = await got.post('http://example.test/form', {
encoding: null,
responseType: 'buffer',
})

t.equal(statusCode, 201)
Expand All @@ -88,7 +88,7 @@ test('post with empty response body', async t => {
.reply()

const { statusCode, body } = await got.post('http://example.test/form', {
encoding: null,
responseType: 'buffer',
})

t.equal(statusCode, 200)
Expand Down Expand Up @@ -137,7 +137,7 @@ test('post with regexp as spec', async t => {
.post('/echo', /key=v.?l/g)
.reply(200, (uri, body) => ['OK', uri, body].join(' '))

const { body } = await got('http://example.test/echo', { body: input })
const { body } = await got.post('http://example.test/echo', { body: input })

t.equal(body, 'OK /echo key=val')
scope.done()
Expand All @@ -148,7 +148,9 @@ test('post with function as spec', async t => {
.post('/echo', body => body === 'key=val')
.reply(200, (uri, body) => ['OK', uri, body].join(' '))

const { body } = await got('http://example.test/echo', { body: 'key=val' })
const { body } = await got.post('http://example.test/echo', {
body: 'key=val',
})

t.equal(body, 'OK /echo key=val')
scope.done()
Expand All @@ -161,7 +163,7 @@ test('post with chaining on call', async t => {
.post('/echo', input)
.reply(200, (uri, body) => ['OK', uri, body].join(' '))

const { body } = await got('http://example.test/echo', { body: input })
const { body } = await got.post('http://example.test/echo', { body: input })

t.equal(body, 'OK /echo key=val')
scope.done()
Expand Down Expand Up @@ -212,11 +214,11 @@ test('body data is differentiating', async t => {
.post('/', 'def')
.reply(200, 'Hey 2')

const response1 = await got('http://example.test/', { body: 'abc' })
const response1 = await got.post('http://example.test/', { body: 'abc' })
t.equal(response1.statusCode, 200)
t.equal(response1.body, 'Hey 1')

const response2 = await got('http://example.test/', { body: 'def' })
const response2 = await got.post('http://example.test/', { body: 'def' })
t.equal(response2.statusCode, 200)
t.equal(response2.body, 'Hey 2')

Expand Down Expand Up @@ -263,7 +265,7 @@ test('on interceptor, filter path with function', async t => {
.reply(200, 'Hello World!')

const { statusCode } = await got('http://example.test/', {
query: { a: '1', b: '2' },
searchParams: { a: '1', b: '2' },
})

t.equal(statusCode, 200)
Expand Down Expand Up @@ -402,7 +404,7 @@ test('can use https', async t => {
.reply()

const { statusCode } = await got('https://example.test/', {
encoding: null,
responseType: 'buffer',
})

t.equal(statusCode, 200)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_query.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ describe('`query()`', () => {
.reply()

const { statusCode } = await got('http://example.test/', {
query: { 'foo&bar': 'hello&world' },
searchParams: { 'foo&bar': 'hello&world' },
})

expect(statusCode).to.equal(200)
Expand Down
55 changes: 50 additions & 5 deletions tests/test_recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ it('records nonstandard ports', done => {
})
})

it('req.end accepts and calls a callback when recording', done => {
it('`req.end()` accepts and calls a callback when recording', done => {
const onEnd = sinon.spy()

server = http.createServer((request, response) => {
Expand Down Expand Up @@ -469,6 +469,51 @@ it('req.end accepts and calls a callback when recording', done => {
})
})

// https://nodejs.org/api/http.html#http_request_end_data_encoding_callback
it('when recording, when `req.end()` is called with only data and a callback, the callback is invoked and the data is sent', done => {
const onEnd = sinon.spy()

let requestBody = ''
server = http.createServer((request, response) => {
request.on('data', data => {
requestBody += data
})
request.on('end', () => {
response.writeHead(200)
response.end()
})
})

nock.restore()
nock.recorder.clear()
expect(nock.recorder.play()).to.be.empty()

server.listen(() => {
nock.recorder.rec({ dont_print: true })

const req = http.request(
{
hostname: 'localhost',
port: server.address().port,
path: '/',
method: 'POST',
},
res => {
expect(onEnd).to.have.been.calledOnce()
expect(res.statusCode).to.equal(200)

res.on('end', () => {
expect(requestBody).to.equal('foobar')
done()
})
res.resume()
}
)

req.end('foobar', onEnd)
})
})

it('rec() throws when reinvoked with already recorder requests', () => {
nock.restore()
nock.recorder.clear()
Expand Down Expand Up @@ -943,7 +988,7 @@ it('records query parameters', async () => {
})

await got(`http://localhost:${server.address().port}`, {
query: { q: 'test search' },
searchParams: { q: 'test search' },
})

nock.restore()
Expand All @@ -970,7 +1015,7 @@ it('encodes the query parameters when not outputting objects', async () => {
})

await got(`http://localhost:${server.address().port}`, {
query: { q: 'test search++' },
searchParams: { q: 'test search++' },
})

nock.restore()
Expand Down Expand Up @@ -1054,7 +1099,7 @@ it('outputs query string parameters using query()', async () => {
nock.recorder.rec(true)

await got(`http://localhost:${server.address().port}/`, {
query: { param1: 1, param2: 2 },
searchParams: { param1: 1, param2: 2 },
})

const recorded = nock.recorder.play()
Expand All @@ -1079,7 +1124,7 @@ it('outputs query string arrays correctly', async () => {
nock.recorder.rec(true)

await got(`http://localhost:${server.address().port}/`, {
query: new URLSearchParams([
searchParams: new URLSearchParams([
['foo', 'bar'],
['foo', 'baz'],
]),
Expand Down
4 changes: 3 additions & 1 deletion tests/test_reply_function_async.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ describe('asynchronous `reply()` function', () => {
callback(null, 'Hello World!')
)

const { body } = await got('http://example.test/', { encoding: null })
const { body } = await got('http://example.test/', {
responseType: 'buffer',
})

expect(body).to.be.an.instanceOf(Buffer)
expect(body.toString('utf8')).to.equal('Hello World!')
Expand Down