Skip to content

Commit

Permalink
test: ensure compatibility of third-party Response
Browse files Browse the repository at this point in the history
  • Loading branch information
climba03003 committed Jan 25, 2024
1 parent c8c4a66 commit ff47a19
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions test/web-api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const Fastify = require('../fastify')
const fs = require('node:fs')
const semver = require('semver')
const { Readable } = require('node:stream')
const { fetch: undiciFetch } = require('undici')

if (semver.lt(process.versions.node, '18.0.0')) {
t.skip('Response or ReadableStream not available, skipping test')
Expand Down Expand Up @@ -157,3 +158,51 @@ test('Error when Response.bodyUsed', async (t) => {
const body = response.json()
t.equal(body.code, 'FST_ERR_REP_RESPONSE_BODY_CONSUMED')
})

test('allow to pipe with fetch', async (t) => {
t.plan(2)

const fastify = Fastify()
t.teardown(fastify.close.bind(fastify))

fastify.get('/', function (request, reply) {
return fetch(`${fastify.listeningOrigin}/fetch`, {
method: 'GET'
})
})

fastify.get('/fetch', function (request, reply) {
reply.code(200).send({ ok: true })
})

await fastify.listen()

const response = await fastify.inject({ method: 'GET', path: '/' })

t.equal(response.statusCode, 200)
t.same(response.json(), { ok: true })
})

test('allow to pipe with undici.fetch', async (t) => {
t.plan(2)

const fastify = Fastify()
t.teardown(fastify.close.bind(fastify))

fastify.get('/', function (request, reply) {
return undiciFetch(`${fastify.listeningOrigin}/fetch`, {
method: 'GET'
})
})

fastify.get('/fetch', function (request, reply) {
reply.code(200).send({ ok: true })
})

await fastify.listen()

const response = await fastify.inject({ method: 'GET', path: '/' })

t.equal(response.statusCode, 200)
t.same(response.json(), { ok: true })
})

0 comments on commit ff47a19

Please sign in to comment.