Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111import assert from 'node:assert'import {type Server} from 'node:http'import {type AddressInfo} from 'node:net'import {after, before, describe, it} from 'node:test'
import express from 'express'
import {type AppContext} from './context.js'import {type SafelinkRule} from './db/schema.js'import i18n from './i18n.js'import redirect from './routes/redirect.js'
const rules: Record<string, SafelinkRule['action']> = { 'https://allowed.example.com/': 'whitelist', 'https://blocked.example.com/': 'block', 'https://warned.example.com/': 'warn',}
const recorded: {event: string; whitelisted?: boolean}[] = []
const ctx = { db: { db: { insertInto: () => ({ values: (event: {event: string; whitelisted?: boolean}) => ({ execute: () => { recorded.push(event) return Promise.resolve() }, }), }), }, }, cfg: { service: { appHostname: 'bsky.app', hostnames: ['go.bsky.app'], safelinkEnabled: true, }, }, metrics: {track: () => {}}, safelinkClient: { tryFindRule: (link: string) => { const action = rules[link] return Promise.resolve(action ? {action} : 'ok') }, },} as unknown as AppContext
void describe('redirect Cache-Control', () => { let server: Server let baseUrl: string
before(async () => { const app = express() app.use(i18n.init) redirect(ctx, app) server = app.listen(0) await new Promise(resolve => server.once('listening', resolve)) baseUrl = `http://127.0.0.1:${(server.address() as AddressInfo).port}` })
after(() => { server.close() })
const cacheControlFor = async (link: string) => { const res = await fetch( `${baseUrl}/redirect?u=${encodeURIComponent(link)}`, {redirect: 'manual'}, ) await res.text() assert.ok(res.status < 500, `unexpected status ${res.status}`) return res.headers.get('cache-control') }
void it('does not cache links without a rule', async () => { assert.strictEqual( await cacheControlFor('https://unknown.example.com/'), 'no-store', ) })
void it('does not cache whitelisted links, so every click is recorded', async () => { recorded.length = 0 assert.strictEqual( await cacheControlFor('https://allowed.example.com/'), 'no-store', ) assert.deepStrictEqual( recorded.map(e => [e.event, e.whitelisted]), [['redirect', true]], ) })
void it('does not cache blocked or warned links', async () => { assert.strictEqual( await cacheControlFor('https://blocked.example.com/'), 'no-store', ) assert.strictEqual( await cacheControlFor('https://warned.example.com/'), 'no-store', ) })
void it('does not cache invalid redirects', async () => { assert.strictEqual(await cacheControlFor('not a url'), 'no-store') })})