Skip to content

Handing empty robots.txt string #18

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

Open
wants to merge 1 commit into
base: master
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ const cleanSpaces = (rawString) => rawString.replace(whitespace, '').trim();
const splitOnLines = (string) => string.split(lineEndings);

const robustSplit = (string) => {
return !string.includes('<html>') ? [...string.match(recordSlices)].map(cleanSpaces) : [];
const matches = (null === string || undefined === string) ? null : string.match(recordSlices);
if (!matches) {
return [];
}
return !string.includes('<html>') ? [...matches].map(cleanSpaces) : [];
};

const parseRecord = (line) => {
Expand Down
9 changes: 9 additions & 0 deletions test/parser/can-parse-test-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const exampleRobotsBcc = require('../test-data/example-robots-txt-bcc.js');
const exampleRobotsKarwei = require('../test-data/example-robots-txt-karwei.js');
const exampleRobotsShort = require('../test-data/example-robots-txt-short.js');
const exampleRobotsZalando = require('../test-data/example-robots-txt-zalando.js');
const exampleRobotsEmpty = require('../test-data/example-robots-txt-empty.js');
const parse = require('../../src/parser.js');

const { expect } = chai;
Expand Down Expand Up @@ -66,4 +67,12 @@ describe('can-parse-test-files', () => {
expect(parseResult['screaming frog seo spider'].disallow).to.have.lengthOf(1);
expect(parseResult.sitemaps).to.have.lengthOf(0);
});

it('Should completely parse robots-txt-empty', () => {
const parseResult = parse(exampleRobotsEmpty);
const userAgents = Object.keys(parseResult).filter((val) => val !== 'sitemaps' && val !== 'host');
expect(userAgents).to.have.lengthOf(0);
expect(parseResult).to.have.keys(['sitemaps']);
expect(parseResult.sitemaps).to.have.lengthOf(0);
});
});
1 change: 1 addition & 0 deletions test/test-data/example-robots-txt-empty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "";