Home / File/ reply-early-hints.test.js — fastify Source File

reply-early-hints.test.js — fastify Source File

Architecture documentation for reply-early-hints.test.js, a javascript file in the fastify codebase.

Entity Profile

Source Code

'use strict'

const Fastify = require('..')
const { test } = require('node:test')
const http = require('node:http')
const http2 = require('node:http2')

const testResBody = 'Hello, world!'

test('sends early hints', (t, done) => {
  t.plan(6)

  const fastify = Fastify({
    logger: false
  })

  fastify.get('/', async (request, reply) => {
    reply.writeEarlyHints({
      link: '</styles.css>; rel=preload; as=style'
    }, () => {
      t.assert.ok('callback called')
    })

    return testResBody
  })

  fastify.listen({ port: 0 }, (err, address) => {
    t.assert.ifError(err)

    const req = http.get(address)

    req.on('information', (res) => {
      t.assert.strictEqual(res.statusCode, 103)
      t.assert.strictEqual(res.headers.link, '</styles.css>; rel=preload; as=style')
    })

    req.on('response', (res) => {
      t.assert.strictEqual(res.statusCode, 200)

      let data = ''
      res.on('data', (chunk) => {
        data += chunk
      })

      res.on('end', () => {
        t.assert.strictEqual(data, testResBody)
        fastify.close()
        done()
      })
    })
  })
})

test('sends early hints (http2)', (t, done) => {
  t.plan(6)

  const fastify = Fastify({
    http2: true,
    logger: false
  })

  fastify.get('/', async (request, reply) => {
    reply.writeEarlyHints({
      link: '</styles.css>; rel=preload; as=style'
    })

    return testResBody
  })

  fastify.listen({ port: 0 }, (err, address) => {
    t.assert.ifError(err)

    const client = http2.connect(address)
    const req = client.request()

    req.on('headers', (headers) => {
      t.assert.notStrictEqual(headers, undefined)
      t.assert.strictEqual(headers[':status'], 103)
      t.assert.strictEqual(headers.link, '</styles.css>; rel=preload; as=style')
    })

    req.on('response', (headers) => {
      t.assert.strictEqual(headers[':status'], 200)
    })

    let data = ''
    req.on('data', (chunk) => {
      data += chunk
    })

    req.on('end', () => {
      t.assert.strictEqual(data, testResBody)
      client.close()
      fastify.close()
      done()
    })

    req.end()
  })
})

Frequently Asked Questions

What does reply-early-hints.test.js do?
reply-early-hints.test.js is a source file in the fastify codebase, written in javascript.
Where is reply-early-hints.test.js in the architecture?
reply-early-hints.test.js is located at test/reply-early-hints.test.js (directory: test).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free