Skip to content

Commit

Permalink
tidy routing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed May 20, 2020
1 parent 0d48926 commit d556ed6
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 93 deletions.
38 changes: 22 additions & 16 deletions test/specs/routing/body-matching.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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/', {
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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);
Expand Down
90 changes: 60 additions & 30 deletions test/specs/routing/header-matching.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand All @@ -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' },
Expand All @@ -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' },
Expand All @@ -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'] },
Expand All @@ -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'] },
Expand All @@ -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' },
Expand All @@ -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' },
Expand All @@ -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' }),
Expand All @@ -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/', {
Expand Down
26 changes: 15 additions & 11 deletions test/specs/routing/method-matching.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
});
});
Loading

0 comments on commit d556ed6

Please sign in to comment.