feat: Decode URI in target extractor

This commit is contained in:
Joachim Van Herwegen
2020-10-06 10:46:18 +02:00
parent b47dc3f7f6
commit bb28af937b
4 changed files with 35 additions and 3 deletions

View File

@@ -16,7 +16,8 @@ describe('A BasicTargetExtractor', (): void => {
});
it('returns the input URL.', async(): Promise<void> => {
await expect(extractor.handle({ url: 'url', headers: { host: 'test.com' }} as any)).resolves.toEqual({ path: 'http://test.com/url' });
await expect(extractor.handle({ url: 'url', headers: { host: 'test.com' }} as any))
.resolves.toEqual({ path: 'http://test.com/url' });
});
it('uses https protocol if the connection is secure.', async(): Promise<void> => {
@@ -24,4 +25,9 @@ describe('A BasicTargetExtractor', (): void => {
{ url: 'url', headers: { host: 'test.com' }, connection: { encrypted: true } as any } as any,
)).resolves.toEqual({ path: 'https://test.com/url' });
});
it('decodes relevant percent encodings.', async(): Promise<void> => {
await expect(extractor.handle({ url: '/a%20path%26/name', headers: { host: 'test.com' }} as any))
.resolves.toEqual({ path: 'http://test.com/a%20path&/name' });
});
});

View File

@@ -1,5 +1,6 @@
import streamifyArray from 'streamify-array';
import { ensureTrailingSlash, matchingMediaType, readableToString } from '../../../src/util/Util';
import { UnsupportedHttpError } from '../../../src/util/errors/UnsupportedHttpError';
import { ensureTrailingSlash, matchingMediaType, readableToString, toCanonicalUrl } from '../../../src/util/Util';
describe('Util function', (): void => {
describe('ensureTrailingSlash', (): void => {
@@ -31,4 +32,15 @@ describe('Util function', (): void => {
expect(matchingMediaType('text/plain', 'text/turtle')).toBeFalsy();
});
});
describe('toCanonicalUrl', (): void => {
it('makes sure only the necessary parts are encoded.', async(): Promise<void> => {
expect(toCanonicalUrl('http://test.com/a%20path%26/name'))
.toEqual('http://test.com/a%20path&/name');
});
it('errors on invalid URLs.', async(): Promise<void> => {
expect((): any => toCanonicalUrl('notAnUrl')).toThrow(new UnsupportedHttpError('Invalid URL notAnUrl'));
});
});
});