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

Accept data: URLs #607

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions src/lib/request-utils.js
@@ -1,7 +1,7 @@
let URL;
// https://stackoverflow.com/a/19709846/308237
// https://stackoverflow.com/a/19709846/308237 plus data: scheme
// split, URL constructor does not support protocol-relative urls
const absoluteUrlRX = new RegExp('^[a-z]+://', 'i');
const absoluteUrlRX = new RegExp('^[a-z]+://|^data:', 'i');
const protocolRelativeUrlRX = new RegExp('^//', 'i');

const headersToArray = (headers) => {
Expand Down
65 changes: 65 additions & 0 deletions test/specs/routing/url-matching.test.js
Expand Up @@ -136,4 +136,69 @@ describe('url matching', () => {
expect(fm.calls(true).length).to.equal(1);
});
});

describe('data: URLs', () => {
it('match exact strings', async () => {
fm.mock('data:text/plain,path', 200).catch();
await fm.fetchHandler('data:text/plain,pat');
await fm.fetchHandler('data:text/plain,paths');
await fm.fetchHandler('data:text/html,path');
expect(fm.calls(true).length).to.equal(0);
await fm.fetchHandler('data:text/plain,path');
expect(fm.calls(true).length).to.equal(1);
});
it('match exact string against URL object', async () => {
fm.mock('data:text/plain,path', 200).catch();
const url = new URL.URL('data:text/plain,path');
await fm.fetchHandler(url);
expect(fm.calls(true).length).to.equal(1);
});
it('match using URL object as matcher', async () => {
const url = new URL.URL('data:text/plain,path');
fm.mock(url, 200).catch();
await fm.fetchHandler('data:text/plain,path');
expect(fm.calls(true).length).to.equal(1);
});
it('match begin: keyword', async () => {
fm.mock('begin:data:text/plain', 200).catch();
await fm.fetchHandler('http://a.com/path');
await fm.fetchHandler('data:text/html,path');
expect(fm.calls(true).length).to.equal(0);
await fm.fetchHandler('data:text/plain,path');
await fm.fetchHandler('data:text/plain;base64,cGF0aA');
expect(fm.calls(true).length).to.equal(2);
});
it('match end: keyword', async () => {
fm.mock('end:sky', 200).catch();
await fm.fetchHandler('data:text/plain,blue lake');
await fm.fetchHandler('data:text/plain,blue sky research');
expect(fm.calls(true).length).to.equal(0);
await fm.fetchHandler('data:text/plain,blue sky');
await fm.fetchHandler('data:text/plain,grey sky');
expect(fm.calls(true).length).to.equal(2);
});
it('match glob: keyword', async () => {
fm.mock('glob:data:* sky', 200).catch();
await fm.fetchHandler('data:text/plain,blue lake');
expect(fm.calls(true).length).to.equal(0);
await fm.fetchHandler('data:text/plain,blue sky');
await fm.fetchHandler('data:text/plain,grey sky');
expect(fm.calls(true).length).to.equal(2);
});
it('match wildcard string', async () => {
fm.mock('*', 200);
await fm.fetchHandler('data:text/plain,path');
expect(fm.calls(true).length).to.equal(1);
});
it('match regular expressions', async () => {
const rx = /data\:text\/plain,\d+/;
fm.mock(rx, 200).catch();
await fm.fetchHandler('data:text/html,12345');
expect(fm.calls(true).length).to.equal(0);
await fm.fetchHandler('data:text/plain,12345');
expect(fm.calls(true).length).to.equal(1);
await fm.fetchHandler('data:text/plain,path');
expect(fm.calls(true).length).to.equal(1);
});
});
});