|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { parseRobots } from '@/connectors/website/robots' |
| 3 | + |
| 4 | +describe('robots.txt rules for the website connector', () => { |
| 5 | + it('lets the longest matching rule decide, with Allow winning a tie', () => { |
| 6 | + const robots = parseRobots( |
| 7 | + [ |
| 8 | + 'User-agent: *', |
| 9 | + 'Disallow: /private', |
| 10 | + 'Allow: /private/handbook', |
| 11 | + 'Disallow: /tie', |
| 12 | + 'Allow: /tie', |
| 13 | + ].join('\n') |
| 14 | + ) |
| 15 | + expect(robots.allows('/private/salaries')).toBe(false) |
| 16 | + expect(robots.allows('/private/handbook/leave')).toBe(true) |
| 17 | + expect(robots.allows('/tie')).toBe(true) |
| 18 | + expect(parseRobots('User-agent: *\nAllow: /foo\nDisallow: /foo$').allows('/foo')).toBe(true) |
| 19 | + expect(robots.allows('/public')).toBe(true) |
| 20 | + }) |
| 21 | + |
| 22 | + it('applies only the group naming this crawler when one exists', () => { |
| 23 | + const robots = parseRobots( |
| 24 | + [ |
| 25 | + 'User-agent: *', |
| 26 | + 'Disallow: /', |
| 27 | + '', |
| 28 | + 'User-agent: OtherBot', |
| 29 | + 'User-agent: SimBot', |
| 30 | + 'Disallow: /drafts', |
| 31 | + ].join('\n') |
| 32 | + ) |
| 33 | + expect(robots.allows('/benefits')).toBe(true) |
| 34 | + expect(robots.allows('/drafts/plan')).toBe(false) |
| 35 | + }) |
| 36 | + |
| 37 | + it('matches wildcards and end anchors against the path and query', () => { |
| 38 | + const robots = parseRobots( |
| 39 | + ['User-agent: *', 'Disallow: /*?print=', 'Disallow: /*.pdf$'].join('\n') |
| 40 | + ) |
| 41 | + expect(robots.allows('/plans?print=1')).toBe(false) |
| 42 | + expect(robots.allows('/guide.pdf')).toBe(false) |
| 43 | + expect(robots.allows('/guide.pdf.html')).toBe(true) |
| 44 | + }) |
| 45 | + |
| 46 | + it('treats an empty Disallow as allowing everything and collects declared sitemaps', () => { |
| 47 | + const robots = parseRobots( |
| 48 | + ['Sitemap: https://example.com/a.xml', 'User-agent: *', 'Disallow:', '# Disallow: /x'].join( |
| 49 | + '\n' |
| 50 | + ) |
| 51 | + ) |
| 52 | + expect(robots.allows('/x')).toBe(true) |
| 53 | + expect(robots.sitemaps).toEqual(['https://example.com/a.xml']) |
| 54 | + }) |
| 55 | + |
| 56 | + it('matches rules and paths in one canonical encoding, so an escape cannot slip past a rule', () => { |
| 57 | + const robots = parseRobots( |
| 58 | + ['User-agent: *', 'Disallow: /private', 'Disallow: /café'].join('\n') |
| 59 | + ) |
| 60 | + expect(robots.allows('/%70rivate/salaries')).toBe(false) |
| 61 | + expect(robots.allows('/caf%C3%A9/menu')).toBe(false) |
| 62 | + expect(robots.allows('/a%2Fprivate')).toBe(true) |
| 63 | + }) |
| 64 | + |
| 65 | + it('matches a pathological wildcard rule against a long path in linear time', () => { |
| 66 | + const robots = parseRobots(`User-agent: *\nDisallow: /${'*a'.repeat(40)}b\n`) |
| 67 | + const started = performance.now() |
| 68 | + expect(robots.allows(`/${'a'.repeat(20_000)}`)).toBe(true) |
| 69 | + expect(performance.now() - started).toBeLessThan(1000) |
| 70 | + }) |
| 71 | + |
| 72 | + it('reads records separated by carriage returns alone', () => { |
| 73 | + expect(parseRobots('User-agent: *\rDisallow: /\r').allows('/anything')).toBe(false) |
| 74 | + }) |
| 75 | + |
| 76 | + it('refuses a policy with more rules than it evaluates rather than dropping some', () => { |
| 77 | + const rules = Array.from({ length: 1001 }, (_, index) => `Disallow: /p${index}`) |
| 78 | + expect(() => parseRobots(['User-agent: *', ...rules].join('\n'))).toThrow('more than') |
| 79 | + }) |
| 80 | +}) |
0 commit comments