mirror of
https://github.com/hanzoai/msw.git
synced 2026-09-24 17:00:34 +00:00
51 lines
1.1 KiB
TypeScript
51 lines
1.1 KiB
TypeScript
// @vitest-environment node
|
|
import https from 'https'
|
|
import { http, HttpResponse } from 'msw'
|
|
import { setupServer } from 'msw/node'
|
|
import { httpsAgent, HttpServer } from '@open-draft/test-server/http'
|
|
import { waitForClientRequest } from '../../../../support/utils'
|
|
|
|
const httpServer = new HttpServer((app) => {
|
|
app.get('/user', (req, res) => {
|
|
res.json({ works: false })
|
|
})
|
|
})
|
|
|
|
const server = setupServer()
|
|
|
|
beforeAll(async () => {
|
|
await httpServer.listen()
|
|
server.listen()
|
|
})
|
|
|
|
afterAll(async () => {
|
|
server.close()
|
|
await httpServer.close()
|
|
})
|
|
|
|
test('exposes request cookies', async () => {
|
|
const endpointUrl = httpServer.https.url('/user')
|
|
|
|
server.use(
|
|
http.get(endpointUrl, ({ cookies }) => {
|
|
return HttpResponse.json({ cookies })
|
|
}),
|
|
)
|
|
|
|
const url = new URL(endpointUrl)
|
|
|
|
const request = https.get({
|
|
protocol: url.protocol,
|
|
hostname: url.hostname,
|
|
path: url.pathname,
|
|
port: url.port,
|
|
headers: {
|
|
Cookie: 'auth-token=abc-123',
|
|
},
|
|
agent: httpsAgent,
|
|
})
|
|
const { responseText } = await waitForClientRequest(request)
|
|
|
|
expect(responseText).toBe('{"cookies":{"auth-token":"abc-123"}}')
|
|
})
|