From d556ed6b9ff7b524581a1edd403f4591151144e1 Mon Sep 17 00:00:00 2001 From: Rhys Evans Date: Wed, 20 May 2020 23:02:46 +0100 Subject: [PATCH] tidy routing tests --- test/specs/routing/body-matching.test.js | 38 ++++---- test/specs/routing/header-matching.test.js | 90 ++++++++++++------- test/specs/routing/method-matching.test.js | 26 +++--- .../routing/query-string-matching.test.js | 88 ++++++++++-------- 4 files changed, 149 insertions(+), 93 deletions(-) diff --git a/test/specs/routing/body-matching.test.js b/test/specs/routing/body-matching.test.js index bd4fdb37..c5ef3c98 100644 --- a/test/specs/routing/body-matching.test.js +++ b/test/specs/routing/body-matching.test.js @@ -12,7 +12,7 @@ describe('body matching', () => { afterEach(() => fm.restore()); it('should not match if no body provided in request', async () => { - fm.mock('http://a.com/', 200, { body: { foo: 'bar' } }).catch(); + fm.mock({ body: { foo: 'bar' } }, 200).catch(); await fm.fetchHandler('http://a.com/', { method: 'POST', @@ -21,7 +21,7 @@ describe('body matching', () => { }); it('should match if no content type is specified', async () => { - fm.mock('http://a.com/', 200, { body: { foo: 'bar' } }).catch(); + fm.mock({ body: { foo: 'bar' } }, 200).catch(); await fm.fetchHandler('http://a.com/', { method: 'POST', @@ -31,7 +31,7 @@ describe('body matching', () => { }); it('should match when using Request', async () => { - fm.mock('http://a.com/', 200, { body: { foo: 'bar' } }).catch(); + fm.mock({ body: { foo: 'bar' } }, 200).catch(); await fm.fetchHandler( new fm.config.Request('http://a.com/', { @@ -43,7 +43,7 @@ describe('body matching', () => { }); it('should match if body sent matches expected body', async () => { - fm.mock('http://a.com/', 200, { body: { foo: 'bar' } }).catch(); + fm.mock({ body: { foo: 'bar' } }, 200).catch(); await fm.fetchHandler('http://a.com/', { method: 'POST', @@ -54,7 +54,7 @@ describe('body matching', () => { }); it('should not match if body sent doesn’t match expected body', async () => { - fm.mock('http://a.com/', 200, { body: { foo: 'bar' } }).catch(); + fm.mock({ body: { foo: 'bar' } }, 200).catch(); await fm.fetchHandler('http://a.com/', { method: 'POST', @@ -65,7 +65,7 @@ describe('body matching', () => { }); it('should not match if body sent isn’t JSON', async () => { - fm.mock('http://a.com/', 200, { body: { foo: 'bar' } }).catch(); + fm.mock({ body: { foo: 'bar' } }, 200).catch(); await fm.fetchHandler('http://a.com/', { method: 'POST', @@ -76,12 +76,15 @@ describe('body matching', () => { }); it('should ignore the order of the keys in the body', async () => { - fm.mock('http://a.com/', 200, { - body: { - foo: 'bar', - baz: 'qux', + fm.mock( + { + body: { + foo: 'bar', + baz: 'qux', + }, }, - }).catch(); + 200 + ).catch(); await fm.fetchHandler('http://a.com/', { method: 'POST', @@ -95,12 +98,15 @@ describe('body matching', () => { }); it('should ignore the body option matcher if request was GET', async () => { - fm.mock('http://a.com/', 200, { - body: { - foo: 'bar', - baz: 'qux', + fm.mock( + { + body: { + foo: 'bar', + baz: 'qux', + }, }, - }).catch(); + 200 + ).catch(); await fm.fetchHandler('http://a.com/'); expect(fm.calls(true).length).to.equal(1); diff --git a/test/specs/routing/header-matching.test.js b/test/specs/routing/header-matching.test.js index 40c7b26e..038d8380 100644 --- a/test/specs/routing/header-matching.test.js +++ b/test/specs/routing/header-matching.test.js @@ -12,18 +12,24 @@ describe('header matching', () => { afterEach(() => fm.restore()); it('not match when headers not present', async () => { - fm.mock('http://a.com/', 200, { - headers: { a: 'b' }, - }).catch(); + fm.mock( + { + headers: { a: 'b' }, + }, + 200 + ).catch(); await fm.fetchHandler('http://a.com/'); expect(fm.calls(true).length).to.equal(0); }); it("not match when headers don't match", async () => { - fm.mock('http://a.com/', 200, { - headers: { a: 'b' }, - }).catch(); + fm.mock( + { + headers: { a: 'b' }, + }, + 200 + ).catch(); await fm.fetchHandler('http://a.com/', { headers: { a: 'c' }, @@ -32,9 +38,12 @@ describe('header matching', () => { }); it('match simple headers', async () => { - fm.mock('http://a.com/', 200, { - headers: { a: 'b' }, - }).catch(); + fm.mock( + { + headers: { a: 'b' }, + }, + 200 + ).catch(); await fm.fetchHandler('http://a.com/', { headers: { a: 'b' }, @@ -43,9 +52,12 @@ describe('header matching', () => { }); it('be case insensitive', async () => { - fm.mock('http://a.com/', 200, { - headers: { a: 'b' }, - }).catch(); + fm.mock( + { + headers: { a: 'b' }, + }, + 200 + ).catch(); await fm.fetchHandler('http://a.com/', { headers: { A: 'b' }, @@ -54,9 +66,12 @@ describe('header matching', () => { }); it('match multivalue headers', async () => { - fm.mock('http://a.com/', 200, { - headers: { a: ['b', 'c'] }, - }).catch(); + fm.mock( + { + headers: { a: ['b', 'c'] }, + }, + 200 + ).catch(); await fm.fetchHandler('http://a.com/', { headers: { a: ['b', 'c'] }, @@ -65,9 +80,12 @@ describe('header matching', () => { }); it('not match partially satisfied multivalue headers', async () => { - fm.mock('http://a.com/', 200, { - headers: { a: ['b', 'c', 'd'] }, - }).catch(); + fm.mock( + { + headers: { a: ['b', 'c', 'd'] }, + }, + 200 + ).catch(); await fm.fetchHandler('http://a.com/', { headers: { a: ['b', 'c'] }, @@ -76,9 +94,12 @@ describe('header matching', () => { }); it('match multiple headers', async () => { - fm.mock('http://a.com/', 200, { - headers: { a: 'b', c: 'd' }, - }).catch(); + fm.mock( + { + headers: { a: 'b', c: 'd' }, + }, + 200 + ).catch(); await fm.fetchHandler('http://a.com/', { headers: { a: 'b', c: 'd' }, @@ -87,9 +108,12 @@ describe('header matching', () => { }); it('not match unsatisfied multiple headers', async () => { - fm.mock('http://a.com/', 200, { - headers: { a: 'b', c: 'd' }, - }).catch(); + fm.mock( + { + headers: { a: 'b', c: 'd' }, + }, + 200 + ).catch(); await fm.fetchHandler('http://a.com/', { headers: { a: 'b' }, @@ -98,9 +122,12 @@ describe('header matching', () => { }); it('match Headers instance', async () => { - fm.mock('http://a.com/', 200, { - headers: { a: 'b' }, - }).catch(); + fm.mock( + { + headers: { a: 'b' }, + }, + 200 + ).catch(); await fm.fetchHandler('http://a.com/', { headers: new fm.config.Headers({ a: 'b' }), @@ -123,9 +150,12 @@ describe('header matching', () => { }; customHeaderInstance - .mock('http://a.com/', 200, { - headers: { a: 'b' }, - }) + .mock( + { + headers: { a: 'b' }, + }, + 200 + ) .catch(); await customHeaderInstance.fetchHandler('http://a.com/', { diff --git a/test/specs/routing/method-matching.test.js b/test/specs/routing/method-matching.test.js index 0601a9cf..812ed3e7 100644 --- a/test/specs/routing/method-matching.test.js +++ b/test/specs/routing/method-matching.test.js @@ -12,7 +12,7 @@ describe('method matching', () => { afterEach(() => fm.restore()); it('match any method by default', async () => { - fm.mock('http://a.com/', 200).catch(); + fm.mock('*', 200).catch(); await fm.fetchHandler('http://a.com/', { method: 'GET' }); expect(fm.calls(true).length).to.equal(1); @@ -21,7 +21,7 @@ describe('method matching', () => { }); it('configure an exact method to match', async () => { - fm.mock('http://a.com/', 200, { method: 'POST' }).catch(); + fm.mock({ method: 'POST' }, 200).catch(); await fm.fetchHandler('http://a.com/', { method: 'GET' }); expect(fm.calls(true).length).to.equal(0); @@ -30,29 +30,33 @@ describe('method matching', () => { }); it('match implicit GET', async () => { - fm.mock('http://a.com/', 200, { method: 'GET' }).catch(); + fm.mock({ method: 'GET' }, 200).catch(); await fm.fetchHandler('http://a.com/'); expect(fm.calls(true).length).to.equal(1); }); it('be case insensitive', async () => { - fm.mock('http://a.com/', 200, { method: 'POST' }) - .mock('http://it.at.where/', 200, { method: 'patch' }) - .catch(); + fm.mock({ method: 'POST' }, 200).mock({ method: 'patch' }, 200).catch(); await fm.fetchHandler('http://a.com/', { method: 'post' }); expect(fm.calls(true).length).to.equal(1); - await fm.fetchHandler('http://it.at.where/', { method: 'PATCH' }); + await fm.fetchHandler('http://a.com/', { method: 'PATCH' }); expect(fm.calls(true).length).to.equal(2); }); it('can be used alongside function matchers', async () => { - fm.mock((url) => /person/.test(url), 200, { method: 'POST' }).catch(); - - await fm.fetchHandler('http://domain.com/person'); + fm.mock( + { + method: 'POST', + functionMatcher: (url) => /a\.com/.test(url), + }, + 200 + ).catch(); + + await fm.fetchHandler('http://a.com'); expect(fm.calls(true).length).to.equal(0); - await fm.fetchHandler('http://domain.com/person', { method: 'POST' }); + await fm.fetchHandler('http://a.com', { method: 'POST' }); expect(fm.calls(true).length).to.equal(1); }); }); diff --git a/test/specs/routing/query-string-matching.test.js b/test/specs/routing/query-string-matching.test.js index 0d03ab2a..89197ecb 100644 --- a/test/specs/routing/query-string-matching.test.js +++ b/test/specs/routing/query-string-matching.test.js @@ -13,9 +13,12 @@ describe('query string matching', () => { afterEach(() => fm.restore()); it('match a query string', async () => { - fm.mock('http://a.com/', 200, { - query: { a: 'b', c: 'd' }, - }).catch(); + fm.mock( + { + query: { a: 'b', c: 'd' }, + }, + 200 + ).catch(); await fm.fetchHandler('http://a.com'); expect(fm.calls(true).length).to.equal(0); @@ -24,9 +27,12 @@ describe('query string matching', () => { }); it('match a query string against a URL object', async () => { - fm.mock('http://a.com/path', 200, { - query: { a: 'b', c: 'd' }, - }).catch(); + fm.mock( + { + query: { a: 'b', c: 'd' }, + }, + 200 + ).catch(); const url = new URL.URL('http://a.com/path'); url.searchParams.append('a', 'b'); url.searchParams.append('c', 'd'); @@ -35,18 +41,24 @@ describe('query string matching', () => { }); it('match a query string against a relative path', async () => { - fm.mock('/path', 200, { - query: { a: 'b' }, - }).catch(); + fm.mock( + { + query: { a: 'b' }, + }, + 200 + ).catch(); const url = '/path?a=b'; await fm.fetchHandler(url); expect(fm.calls(true).length).to.equal(1); }); it('match multiple query strings', async () => { - fm.mock('http://a.com/', 200, { - query: { a: 'b', c: 'd' }, - }).catch(); + fm.mock( + { + query: { a: 'b', c: 'd' }, + }, + 200 + ).catch(); await fm.fetchHandler('http://a.com'); expect(fm.calls(true).length).to.equal(0); @@ -59,9 +71,12 @@ describe('query string matching', () => { }); it('match an empty query string', async () => { - fm.mock('http://a.com/', 200, { - query: { a: '' }, - }).catch(); + fm.mock( + { + query: { a: '' }, + }, + 200 + ).catch(); await fm.fetchHandler('http://a.com'); expect(fm.calls(true).length).to.equal(0); @@ -71,14 +86,15 @@ describe('query string matching', () => { it('distinguish between query strings that only partially differ', async () => { expect(() => - fm - .mock('/it-at-there', 200, { query: { a: 'b', c: 'e' } }) - .mock('/it-at-there', 300, { + fm.mock({ query: { a: 'b', c: 'e' } }, 200).mock( + { overwriteRoutes: false, query: { a: 'b', c: 'd' }, - }) + }, + 300 + ) ).not.to.throw(); - const res = await fm.fetchHandler('/it-at-there?a=b&c=d'); + const res = await fm.fetchHandler('http://a.com?a=b&c=d'); expect(res.status).to.equal(300); }); @@ -92,9 +108,9 @@ describe('query string matching', () => { }, 200 ).catch(); - await fm.fetchHandler('/path'); + await fm.fetchHandler('http://a.com'); expect(fm.calls(true).length).to.equal(0); - await fm.fetchHandler('/path?a=1'); + await fm.fetchHandler('http://a.com?a=1'); expect(fm.calls(true).length).to.equal(1); }); @@ -107,9 +123,9 @@ describe('query string matching', () => { }, 200 ).catch(); - await fm.fetchHandler('/path'); + await fm.fetchHandler('http://a.com'); expect(fm.calls(true).length).to.equal(0); - await fm.fetchHandler('/path?a=1.2'); + await fm.fetchHandler('http://a.com?a=1.2'); expect(fm.calls(true).length).to.equal(1); }); @@ -132,11 +148,11 @@ describe('query string matching', () => { 200 ) .catch(); - await fm.fetchHandler('/path'); + await fm.fetchHandler('http://a.com'); expect(fm.calls(true).length).to.equal(0); - await fm.fetchHandler('/path?a=true'); + await fm.fetchHandler('http://a.com?a=true'); expect(fm.calls(true).length).to.equal(1); - await fm.fetchHandler('/path?b=false'); + await fm.fetchHandler('http://a.com?b=false'); expect(fm.calls(true).length).to.equal(2); }); @@ -149,9 +165,9 @@ describe('query string matching', () => { }, 200 ).catch(); - await fm.fetchHandler('/path'); + await fm.fetchHandler('http://a.com'); expect(fm.calls(true).length).to.equal(0); - await fm.fetchHandler('/path?a='); + await fm.fetchHandler('http://a.com?a='); expect(fm.calls(true).length).to.equal(1); }); @@ -164,9 +180,9 @@ describe('query string matching', () => { }, 200 ).catch(); - await fm.fetchHandler('/path'); + await fm.fetchHandler('http://a.com'); expect(fm.calls(true).length).to.equal(0); - await fm.fetchHandler('/path?a='); + await fm.fetchHandler('http://a.com?a='); expect(fm.calls(true).length).to.equal(1); }); @@ -179,9 +195,9 @@ describe('query string matching', () => { }, 200 ).catch(); - await fm.fetchHandler('/path'); + await fm.fetchHandler('http://a.com'); expect(fm.calls(true).length).to.equal(0); - await fm.fetchHandler('/path?a='); + await fm.fetchHandler('http://a.com?a='); expect(fm.calls(true).length).to.equal(1); }); @@ -270,13 +286,13 @@ describe('query string matching', () => { }); it('can be used alongside function matchers', async () => { - fm.mock((url) => /person/.test(url), 200, { + fm.mock((url) => /a\.com/.test(url), 200, { query: { a: 'b' }, }).catch(); - await fm.fetchHandler('http://domain.com/person'); + await fm.fetchHandler('http://a.com'); expect(fm.calls(true).length).to.equal(0); - await fm.fetchHandler('http://domain.com/person?a=b'); + await fm.fetchHandler('http://a.com?a=b'); expect(fm.calls(true).length).to.equal(1); }); });