From 92575b6b3c5731f2fd8289f353c210c683eb65ab Mon Sep 17 00:00:00 2001 From: "Matt R. Wilson" Date: Sun, 15 Mar 2020 20:49:01 -0600 Subject: [PATCH] refactor(test): Chai assert basic auth & body match (#1952) Also migrated a couple tests from request > got. --- tests/test_basic_auth.js | 18 ++--- tests/test_body_match.js | 151 +++++++++++++++++---------------------- 2 files changed, 74 insertions(+), 95 deletions(-) diff --git a/tests/test_basic_auth.js b/tests/test_basic_auth.js index 2d6aab951..e5f9eb630 100644 --- a/tests/test_basic_auth.js +++ b/tests/test_basic_auth.js @@ -1,11 +1,13 @@ 'use strict' +const { expect } = require('chai') const { test } = require('tap') const assertRejects = require('assert-rejects') const nock = require('..') const got = require('./got_client') require('./cleanup_after_each')() +require('./setup') test('basic auth with username and password', async t => { t.beforeEach(done => { @@ -16,16 +18,16 @@ test('basic auth with username and password', async t => { done() }) - await t.test('succeeds when it matches', async tt => { + await t.test('succeeds when it matches', async () => { const response = await got('http://example.test/test', { username: 'foo', password: 'bar', }) - tt.equal(response.statusCode, 200) - tt.equal(response.body, 'Here is the content') + expect(response.statusCode).to.equal(200) + expect(response.body).to.equal('Here is the content') }) - await t.test('fails when it doesnt match', async tt => { + await t.test('fails when it doesnt match', async () => { await assertRejects( got('http://example.test/test'), /Nock: No match for request/ @@ -42,16 +44,16 @@ test('basic auth with username only', async t => { done() }) - await t.test('succeeds when it matches', async tt => { + await t.test('succeeds when it matches', async () => { const response = await got('http://example.test/test', { username: 'foo', password: '', }) - tt.equal(response.statusCode, 200) - tt.equal(response.body, 'Here is the content') + expect(response.statusCode).to.equal(200) + expect(response.body).to.equal('Here is the content') }) - await t.test('fails when it doesnt match', async tt => { + await t.test('fails when it doesnt match', async () => { await assertRejects( got('http://example.test/test'), /Nock: No match for request/ diff --git a/tests/test_body_match.js b/tests/test_body_match.js index 1fb37a207..5cfaa4013 100644 --- a/tests/test_body_match.js +++ b/tests/test_body_match.js @@ -1,14 +1,16 @@ 'use strict' -const assert = require('assert') +const assertRejects = require('assert-rejects') +const { expect } = require('chai') const mikealRequest = require('request') const { test } = require('tap') const nock = require('../') const got = require('./got_client') require('./cleanup_after_each')() +require('./setup') -test('match json body regardless of key ordering', async t => { +test('match json body regardless of key ordering', async () => { const scope = nock('http://example.test') .post('/', { foo: 'bar', bar: 'foo' }) .reply(200, 'Heyyyy!') @@ -17,11 +19,11 @@ test('match json body regardless of key ordering', async t => { body: JSON.stringify({ bar: 'foo', foo: 'bar' }), }) - t.equal(body, 'Heyyyy!') + expect(body).to.equal('Heyyyy!') scope.done() }) -test('match form body reagardless of field ordering', async t => { +test('match form body regardless of field ordering', async () => { const scope = nock('http://example.test') .post('/', { foo: 'bar', bar: 'foo' }) .reply(200, 'Heyyyy!') @@ -30,11 +32,11 @@ test('match form body reagardless of field ordering', async t => { form: { bar: 'foo', foo: 'bar' }, }) - t.equal(body, 'Heyyyy!') + expect(body).to.equal('Heyyyy!') scope.done() }) -test('match json body specified as json string', async t => { +test('match json body specified as json string', async () => { const scope = nock('http://example.test') .post('/', JSON.stringify({ bar: 'foo', foo: 'bar' })) .reply(200, 'Heyyyy!') @@ -43,11 +45,11 @@ test('match json body specified as json string', async t => { body: JSON.stringify({ bar: 'foo', foo: 'bar' }), }) - t.equal(body, 'Heyyyy!') + expect(body).to.equal('Heyyyy!') scope.done() }) -test('match body is regex trying to match string (matches)', async t => { +test('match body is regex trying to match string (matches)', async () => { const scope = nock('http://example.test') .post('/', new RegExp('abc')) .reply(201) @@ -56,11 +58,11 @@ test('match body is regex trying to match string (matches)', async t => { body: JSON.stringify({ nested: { value: 'abc' } }), }) - t.is(statusCode, 201) + expect(statusCode).to.equal(201) scope.done() }) -test('match body is regex trying to match string (does not match)', async t => { +test('match body is regex trying to match string (does not match)', async () => { const scope1 = nock('http://example.test') .post('/', new RegExp('def')) .reply(201) @@ -72,8 +74,8 @@ test('match body is regex trying to match string (does not match)', async t => { body: JSON.stringify({ nested: { value: 'abc' } }), }) - t.is(statusCode, 202) - t.equal(scope1.isDone(), false) + expect(statusCode).to.equal(202) + expect(scope1.isDone()).to.be.false() scope2.done() }) @@ -94,7 +96,7 @@ test('match body with regex', t => { }, function(err, res) { if (err) throw err - assert.strictEqual(res.statusCode, 200) + expect(res.statusCode).to.equal(200) t.end() } ) @@ -117,7 +119,7 @@ test('match body (with space character) with regex', t => { }, function(err, res) { if (err) throw err - assert.strictEqual(res.statusCode, 200) + expect(res.statusCode).to.equal(200) t.end() } ) @@ -142,7 +144,7 @@ test('match body with regex inside array', t => { }, function(err, res) { if (err) throw err - assert.strictEqual(res.statusCode, 200) + expect(res.statusCode).to.equal(200) t.end() } ) @@ -163,7 +165,7 @@ test('match body with empty object inside', t => { }, function(err, res) { if (err) throw err - assert.strictEqual(res.statusCode, 200) + expect(res.statusCode).to.equal(200) t.end() } ) @@ -186,50 +188,29 @@ test('match body with nested object inside', t => { }, function(err, res) { if (err) throw err - assert.strictEqual(res.statusCode, 200) + expect(res.statusCode).to.equal(200) t.end() } ) }) -test("doesn't match body with mismatching keys", t => { +test("doesn't match body with mismatching keys", async () => { nock('http://example.test') .post('/', { a: 'a' }) .reply(200) - mikealRequest( - { - url: 'http://example.test', - method: 'post', - json: { - a: 'a', - b: 'b', - }, - }, - function(err) { - assert.ok(err) - t.end() - } - ) + const request = got.post('http://example.test', { json: { a: 'a', b: 'b' } }) + await assertRejects(request, /Nock: No match for request/) }) // https://github.com/nock/nock/issues/1713 -test("doesn't match body with same number of keys but different keys", t => { +test("doesn't match body with same number of keys but different keys", async () => { nock('http://example.test') .post('/', { a: {} }) .reply() - mikealRequest( - { - url: 'http://example.test', - method: 'post', - json: { b: 123 }, - }, - function(err) { - assert.ok(err) - t.end() - } - ) + const request = got.post('http://example.test', { json: { b: 123 } }) + await assertRejects(request, /Nock: No match for request/) }) test('match body with form multipart', t => { @@ -247,7 +228,7 @@ test('match body with form multipart', t => { }, function(err, res) { if (err) throw err - assert.strictEqual(res.statusCode, 200) + expect(res.statusCode).to.equal(200) t.end() } ) @@ -256,8 +237,8 @@ test('match body with form multipart', t => { form.append('field', 'value') }) -test('array like urlencoded form posts are correctly parsed', t => { - nock('http://example.test') +test('array like urlencoded form posts are correctly parsed', async () => { + const scope = nock('http://example.test') .post('/', { arrayLike: [ { @@ -267,26 +248,28 @@ test('array like urlencoded form posts are correctly parsed', t => { }, ], }) - .reply(200) + .reply() - mikealRequest( - { - url: 'http://example.test', - method: 'post', - form: { - 'arrayLike[0].fieldA': '0', - 'arrayLike[0].fieldB': 'data', - 'arrayLike[0].fieldC': 'value', - }, + const { statusCode } = await got.post('http://example.test', { + form: { + 'arrayLike[0].fieldA': '0', + 'arrayLike[0].fieldB': 'data', + 'arrayLike[0].fieldC': 'value', }, - function(err, res) { - if (err) throw err - assert.strictEqual(res.statusCode, 200) - t.end() - } - ) + }) + + expect(statusCode).to.equal(200) + scope.done() }) +// This test pokes at an inherent shortcoming of URL encoded form data. "technically" form data values +// can ONLY be strings. However, years of HTML abuse have lead to non-standard ways of handling more complex data. +// https://url.spec.whatwg.org/#urlencoded-serializing +// > The application/x-www-form-urlencoded format is in many ways an aberrant monstrosity... +// Mikeal's Request uses `querystring` by default, optionally `qs` or `form-data`. Got uses `URLSearchParams`. +// All of which handle "arrays" as values differently. +// Nock uses `querystring`, as the consensus seems to be that it's the most widely used and intuitive, but it means +// this test would fail if converted to Got. test('urlencoded form posts are matched with non-string values', t => { nock('http://example.test') .post('/', { @@ -294,7 +277,7 @@ test('urlencoded form posts are matched with non-string values', t => { number: 1, values: [false, -1, 'test'], }) - .reply(200) + .reply() mikealRequest( { @@ -308,33 +291,27 @@ test('urlencoded form posts are matched with non-string values', t => { }, function(err, res) { if (err) throw err - assert.strictEqual(res.statusCode, 200) + expect(res.statusCode).to.equal(200) t.end() } ) }) -test('urlencoded form posts are matched with regexp', t => { - nock('http://example.test') +test('urlencoded form posts are matched with regexp', async () => { + const scope = nock('http://example.test') .post('/', { regexp: /^xyz$/, }) - .reply(200) + .reply() - mikealRequest( - { - url: 'http://example.test', - method: 'post', - form: { - regexp: 'xyz', - }, + const { statusCode } = await got.post('http://example.test', { + form: { + regexp: 'xyz', }, - function(err, res) { - if (err) throw err - assert.strictEqual(res.statusCode, 200) - t.end() - } - ) + }) + + expect(statusCode).to.equal(200) + scope.done() }) test('match utf-8 buffer body with utf-8 buffer', t => { @@ -351,7 +328,7 @@ test('match utf-8 buffer body with utf-8 buffer', t => { }, function(err, res) { if (err) throw err - assert.strictEqual(res.statusCode, 200) + expect(res.statusCode).to.equal(200) t.end() } ) @@ -370,7 +347,7 @@ test("doesn't match utf-8 buffer body with mismatching utf-8 buffer", t => { body: Buffer.from('hello'), }, function(err) { - assert.ok(err) + expect(err).to.be.an('Error') t.end() } ) @@ -390,7 +367,7 @@ test('match binary buffer body with binary buffer', t => { }, function(err, res) { if (err) throw err - assert.strictEqual(res.statusCode, 200) + expect(res.statusCode).to.equal(200) t.end() } ) @@ -409,7 +386,7 @@ test("doesn't match binary buffer body with mismatching binary buffer", t => { body: Buffer.from([0xff, 0xff, 0xff]), }, function(err) { - assert.ok(err) + expect(err).to.be.an('Error') t.end() } ) @@ -430,7 +407,7 @@ test("doesn't match binary buffer body with mismatching utf-8 buffer", t => { body: Buffer.from('hello'), }, function(err) { - assert.ok(err) + expect(err).to.be.an('Error') t.end() } ) @@ -449,7 +426,7 @@ test("doesn't match utf-8 buffer body with mismatching binary buffer", t => { body: Buffer.from([0xff, 0xff, 0xff]), }, function(err) { - assert.ok(err) + expect(err).to.be.an('Error') t.end() } )