Skip to content

Commit

Permalink
Normalize urls in addAsset
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Jul 28, 2022
1 parent 5df2b73 commit 9ae1b02
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
7 changes: 6 additions & 1 deletion lib/AssetGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,14 @@ class AssetGraph extends EventEmitter {
const baseUrl = (incomingRelation && incomingRelation.baseUrl) || this.root;
if (typeof assetConfig === 'string') {
if (/^[a-zA-Z-+]+:/.test(assetConfig)) {
if (!/^data:/i.test(assetConfig)) {
assetConfig = normalizeUrl(assetConfig);
}
assetConfig = { url: assetConfig };
} else {
assetConfig = { url: this.resolveUrl(baseUrl, encodeURI(assetConfig)) };
assetConfig = {
url: this.resolveUrl(baseUrl, normalizeUrl(encodeURI(assetConfig))),
};
}
}
let asset;
Expand Down
29 changes: 29 additions & 0 deletions test/addAsset.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,35 @@ describe('AssetGraph#addAsset', function () {
expect(asset.rawSrc, 'to equal', Buffer.from('Hello, world!\n', 'utf-8'));
});

describe('when called with an absolute url', function () {
it('should normalize the url', function () {
const assetGraph = new AssetGraph();
const asset = assetGraph.addAsset(
'file:///C:/dev/assetgraph/docs/index.html'
);
assetGraph.addAsset(asset);
expect(
asset.url,
'to equal',
'file:///C%3A/dev/assetgraph/docs/index.html'
);
});
});

describe('when called with a relative url', function () {
it('should normalize the url', function () {
const assetGraph = new AssetGraph({ root: 'file:///C:/dev/assetgraph/' });
expect(assetGraph.root, 'to equal', 'file:///C%3A/dev/assetgraph/');
const asset = assetGraph.addAsset('docs/i:n:d:e:x.html');
assetGraph.addAsset(asset);
expect(
asset.url,
'to equal',
'file:///C%3A/dev/assetgraph/docs/i%3An%3Ad%3Ae%3Ax.html'
);
});
});

it('should not loop infinitely when encountering non-resolvable urls', async function () {
const assetGraph = new AssetGraph();
const warnSpy = sinon.spy().named('warn');
Expand Down
5 changes: 2 additions & 3 deletions test/resolvers/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ describe('resolvers/http', function () {
it('should not break when a url contains curly braces', async function () {
httpception({
request:
'GET https://api.amplitude.com/httpapi?api_key=e102edba5e9caea6b89e3c04fac87a4d&event={%22event_type%22:%22Overall%20Active%20User%22,%22event_properties%22:{%22Email%20Send%20Time%22:%222019-05-10T10:43:42%22,%22Current%20Date%22:%2210/05/2019%22,%22UserId%22:5698327,%22campaign%22:%22Daily%20Digest%22,%22Platform%22:%22E-Mail%22},%22user_id%22:%225698327%22}',
'GET https://api.amplitude.com/httpapi?api_key=e102edba5e9caea6b89e3c04fac87a4d&event={%22event_type%22%3A%22Overall%20Active%20User%22,%22event_properties%22%3A{%22Email%20Send%20Time%22%3A%222019-05-10T10%3A43%3A42%22,%22Current%20Date%22%3A%2210/05/2019%22,%22UserId%22%3A5698327,%22campaign%22%3A%22Daily%20Digest%22,%22Platform%22%3A%22E-Mail%22},%22user_id%22%3A%225698327%22}',
response: {
statusCode: 200,
body: '<html><body>Hello, world!</body></html>',
Expand All @@ -132,10 +132,9 @@ describe('resolvers/http', function () {
'https://api.amplitude.com/httpapi?api_key=e102edba5e9caea6b89e3c04fac87a4d&event={%22event_type%22:%22Overall%20Active%20User%22,%22event_properties%22:{%22Email%20Send%20Time%22:%222019-05-10T10:43:42%22,%22Current%20Date%22:%2210/05/2019%22,%22UserId%22:5698327,%22campaign%22:%22Daily%20Digest%22,%22Platform%22:%22E-Mail%22},%22user_id%22:%225698327%22}'
);
await assetGraph.populate();

expect(assetGraph, 'to contain asset', {
type: 'Html',
url: 'https://api.amplitude.com/httpapi?api_key=e102edba5e9caea6b89e3c04fac87a4d&event={%22event_type%22:%22Overall%20Active%20User%22,%22event_properties%22:{%22Email%20Send%20Time%22:%222019-05-10T10:43:42%22,%22Current%20Date%22:%2210/05/2019%22,%22UserId%22:5698327,%22campaign%22:%22Daily%20Digest%22,%22Platform%22:%22E-Mail%22},%22user_id%22:%225698327%22}',
url: 'https://api.amplitude.com/httpapi?api_key=e102edba5e9caea6b89e3c04fac87a4d&event=%7B%22event_type%22%3A%22Overall%20Active%20User%22,%22event_properties%22%3A%7B%22Email%20Send%20Time%22%3A%222019-05-10T10%3A43%3A42%22,%22Current%20Date%22%3A%2210/05/2019%22,%22UserId%22%3A5698327,%22campaign%22%3A%22Daily%20Digest%22,%22Platform%22%3A%22E-Mail%22%7D,%22user_id%22%3A%225698327%22%7D',
});
});
});

0 comments on commit 9ae1b02

Please sign in to comment.