Home / File/ secure.test.js — fastify Source File

secure.test.js — fastify Source File

Architecture documentation for secure.test.js, a javascript file in the fastify codebase.

Entity Profile

Source Code

'use strict'

const { test } = require('node:test')
const Fastify = require('../..')
const h2url = require('h2url')
const msg = { hello: 'world' }

const { buildCertificate } = require('../build-certificate')
test.before(buildCertificate)

test('secure', async (t) => {
  t.plan(4)

  let fastify
  try {
    fastify = Fastify({
      http2: true,
      https: {
        key: global.context.key,
        cert: global.context.cert
      }
    })
    t.assert.ok(true, 'Key/cert successfully loaded')
  } catch (e) {
    t.assert.fail('Key/cert loading failed')
  }

  fastify.get('/', function (req, reply) {
    reply.code(200).send(msg)
  })
  fastify.get('/proto', function (req, reply) {
    reply.code(200).send({ proto: req.protocol })
  })
  fastify.get('/hostname_port', function (req, reply) {
    reply.code(200).send({ hostname: req.hostname, port: req.port })
  })

  t.after(() => { fastify.close() })
  await fastify.listen({ port: 0 })

  await t.test('https get request', async (t) => {
    t.plan(3)

    const url = `https://localhost:${fastify.server.address().port}`
    const res = await h2url.concat({ url })

    t.assert.strictEqual(res.headers[':status'], 200)
    t.assert.strictEqual(res.headers['content-length'], '' + JSON.stringify(msg).length)
    t.assert.deepStrictEqual(JSON.parse(res.body), msg)
  })

  await t.test('https get request without trust proxy - protocol', async (t) => {
    t.plan(2)

    const url = `https://localhost:${fastify.server.address().port}/proto`
    t.assert.deepStrictEqual(JSON.parse((await h2url.concat({ url })).body), { proto: 'https' })
    t.assert.deepStrictEqual(JSON.parse((await h2url.concat({ url, headers: { 'X-Forwarded-Proto': 'lorem' } })).body), { proto: 'https' })
  })
  await t.test('https get request - test hostname and port', async (t) => {
    t.plan(2)

    const url = `https://localhost:${fastify.server.address().port}/hostname_port`
    const parsedbody = JSON.parse((await h2url.concat({ url })).body)
    t.assert.strictEqual(parsedbody.hostname, 'localhost')
    t.assert.strictEqual(parsedbody.port, fastify.server.address().port)
  })
})

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free