Files
safetynet/tests/broker-http-response-gate.test.mjs
T

16 lines
1.5 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {createRequire} from 'node:module';
const {validateResponse}=createRequire(import.meta.url)('../apps/network-broker/http-response-gate.cjs');
test('private response preserves status/body and separate cookies; strips hop by hop',()=>{
const body=Buffer.from('accepted'),cookies=['a=1; Secure','b=2; HttpOnly'];
const result=validateResponse({statusCode:201,headers:{'Content-Type':'text/plain','Set-Cookie':cookies,Connection:'keep-alive','Transfer-Encoding':'chunked','Content-Length':'999','Proxy-Authenticate':'private'},body});
body.fill(0);cookies[0]='changed';
assert.equal(result.body.toString(),'accepted');assert.equal(result.headers['content-length'],'8');assert.deepEqual(result.headers['set-cookie'],['a=1; Secure','b=2; HttpOnly']);assert.equal(result.headers.connection,'close');assert.equal(result.headers['transfer-encoding'],undefined);assert.equal(result.headers['proxy-authenticate'],undefined);
});
test('invalid status, injected headers, overflow and unsupported encoding fail closed',()=>{
const valid={statusCode:200,headers:{},body:Buffer.from('public')};
for(const change of [{statusCode:101},{statusCode:999},{statusCode:204},{headers:{'set-cookie':['a=1\r\nInjected: yes']}},{headers:{'Content-Type':'text/plain','content-type':'other'}},{headers:{'content-encoding':'gzip'}},{headers:{location:['one','two']}}])assert.throws(()=>validateResponse({...valid,...change}));
assert.throws(()=>validateResponse(valid,3));
});