mirror of
https://github.com/hanzoai/msw.git
synced 2026-09-24 17:00:34 +00:00
Co-authored-by: Frederik Rabøl <[email protected]> Co-authored-by: Christoph Fricke <[email protected]> Co-authored-by: Piotr <[email protected]> Co-authored-by: Kristján Oddsson <[email protected]> Co-authored-by: thepassle <[email protected]> Co-authored-by: Kevin Østerkilde <[email protected]> Co-authored-by: Laryssa Rocha <[email protected]> Co-authored-by: Matthew Costabile <[email protected]>
47 lines
982 B
TypeScript
47 lines
982 B
TypeScript
/**
|
|
* @jest-environment node
|
|
*/
|
|
import https from 'https'
|
|
import { HttpResponse, http } from 'msw'
|
|
import { setupServer } from 'msw/node'
|
|
|
|
const server = setupServer()
|
|
|
|
beforeAll(() => {
|
|
server.listen()
|
|
})
|
|
|
|
afterEach(() => {
|
|
server.resetHandlers()
|
|
})
|
|
|
|
afterAll(() => {
|
|
server.close()
|
|
})
|
|
|
|
test('intercepts and mocks a request made via "https"', (done) => {
|
|
server.use(
|
|
http.get('https://api.example.com/resource', () => {
|
|
return HttpResponse.text('Hello, world!')
|
|
}),
|
|
)
|
|
const request = https.get('https://api.example.com/resource')
|
|
|
|
request.on('response', (response) => {
|
|
const chunks: Array<Buffer> = []
|
|
response.on('data', (chunk) => chunks.push(Buffer.from(chunk)))
|
|
|
|
response.on('error', done)
|
|
response.once('end', () => {
|
|
expect(chunks).toHaveLength(1)
|
|
|
|
const responseText = Buffer.concat(chunks).toString('utf8')
|
|
expect(responseText).toBe('Hello, world!')
|
|
|
|
done()
|
|
})
|
|
})
|
|
|
|
request.on('error', done)
|
|
})
|