Skip to content

Commit

Permalink
Merge pull request #299 from gemini-testing/database-urls-json-may-co…
Browse files Browse the repository at this point in the history
…ntain-urls-with-search-params

fix: report may refers to other reports by urls with search params
  • Loading branch information
CatWithApple authored Dec 25, 2019
2 parents 861aba2 + 8ed2f99 commit a3b8234
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/server-utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const path = require('path');
const url = require('url');

const chalk = require('chalk');
const _ = require('lodash');
Expand Down Expand Up @@ -107,9 +108,17 @@ async function saveStaticFilesToReportDir(hermione, pluginConfig, destPath) {
]);
}

function urlPathNameEndsWith(currentUrl, searchString) {
try {
return url.parse(currentUrl).pathname.endsWith(searchString);
} catch (e) {
return false;
}
}

async function createDatabaseUrlsFile(destPath, srcPaths) {
const jsonUrls = srcPaths.filter(p => p.endsWith('.json'));
const dbUrls = srcPaths.filter(p => p.endsWith('.db'));
const jsonUrls = srcPaths.filter(p => urlPathNameEndsWith(p, '.json'));
const dbUrls = srcPaths.filter(p => urlPathNameEndsWith(p, '.db'));

const data = {
dbUrls,
Expand Down
52 changes: 52 additions & 0 deletions test/unit/lib/server-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,56 @@ describe('server-utils', () => {
});
});
});

describe('createDatabaseUrlsFile', () => {
beforeEach(() => {
sandbox.stub(fs, 'writeJson').resolves();
});

it('should write json even if source paths are empty', () => {
const destPath = '/foobar';
const srcPaths = [];

utils.createDatabaseUrlsFile(destPath, srcPaths);

assert.calledOnceWith(fs.writeJson, '/foobar/databaseUrls.json', {dbUrls: [], jsonUrls: []});
});

it('should not write invalid urls', () => {
const destPath = '/foo';
const srcPaths = [
null,
'',
'foo',
'foo.bar',
'foo://bar/baz',
'http://foo.bar/baz.bar?test=stub'
];

utils.createDatabaseUrlsFile(destPath, srcPaths);

assert.calledOnceWith(fs.writeJson, sinon.match.any, {dbUrls: [], jsonUrls: []});
});

it('should write relative urls', () => {
const destPath = '/foo';
const srcPaths = ['bar.db', 'bar.json'];

utils.createDatabaseUrlsFile(destPath, srcPaths);

assert.calledOnceWith(fs.writeJson, sinon.match.any, {dbUrls: ['bar.db'], jsonUrls: ['bar.json']});
});

it('should write absolute urls with search params', () => {
const destPath = '/foo';
const srcPaths = ['http://foo.bar/baz.db?test=stub', 'http://foo.bar/baz.json?test=stub'];

utils.createDatabaseUrlsFile(destPath, srcPaths);

assert.calledOnceWith(fs.writeJson, sinon.match.any, {
dbUrls: ['http://foo.bar/baz.db?test=stub'],
jsonUrls: ['http://foo.bar/baz.json?test=stub']
});
});
});
});

0 comments on commit a3b8234

Please sign in to comment.