plugins.test.tsx
This file
Path
src/helper/ssg/plugins.test.tsx
227 lines 路 9.3 KiB 路 TypeScript
Last changed by
Not known.
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228import { Hono } from '../../hono'import type { RedirectStatusCode, StatusCode } from '../../utils/http-status'import * as plugins from './plugins'import { toSSG } from './ssg'import type { FileSystemModule } from './ssg'
const { defaultPlugin, redirectPlugin } = plugins
describe('Built-in SSG plugins', () => { let app: Hono let fsMock: FileSystemModule
beforeEach(() => { app = new Hono() app.get('/', (c) => c.html('<h1>Home</h1>')) app.get('/about', (c) => c.html('<h1>About</h1>')) app.get('/blog', (c) => c.html('<h1>Blog</h1>')) app.get('/created', (c) => c.text('201 Created', 201)) app.get('/redirect', (c) => c.redirect('/')) app.get('/notfound', (c) => c.notFound()) app.get('/error', (c) => c.text('500 Error', 500))
fsMock = { writeFile: vi.fn(() => Promise.resolve()), mkdir: vi.fn(() => Promise.resolve()), } })
describe('default plugin', () => { it('uses defaultPlugin when plugins option is omitted', async () => { const defaultPluginSpy = vi.spyOn(plugins, 'defaultPlugin') await toSSG(app, fsMock, { dir: './static' }) expect(defaultPluginSpy).toHaveBeenCalled() defaultPluginSpy.mockRestore() })
it('skips non-200 responses with defaultPlugin', async () => { const result = await toSSG(app, fsMock, { plugins: [defaultPlugin()], dir: './static' }) expect(fsMock.writeFile).toHaveBeenCalledWith('static/index.html', '<h1>Home</h1>') expect(fsMock.writeFile).toHaveBeenCalledWith('static/about.html', '<h1>About</h1>') expect(fsMock.writeFile).toHaveBeenCalledWith('static/blog.html', '<h1>Blog</h1>') expect(fsMock.writeFile).not.toHaveBeenCalledWith('static/created.txt', expect.any(String)) expect(fsMock.writeFile).not.toHaveBeenCalledWith('static/redirect.txt', expect.any(String)) expect(fsMock.writeFile).not.toHaveBeenCalledWith('static/notfound.txt', expect.any(String)) expect(fsMock.writeFile).not.toHaveBeenCalledWith('static/error.txt', expect.any(String)) expect(result.files.some((f) => f.includes('created'))).toBe(false) expect(result.files.some((f) => f.includes('redirect'))).toBe(false) expect(result.files.some((f) => f.includes('notfound'))).toBe(false) expect(result.files.some((f) => f.includes('error'))).toBe(false) expect(result.success).toBe(true) }) })
describe('redirect plugin', () => { it('generates redirect HTML for status codes requiring Location per HTTP Semantics specification', async () => { const statusCodes = [301, 302, 303, 307, 308] satisfies RedirectStatusCode[] for (const statusCode of statusCodes) { const writtenFiles: Record<string, string> = {} const fsMockLocal: FileSystemModule = { writeFile: (path, data) => { writtenFiles[path] = typeof data === 'string' ? data : data.toString() return Promise.resolve() }, mkdir: vi.fn(() => Promise.resolve()), } const redirectApp = new Hono() redirectApp.get('/old', (c) => c.redirect('/new', statusCode)) // Default is 302 redirectApp.get('/new', (c) => c.html('New Page'))
await toSSG(redirectApp, fsMockLocal, { dir: './static', plugins: [redirectPlugin()] })
expect(writtenFiles['static/old.html']).toBeDefined() const content = writtenFiles['static/old.html'] // Should contain meta refresh expect(content).toContain('meta http-equiv="refresh" content="0;url=/new"') // Should contain canonical expect(content).toContain('rel="canonical" href="/new"') // Should contain robots noindex expect(content).toContain('<meta name="robots" content="noindex" />') // Should contain link anchor expect(content).toContain('<a href="/new">Redirecting to <code>/new</code></a>') // Should contain a body element that includes the anchor expect(content).toMatch(/<body[^>]*>[\s\S]*<a href=\"\/new\">[\s\S]*<\/body>/) } })
it('skips generating redirect HTML for status codes requiring Location when Location header is missing', async () => { const statusCodes = [301, 302, 303, 307, 308] satisfies RedirectStatusCode[] for (const statusCode of statusCodes) { const writtenFiles: Record<string, string> = {} const fsMockLocal: FileSystemModule = { writeFile: (path, data) => { writtenFiles[path] = typeof data === 'string' ? data : data.toString() return Promise.resolve() }, mkdir: vi.fn(() => Promise.resolve()), } const redirectApp = new Hono() redirectApp.get('/bad', () => new Response(null, { status: statusCode }))
await toSSG(redirectApp, fsMockLocal, { dir: './static', plugins: [redirectPlugin()] })
expect(writtenFiles['static/bad.html']).toBeUndefined() } })
it('skips generating redirect HTML for status codes not requiring Location per HTTP Semantics specification', async () => { const statusCodes = [300, 304, 305, 306] satisfies RedirectStatusCode[] for (const statusCode of statusCodes) { const writtenFiles: Record<string, string> = {} const fsMockLocal: FileSystemModule = { writeFile: (path, data) => { writtenFiles[path] = typeof data === 'string' ? data : data.toString() return Promise.resolve() }, mkdir: vi.fn(() => Promise.resolve()), } const redirectApp = new Hono()
redirectApp.get( '/response', () => new Response(null, { status: statusCode, headers: { Location: '/' } }) )
await toSSG(redirectApp, fsMockLocal, { dir: './static', plugins: [redirectPlugin()] })
expect(writtenFiles['static/response.html']).toBeUndefined() } })
it('does not apply redirect HTML for non-redirect status codes even with Location header', async () => { const statusCodes = [200, 201, 400, 404, 500] satisfies Exclude< StatusCode, RedirectStatusCode >[] for (const statusCode of statusCodes) { const writtenFiles: Record<string, string> = {} const fsMockLocal: FileSystemModule = { writeFile: (path, data) => { writtenFiles[path] = typeof data === 'string' ? data : data.toString() return Promise.resolve() }, mkdir: vi.fn(() => Promise.resolve()), } const redirectApp = new Hono()
redirectApp.get( '/response', () => new Response('Response Body', { status: statusCode, headers: { Location: '/' } }) )
await toSSG(redirectApp, fsMockLocal, { dir: './static', plugins: [redirectPlugin()] })
expect(writtenFiles['static/response.txt']).toBeDefined() expect(writtenFiles['static/response.txt']).toBe('Response Body') } })
it('escapes Location header values when generating redirect HTML', async () => { const writtenFiles: Record<string, string> = {} const fsMockLocal: FileSystemModule = { writeFile: (path, data) => { writtenFiles[path] = typeof data === 'string' ? data : data.toString() return Promise.resolve() }, mkdir: vi.fn(() => Promise.resolve()), }
const maliciousLocation = '/new"> <script>alert(1)</script>' const redirectApp = new Hono() redirectApp.get( '/evil', () => new Response(null, { status: 301, headers: { Location: maliciousLocation } }) )
await toSSG(redirectApp, fsMockLocal, { dir: './static', plugins: [redirectPlugin()] })
const content = writtenFiles['static/evil.html'] expect(content).toBeDefined() expect(content).not.toContain('<script>alert(1)</script>') expect(content).toContain('<script>alert(1)</script>') expect(content).toContain('"') })
it('redirectPlugin before defaultPlugin generates redirect HTML', async () => { const writtenFiles: Record<string, string> = {} const fsMockLocal: FileSystemModule = { writeFile: (path, data) => { writtenFiles[path] = typeof data === 'string' ? data : data.toString() return Promise.resolve() }, mkdir: vi.fn(() => Promise.resolve()), }
const redirectApp = new Hono() redirectApp.get('/old', (c) => c.redirect('/new')) redirectApp.get('/new', (c) => c.html('New Page'))
await toSSG(redirectApp, fsMockLocal, { dir: './static', plugins: [redirectPlugin(), defaultPlugin()], }) expect(writtenFiles['static/old.html']).toBeDefined() })
it('redirectPlugin after defaultPlugin does not generate redirect HTML', async () => { const writtenFiles: Record<string, string> = {} const fsMockLocal: FileSystemModule = { writeFile: (path, data) => { writtenFiles[path] = typeof data === 'string' ? data : data.toString() return Promise.resolve() }, mkdir: vi.fn(() => Promise.resolve()), }
const redirectApp = new Hono() redirectApp.get('/old', (c) => c.redirect('/new')) redirectApp.get('/new', (c) => c.html('New Page'))
await toSSG(redirectApp, fsMockLocal, { dir: './static', plugins: [defaultPlugin(), redirectPlugin()], }) expect(writtenFiles['static/old.html']).toBeUndefined() }) })})