Skip to content

Commit

Permalink
test: test names, polish the setup
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Jul 21, 2024
1 parent a079fe9 commit ad6cf89
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions test/modules/fetch/compliance/response-content-encoding.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { vi, it, expect, beforeAll, afterEach, afterAll } from 'vitest'
import zlib from "zlib";
import zlib from 'zlib'
import { HttpServer } from '@open-draft/test-server/http'
import { HttpRequestEventMap } from '../../../../src'
import { FetchInterceptor } from '../../../../src/interceptors/fetch'

function compress(value: string): Buffer {
return zlib.gzipSync(zlib.brotliCompressSync(value))
}

const httpServer = new HttpServer((app) => {
app.get('/compressed', (_req, res) => {
res
.set('Content-Encoding', 'gzip, br')
.send(zlib.gzipSync(zlib.brotliCompressSync('Lorem ipsum dolor sit amet')))
.end()
res.set('Content-Encoding', 'gzip, br').end(compress('hello world'))
})
})

Expand All @@ -24,36 +25,36 @@ beforeAll(async () => {
})

afterEach(() => {
vi.resetAllMocks()
interceptor.removeAllListeners()
})

afterAll(async () => {
interceptor.dispose()
await httpServer.close()
})

it('support content-encoding: gzip, br for mocked request', async () => {
const message = 'Lorem ipsum dolor sit amet'
const compressed = zlib.brotliCompressSync(zlib.gzipSync(message))

interceptor.once('request', ({ controller }) => {
controller.respondWith(new Response(compressed, {
headers: {
'Content-Encoding': 'gzip, br',
}
}))
it('decompresses a mocked "content-encoding: gzip, br" response body', async () => {
interceptor.on('request', ({ controller }) => {
controller.respondWith(
new Response(compress('hello world'), {
headers: {
'Content-Encoding': 'gzip, br',
},
})
)
})

const response = await fetch('http://localhost')
const response = await fetch('http://localhost/resource')

expect(response.status).toBe(200)
expect(await response.text()).toBe(message)
// Must read as decompressed response.
expect(await response.text()).toBe('hello world')
})

it('support content-encoding: gzip, br for unmocked request', async () => {
const message = 'Lorem ipsum dolor sit amet'
it('decompresses a bypassed "content-encoding: gzip, br" response body', async () => {
const response = await fetch(httpServer.http.url('/compressed'))

expect(response.status).toBe(200)
expect(await response.text()).toBe(message)
})
// Must read as decompressed response.
expect(await response.text()).toBe('hello world')
})

0 comments on commit ad6cf89

Please sign in to comment.