| 1 | import { expect, test } from './_fixtures'; |
| 2 | |
| 3 | const ATOM = '/atom.xml'; |
| 4 | const RSS = '/rss.xml'; |
| 5 | |
| 6 | test.describe('Feeds', () => { |
| 7 | // Content-Type subtype check uses a loose XML regex. Prerendered .xml |
| 8 | // files are served by extension by static hosts (Astro preview emits |
| 9 | // `text/xml`; Netlify production emits `application/atom+xml` via |
| 10 | // public/_headers). Readers sniff content rather than the subtype. |
| 11 | test(`${ATOM} responds 200 with XML Content-Type`, async ({ request }) => { |
| 12 | const res = await request.get(ATOM); |
| 13 | expect(res.status()).toBe(200); |
| 14 | expect(res.headers()['content-type']).toMatch(/xml/); |
| 15 | }); |
| 16 | |
| 17 | test(`${RSS} responds 200 with XML Content-Type`, async ({ request }) => { |
| 18 | const res = await request.get(RSS); |
| 19 | expect(res.status()).toBe(200); |
| 20 | expect(res.headers()['content-type']).toMatch(/xml/); |
| 21 | }); |
| 22 | |
| 23 | test(`${ATOM} parses as Atom 1.0 with entries`, async ({ request }) => { |
| 24 | const body = await (await request.get(ATOM)).text(); |
| 25 | expect(body).toMatch(/<feed[^>]*xmlns="http:\/\/www\.w3\.org\/2005\/Atom"/); |
| 26 | const entries = body.match(/<entry>/g) ?? []; |
| 27 | expect(entries.length).toBeGreaterThan(10); |
| 28 | }); |
| 29 | |
| 30 | test(`${RSS} parses as RSS 2.0 with items`, async ({ request }) => { |
| 31 | const body = await (await request.get(RSS)).text(); |
| 32 | expect(body).toMatch(/<rss[^>]*version="2\.0"/); |
| 33 | const items = body.match(/<item>/g) ?? []; |
| 34 | expect(items.length).toBeGreaterThan(10); |
| 35 | }); |
| 36 | |
| 37 | test('atom and rss have identical entry counts', async ({ request }) => { |
| 38 | const atomBody = await (await request.get(ATOM)).text(); |
| 39 | const rssBody = await (await request.get(RSS)).text(); |
| 40 | const atomCount = (atomBody.match(/<entry>/g) ?? []).length; |
| 41 | const rssCount = (rssBody.match(/<item>/g) ?? []).length; |
| 42 | expect(atomCount).toBe(rssCount); |
| 43 | }); |
| 44 | |
| 45 | test(`${ATOM} entry links are absolute https URLs`, async ({ request }) => { |
| 46 | const body = await (await request.get(ATOM)).text(); |
| 47 | // Every <link href="..."/> inside an <entry> must be absolute. |
| 48 | const entryBlock = body.split('<entry>').slice(1).join('<entry>'); |
| 49 | const links = [...entryBlock.matchAll(/<link[^>]*href="([^"]+)"/g)].map(m => m[1]); |
| 50 | expect(links.length).toBeGreaterThan(0); |
| 51 | for (const href of links) { |
| 52 | expect(href).toMatch(/^https?:\/\//); |
| 53 | } |
| 54 | }); |
| 55 | |
| 56 | test('feed content does not leak raw MDX imports or JSX', async ({ request }) => { |
| 57 | const body = await (await request.get(ATOM)).text(); |
| 58 | // MDX `import` statements and bare JSX opener tags must be stripped. |
| 59 | expect(body).not.toMatch(/^\s*import\s+\w+\s+from\s+['"]/m); |
| 60 | expect(body).not.toMatch(/<[A-Z][A-Za-z0-9]+\s/); |
| 61 | }); |
| 62 | |
| 63 | test('feed content embeds absolutized root-relative URLs', async ({ request }) => { |
| 64 | const body = await (await request.get(ATOM)).text(); |
| 65 | // Active hrefs/srcs in entry HTML must be absolute. Strip <pre>/<code> |
| 66 | // first: those contain pedagogical HTML examples where literal relative |
| 67 | // paths are intentional (and the `<` is entity-escaped, so they are not |
| 68 | // active markup — they render as text). |
| 69 | const cdataBlocks = [...body.matchAll(/<!\[CDATA\[([\s\S]*?)\]\]>/g)].map(m => m[1]).join('\n'); |
| 70 | const stripped = cdataBlocks |
| 71 | .replace(/<pre\b[\s\S]*?<\/pre>/gi, '') |
| 72 | .replace(/<code\b[\s\S]*?<\/code>/gi, ''); |
| 73 | expect(stripped).not.toMatch(/\s(href|src)="\/[^/]/); |
| 74 | }); |
| 75 | }); |