snapshot of https://github.com/ueberdosis/hocuspocus.git @ cf3cfa355b4d3bb61c6b6c80071f71783e32470e (2026-09-15)
Some checks failed
build / lint (22) (push) Failing after 17s
build / test (22) (push) Failing after 8s
build / lint (23) (push) Failing after 2s
build / build (22) (push) Has been skipped
build / build (23) (push) Has been skipped
build / test (23) (push) Failing after 4s
docsearch / docsearch (push) Failing after 1s
Some checks failed
build / lint (22) (push) Failing after 17s
build / test (22) (push) Failing after 8s
build / lint (23) (push) Failing after 2s
build / build (22) (push) Has been skipped
build / build (23) (push) Has been skipped
build / test (23) (push) Failing after 4s
docsearch / docsearch (push) Failing after 1s
This commit is contained in:
81
tests/server/onRequest.ts
Normal file
81
tests/server/onRequest.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import test from 'ava'
|
||||
import type { onRequestPayload } from '@hocuspocus/server'
|
||||
import { newHocuspocus } from '../utils/index.ts'
|
||||
|
||||
test('executes the onRequest callback', async t => {
|
||||
await new Promise(async resolve => {
|
||||
const hocuspocus = await newHocuspocus(t, {
|
||||
async onRequest({ request }: onRequestPayload) {
|
||||
t.is(request.url, '/foobar')
|
||||
|
||||
resolve('done')
|
||||
},
|
||||
})
|
||||
|
||||
fetch(`${hocuspocus.server!.httpURL}/foobar`).catch(() => {})
|
||||
})
|
||||
})
|
||||
|
||||
test('executes the onRequest callback of a custom extension', async t => {
|
||||
await new Promise(async resolve => {
|
||||
class CustomExtension {
|
||||
async onRequest({ response }: onRequestPayload) {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
response.writeHead(200, { 'Content-Type': 'text/plain' })
|
||||
response.end('I like cats.')
|
||||
|
||||
return reject()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const hocuspocus = await newHocuspocus(t, {
|
||||
extensions: [
|
||||
new CustomExtension(),
|
||||
],
|
||||
})
|
||||
|
||||
const response = await fetch(hocuspocus.server!.httpURL)
|
||||
t.is(await response.text(), 'I like cats.')
|
||||
resolve('done')
|
||||
})
|
||||
})
|
||||
|
||||
test('can intercept specific URLs', async t => {
|
||||
await new Promise(async resolve => {
|
||||
const hocuspocus = await newHocuspocus(t, {
|
||||
async onRequest({ response, request }: onRequestPayload) {
|
||||
if (request.url === '/foobar') {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
response.writeHead(200, { 'Content-Type': 'text/plain' })
|
||||
response.end('I like cats.')
|
||||
|
||||
return reject()
|
||||
})
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
const interceptedResponse = await fetch(`${hocuspocus.server!.httpURL}/foobar`)
|
||||
t.is(await interceptedResponse.text(), 'I like cats.')
|
||||
|
||||
const regularResponse = await fetch(hocuspocus.server!.httpURL)
|
||||
t.is(await regularResponse.text(), 'Welcome to Hocuspocus!')
|
||||
resolve('done')
|
||||
})
|
||||
})
|
||||
|
||||
test('has the instance', async t => {
|
||||
await new Promise(async resolve => {
|
||||
const hocuspocus = await newHocuspocus(t, {
|
||||
async onRequest({ instance }) {
|
||||
t.is(instance, hocuspocus)
|
||||
resolve('done')
|
||||
},
|
||||
})
|
||||
|
||||
fetch(`${hocuspocus.server!.httpURL}/foobar`).catch(() => {})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user