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
The walk stopped after 200 commits: lines older than f0b094d point at it.
Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 1–227why, one step back1import { Hono } from '../../hono'2import type { RedirectStatusCode, StatusCode } from '../../utils/http-status'3import * as plugins from './plugins'4import { toSSG } from './ssg'5import type { FileSystemModule } from './ssg'67const { defaultPlugin, redirectPlugin } = plugins89describe('Built-in SSG plugins', () => {10 let app: Hono11 let fsMock: FileSystemModule1213 beforeEach(() => {14 app = new Hono()15 app.get('/', (c) => c.html('<h1>Home</h1>'))16 app.get('/about', (c) => c.html('<h1>About</h1>'))17 app.get('/blog', (c) => c.html('<h1>Blog</h1>'))18 app.get('/created', (c) => c.text('201 Created', 201))19 app.get('/redirect', (c) => c.redirect('/'))20 app.get('/notfound', (c) => c.notFound())21 app.get('/error', (c) => c.text('500 Error', 500))2223 fsMock = {24 writeFile: vi.fn(() => Promise.resolve()),25 mkdir: vi.fn(() => Promise.resolve()),26 }27 })2829 describe('default plugin', () => {30 it('uses defaultPlugin when plugins option is omitted', async () => {31 const defaultPluginSpy = vi.spyOn(plugins, 'defaultPlugin')32 await toSSG(app, fsMock, { dir: './static' })33 expect(defaultPluginSpy).toHaveBeenCalled()34 defaultPluginSpy.mockRestore()35 })3637 it('skips non-200 responses with defaultPlugin', async () => {38 const result = await toSSG(app, fsMock, { plugins: [defaultPlugin()], dir: './static' })39 expect(fsMock.writeFile).toHaveBeenCalledWith('static/index.html', '<h1>Home</h1>')40 expect(fsMock.writeFile).toHaveBeenCalledWith('static/about.html', '<h1>About</h1>')41 expect(fsMock.writeFile).toHaveBeenCalledWith('static/blog.html', '<h1>Blog</h1>')42 expect(fsMock.writeFile).not.toHaveBeenCalledWith('static/created.txt', expect.any(String))43 expect(fsMock.writeFile).not.toHaveBeenCalledWith('static/redirect.txt', expect.any(String))44 expect(fsMock.writeFile).not.toHaveBeenCalledWith('static/notfound.txt', expect.any(String))45 expect(fsMock.writeFile).not.toHaveBeenCalledWith('static/error.txt', expect.any(String))46 expect(result.files.some((f) => f.includes('created'))).toBe(false)47 expect(result.files.some((f) => f.includes('redirect'))).toBe(false)48 expect(result.files.some((f) => f.includes('notfound'))).toBe(false)49 expect(result.files.some((f) => f.includes('error'))).toBe(false)50 expect(result.success).toBe(true)51 })52 })5354 describe('redirect plugin', () => {55 it('generates redirect HTML for status codes requiring Location per HTTP Semantics specification', async () => {56 const statusCodes = [301, 302, 303, 307, 308] satisfies RedirectStatusCode[]57 for (const statusCode of statusCodes) {58 const writtenFiles: Record<string, string> = {}59 const fsMockLocal: FileSystemModule = {60 writeFile: (path, data) => {61 writtenFiles[path] = typeof data === 'string' ? data : data.toString()62 return Promise.resolve()63 },64 mkdir: vi.fn(() => Promise.resolve()),65 }66 const redirectApp = new Hono()67 redirectApp.get('/old', (c) => c.redirect('/new', statusCode)) // Default is 30268 redirectApp.get('/new', (c) => c.html('New Page'))6970 await toSSG(redirectApp, fsMockLocal, { dir: './static', plugins: [redirectPlugin()] })7172 expect(writtenFiles['static/old.html']).toBeDefined()73 const content = writtenFiles['static/old.html']74 // Should contain meta refresh75 expect(content).toContain('meta http-equiv="refresh" content="0;url=/new"')76 // Should contain canonical77 expect(content).toContain('rel="canonical" href="/new"')78 // Should contain robots noindex79 expect(content).toContain('<meta name="robots" content="noindex" />')80 // Should contain link anchor81 expect(content).toContain('<a href="/new">Redirecting to <code>/new</code></a>')82 // Should contain a body element that includes the anchor83 expect(content).toMatch(/<body[^>]*>[\s\S]*<a href=\"\/new\">[\s\S]*<\/body>/)84 }85 })8687 it('skips generating redirect HTML for status codes requiring Location when Location header is missing', async () => {88 const statusCodes = [301, 302, 303, 307, 308] satisfies RedirectStatusCode[]89 for (const statusCode of statusCodes) {90 const writtenFiles: Record<string, string> = {}91 const fsMockLocal: FileSystemModule = {92 writeFile: (path, data) => {93 writtenFiles[path] = typeof data === 'string' ? data : data.toString()94 return Promise.resolve()95 },96 mkdir: vi.fn(() => Promise.resolve()),97 }98 const redirectApp = new Hono()99 redirectApp.get('/bad', () => new Response(null, { status: statusCode }))100101 await toSSG(redirectApp, fsMockLocal, { dir: './static', plugins: [redirectPlugin()] })102103 expect(writtenFiles['static/bad.html']).toBeUndefined()104 }105 })106107 it('skips generating redirect HTML for status codes not requiring Location per HTTP Semantics specification', async () => {108 const statusCodes = [300, 304, 305, 306] satisfies RedirectStatusCode[]109 for (const statusCode of statusCodes) {110 const writtenFiles: Record<string, string> = {}111 const fsMockLocal: FileSystemModule = {112 writeFile: (path, data) => {113 writtenFiles[path] = typeof data === 'string' ? data : data.toString()114 return Promise.resolve()115 },116 mkdir: vi.fn(() => Promise.resolve()),117 }118 const redirectApp = new Hono()119120 redirectApp.get(121 '/response',122 () => new Response(null, { status: statusCode, headers: { Location: '/' } })123 )124125 await toSSG(redirectApp, fsMockLocal, { dir: './static', plugins: [redirectPlugin()] })126127 expect(writtenFiles['static/response.html']).toBeUndefined()128 }129 })130131 it('does not apply redirect HTML for non-redirect status codes even with Location header', async () => {132 const statusCodes = [200, 201, 400, 404, 500] satisfies Exclude<133 StatusCode,134 RedirectStatusCode135 >[]136 for (const statusCode of statusCodes) {137 const writtenFiles: Record<string, string> = {}138 const fsMockLocal: FileSystemModule = {139 writeFile: (path, data) => {140 writtenFiles[path] = typeof data === 'string' ? data : data.toString()141 return Promise.resolve()142 },143 mkdir: vi.fn(() => Promise.resolve()),144 }145 const redirectApp = new Hono()146147 redirectApp.get(148 '/response',149 () => new Response('Response Body', { status: statusCode, headers: { Location: '/' } })150 )151152 await toSSG(redirectApp, fsMockLocal, { dir: './static', plugins: [redirectPlugin()] })153154 expect(writtenFiles['static/response.txt']).toBeDefined()155 expect(writtenFiles['static/response.txt']).toBe('Response Body')156 }157 })158159 it('escapes Location header values when generating redirect HTML', async () => {160 const writtenFiles: Record<string, string> = {}161 const fsMockLocal: FileSystemModule = {162 writeFile: (path, data) => {163 writtenFiles[path] = typeof data === 'string' ? data : data.toString()164 return Promise.resolve()165 },166 mkdir: vi.fn(() => Promise.resolve()),167 }168169 const maliciousLocation = '/new"> <script>alert(1)</script>'170 const redirectApp = new Hono()171 redirectApp.get(172 '/evil',173 () => new Response(null, { status: 301, headers: { Location: maliciousLocation } })174 )175176 await toSSG(redirectApp, fsMockLocal, { dir: './static', plugins: [redirectPlugin()] })177178 const content = writtenFiles['static/evil.html']179 expect(content).toBeDefined()180 expect(content).not.toContain('<script>alert(1)</script>')181 expect(content).toContain('<script>alert(1)</script>')182 expect(content).toContain('"')183 })184185 it('redirectPlugin before defaultPlugin generates redirect HTML', async () => {186 const writtenFiles: Record<string, string> = {}187 const fsMockLocal: FileSystemModule = {188 writeFile: (path, data) => {189 writtenFiles[path] = typeof data === 'string' ? data : data.toString()190 return Promise.resolve()191 },192 mkdir: vi.fn(() => Promise.resolve()),193 }194195 const redirectApp = new Hono()196 redirectApp.get('/old', (c) => c.redirect('/new'))197 redirectApp.get('/new', (c) => c.html('New Page'))198199 await toSSG(redirectApp, fsMockLocal, {200 dir: './static',201 plugins: [redirectPlugin(), defaultPlugin()],202 })203 expect(writtenFiles['static/old.html']).toBeDefined()204 })205206 it('redirectPlugin after defaultPlugin does not generate redirect HTML', async () => {207 const writtenFiles: Record<string, string> = {}208 const fsMockLocal: FileSystemModule = {209 writeFile: (path, data) => {210 writtenFiles[path] = typeof data === 'string' ? data : data.toString()211 return Promise.resolve()212 },213 mkdir: vi.fn(() => Promise.resolve()),214 }215216 const redirectApp = new Hono()217 redirectApp.get('/old', (c) => c.redirect('/new'))218 redirectApp.get('/new', (c) => c.html('New Page'))219220 await toSSG(redirectApp, fsMockLocal, {221 dir: './static',222 plugins: [defaultPlugin(), redirectPlugin()],223 })224 expect(writtenFiles['static/old.html']).toBeUndefined()225 })226 })227})