Skip to content

Commit

Permalink
test: Update got to the latest version and fill in missing coverage (#…
Browse files Browse the repository at this point in the history
…1825)

* chore(package): update got to version 10.0.0

* chore(package): update lockfile package-lock.json

* Bump got again and run npm install

* query -> searchParams

* encoding -> responseType

* `{body: …}` no longer implies `.post()`

* auth -> username + password

* query -> searchParams

* Error now has a `response` property

* Couple other fixes

* Restore 100% coverage

* Update lockfile

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

* refactor(test): use Mocha DSL for allow unmocked

* Fix format

* baseUrl -> prefixUrl

* Update json’s for breaking changes

* update usage of prefixUrl.

There was a subtle, and I think unnecessary, change in Got 10.x where
using `prefixUrl` means the input, when making a request, can no longer
have a leading slash.
The prefix value can optionally have a trailing slash.
https://github.com/sindresorhus/got#prefixurl

Co-authored-by: Paul Melnikow <github@paulmelnikow.com>
Co-authored-by: Matt R. Wilson <github@mattw.co>
  • Loading branch information
3 people authored and gr2m committed Feb 16, 2020
1 parent 7f9e26c commit b8dd8c8
Show file tree
Hide file tree
Showing 13 changed files with 263 additions and 137 deletions.
239 changes: 155 additions & 84 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
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
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
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
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
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
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
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
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
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
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

0 comments on commit b8dd8c8

Please sign in to comment.