MC/website/tests/forgejo-proxy.test.mjs
2026-11-27 23:43:05 +00:00

7 lines
1.8 KiB
JavaScript

import test from 'node:test';import assert from 'node:assert/strict';import {createServer,request} from 'node:http';import {createForgejoProxy} from '../node/forgejo-proxy.mjs';
const listen=s=>new Promise(r=>s.listen(0,'127.0.0.1',r));const close=s=>new Promise(r=>s.close(r));
test('Forgejo subpath streams git bodies, preserves auth, strips website identity and cookies',async()=>{
const upstream=createServer((req,res)=>{let bytes=0;req.on('data',c=>bytes+=c.length);req.on('end',()=>{res.setHeader('Set-Cookie',['forgejo_session=test; Path=/git/; Secure']);res.end(JSON.stringify({path:req.url,auth:req.headers.authorization,cookie:req.headers.cookie,forged:req.headers['x-webauth-user'],bytes}));});});await listen(upstream);
const proxy=createForgejoProxy({FORGEJO_URL:'http://127.0.0.1:'+upstream.address().port,PUBLIC_ORIGIN:'https://dunerealms.com'});const server=createServer((req,res)=>{if(!proxy(req,res)){res.statusCode=404;res.end();}});await listen(server);
try{const base='http://127.0.0.1:'+server.address().port;const redirected=await fetch(base+'/git',{redirect:'manual'});assert.equal(redirected.status,308);assert.equal(redirected.headers.get('location'),'/git/');const r=await fetch(base+'/git/unimatrix/MC.git/git-receive-pack',{method:'POST',headers:{Authorization:'Basic test',Cookie:'__Host-dune_session=private; __Host-dune_git_session=private_git; forgejo_session=abc','X-Webauth-User':'admin'},body:Buffer.alloc(90000)});const data=await r.json();assert.equal(data.path,'/unimatrix/MC.git/git-receive-pack');assert.equal(data.auth,'Basic test');assert.ok(!data.cookie.includes('private'));assert.equal(data.forged,undefined);assert.equal(data.bytes,90000);assert.match(r.headers.get('set-cookie'),/Path=\/git\//);}finally{await close(server);await close(upstream);}
});