// probeWeb against a real local server shaped like the web UIs on stortron.
import test from 'brittle'
import http from 'bare-http1'
import { probeWeb } from '../lib/probe.js'
function serve (routes) {
const server = http.createServer((req, res) => {
const r = routes[req.url]
if (!r) {
res.writeHead(404, { 'content-type': 'text/html' })
return res.end('<title>404 Not Found</title>')
}
// `to` builds a Location that needs the port the server got.
const headers = r.to ? { location: r.to(server.address().port) } : r.headers
res.writeHead(r.code, headers || { 'content-type': 'text/html' })
res.end(r.body || '')
})
return new Promise((resolve) => server.listen(0, '127.0.0.1', () => resolve(server)))
}
test('302 to /login: title comes from the landing page, code stays 302', async (t) => {
const s = await serve({
'/': { code: 302, headers: { location: '/login?returnUrl=%2F' } },
'/login?returnUrl=%2F': { code: 200, body: '<title>Login - Radarr</title>' }
})
const port = s.address().port
const r = await probeWeb('127.0.0.1', port)
t.is(r.open, true)
t.is(r.http.code, 302)
t.is(r.http.title, 'Login - Radarr')
t.is(r.http.landing, '/login?returnUrl=%2F')
s.close()
})
test('a redirect to another port is not followed', async (t) => {
const s = await serve({
'/': { code: 303, headers: { location: 'http://127.0.0.1:1/elsewhere' } }
})
const r = await probeWeb('127.0.0.1', s.address().port)
t.is(r.http.code, 303)
t.is(r.http.title, null, 'did not leave the port')
t.is(r.http.landing, '/')
s.close()
})
test('absolute same-port Location is followed', async (t) => {
const s = await serve({
'/': { code: 303, to: (port) => 'http://127.0.0.1:' + port + '/git/repos' },
'/git/repos': { code: 200, body: '<title>repos · pear-git-v3</title>' }
})
const r = await probeWeb('127.0.0.1', s.address().port)
t.is(r.http.title, 'repos · pear-git-v3')
t.is(r.http.landing, '/git/repos')
s.close()
})
test('redirect loop stops after maxRedirects', async (t) => {
const s = await serve({
'/': { code: 302, headers: { location: '/a' } },
'/a': { code: 302, headers: { location: '/' } }
})
const r = await probeWeb('127.0.0.1', s.address().port)
t.is(r.http.code, 302)
t.is(r.http.title, null)
s.close()
})
test('error page title is dropped', async (t) => {
const s = await serve({ '/': { code: 404, body: '<title>404 Not Found</title>' } })
const r = await probeWeb('127.0.0.1', s.address().port)
t.is(r.http.code, 404)
t.is(r.http.title, null)
s.close()
})
test('UTF-8 titles decode as UTF-8', async (t) => {
const s = await serve({ '/': { code: 200, body: '<title>repos · pear-git-v3 — café</title>' } })
const r = await probeWeb('127.0.0.1', s.address().port)
t.is(r.http.title, 'repos · pear-git-v3 — café')
s.close()
})