|
| 1 | +import { createResolver } from '@nuxt/kit' |
| 2 | +import { setup } from '@nuxt/test-utils' |
| 3 | +import { describe, expect, it } from 'vitest' |
| 4 | + |
| 5 | +const { resolve } = createResolver(import.meta.url) |
| 6 | + |
| 7 | +process.env.NODE_ENV = 'production' |
| 8 | + |
| 9 | +describe('robots:config hook - issue #233', async () => { |
| 10 | + await setup({ |
| 11 | + rootDir: resolve('../../.playground'), |
| 12 | + build: true, |
| 13 | + server: true, |
| 14 | + nuxtConfig: { |
| 15 | + nitro: { |
| 16 | + plugins: [], |
| 17 | + }, |
| 18 | + hooks: { |
| 19 | + 'nitro:config': function (nitroConfig: any) { |
| 20 | + nitroConfig.plugins = nitroConfig.plugins || [] |
| 21 | + nitroConfig.plugins.push(resolve('../fixtures/hook-config/server/plugins/robots.ts')) |
| 22 | + }, |
| 23 | + }, |
| 24 | + }, |
| 25 | + }) |
| 26 | + |
| 27 | + it('generates robots.txt with groups from hook', async () => { |
| 28 | + const robotsTxt = await $fetch('/robots.txt') |
| 29 | + expect(robotsTxt).toContain('Disallow: /_cwa/*') |
| 30 | + expect(robotsTxt).toContain('AhrefsBot') |
| 31 | + }) |
| 32 | + |
| 33 | + it('should NOT block indexable pages when groups are added via hook', async () => { |
| 34 | + // This test demonstrates the bug: pages that should be indexable |
| 35 | + // are incorrectly marked as non-indexable because groups added via |
| 36 | + // the hook are missing the _indexable property |
| 37 | + const { headers: indexHeaders } = await $fetch.raw('/', { |
| 38 | + headers: { |
| 39 | + 'User-Agent': 'Mozilla/5.0', |
| 40 | + }, |
| 41 | + }) |
| 42 | + |
| 43 | + // This page should NOT have noindex header because: |
| 44 | + // 1. The disallow rule is for /_cwa/* which doesn't match / |
| 45 | + // 2. The AhrefsBot rule only applies to AhrefsBot user agent, not Mozilla |
| 46 | + expect(indexHeaders.get('x-robots-tag')).toContain('index') |
| 47 | + expect(indexHeaders.get('x-robots-tag')).not.toContain('noindex') |
| 48 | + }) |
| 49 | + |
| 50 | + it('should correctly block paths matching disallow patterns', async () => { |
| 51 | + // This should be blocked by the /_cwa/* rule even though page doesn't exist |
| 52 | + // We test with ignoreResponseError to capture headers from 404 responses |
| 53 | + const { headers } = await $fetch.raw('/_cwa/test', { |
| 54 | + headers: { |
| 55 | + 'User-Agent': 'Mozilla/5.0', |
| 56 | + }, |
| 57 | + ignoreResponseError: true, |
| 58 | + }) |
| 59 | + |
| 60 | + expect(headers.get('x-robots-tag')).toMatchInlineSnapshot(`"noindex, nofollow"`) |
| 61 | + }) |
| 62 | + |
| 63 | + it('should block AhrefsBot from all paths', async () => { |
| 64 | + const { headers: indexHeaders } = await $fetch.raw('/', { |
| 65 | + headers: { |
| 66 | + 'User-Agent': 'AhrefsBot', |
| 67 | + }, |
| 68 | + }) |
| 69 | + |
| 70 | + // AhrefsBot should be blocked everywhere |
| 71 | + expect(indexHeaders.get('x-robots-tag')).toMatchInlineSnapshot(`"noindex, nofollow"`) |
| 72 | + }) |
| 73 | + |
| 74 | + // Edge case: Multiple hook calls shouldn't cause issues |
| 75 | + it('should handle multiple hook calls without breaking normalization', async () => { |
| 76 | + // Second request - the hook might be called again depending on caching |
| 77 | + const { headers } = await $fetch.raw('/api/test', { |
| 78 | + headers: { |
| 79 | + 'User-Agent': 'Mozilla/5.0', |
| 80 | + }, |
| 81 | + ignoreResponseError: true, |
| 82 | + }) |
| 83 | + |
| 84 | + // Should still work correctly on subsequent requests |
| 85 | + expect(headers.get('x-robots-tag')).toBeDefined() |
| 86 | + }) |
| 87 | + |
| 88 | + // Edge case: Empty user agent header |
| 89 | + it('should handle requests with no user agent gracefully', async () => { |
| 90 | + const { headers } = await $fetch.raw('/', { |
| 91 | + headers: { |
| 92 | + // No User-Agent header |
| 93 | + }, |
| 94 | + }) |
| 95 | + |
| 96 | + // Should still apply rules (defaults to * user agent) |
| 97 | + expect(headers.get('x-robots-tag')).toBeDefined() |
| 98 | + }) |
| 99 | + |
| 100 | + // Edge case: Case sensitivity in user agent matching |
| 101 | + it('should handle user agent case variations', async () => { |
| 102 | + const tests = [ |
| 103 | + { ua: 'ahrefsbot', desc: 'lowercase' }, |
| 104 | + { ua: 'AHREFSBOT', desc: 'uppercase' }, |
| 105 | + { ua: 'AhRefsBot', desc: 'mixed case' }, |
| 106 | + ] |
| 107 | + |
| 108 | + for (const { ua } of tests) { |
| 109 | + const { headers } = await $fetch.raw('/', { |
| 110 | + headers: { |
| 111 | + 'User-Agent': ua, |
| 112 | + }, |
| 113 | + }) |
| 114 | + |
| 115 | + // User agent matching should be case-insensitive |
| 116 | + expect(headers.get('x-robots-tag')).toContain('noindex') |
| 117 | + } |
| 118 | + }) |
| 119 | +}) |
0 commit comments