11// Proxy capture server tests cover request recording and response handling.
2- import { describe , expect , it } from "vitest" ;
3- import { parseConnectTarget } from "./proxy-server.js" ;
2+ import { mkdir , mkdtemp , rm , writeFile } from "node:fs/promises" ;
3+ import { request as httpRequest , createServer as createHttpServer } from "node:http" ;
4+ import type { AddressInfo } from "node:net" ;
5+ import { tmpdir } from "node:os" ;
6+ import { join } from "node:path" ;
7+ import { afterEach , describe , expect , it } from "vitest" ;
8+ import type { DebugProxySettings } from "./env.js" ;
9+ import { parseConnectTarget , startDebugProxyServer } from "./proxy-server.js" ;
10+ import { closeDebugProxyCaptureStore , getDebugProxyCaptureStore } from "./store.sqlite.js" ;
11+
12+ let testRoot : string | undefined ;
13+
14+ async function cleanupTestRoot ( ) : Promise < void > {
15+ closeDebugProxyCaptureStore ( ) ;
16+ if ( ! testRoot ) {
17+ return ;
18+ }
19+ const root = testRoot ;
20+ testRoot = undefined ;
21+ await rm ( root , { recursive : true , force : true } ) ;
22+ }
23+
24+ async function makeSettings ( ) : Promise < DebugProxySettings > {
25+ testRoot = await mkdtemp ( join ( tmpdir ( ) , "openclaw-debug-proxy-server-" ) ) ;
26+ const certDir = join ( testRoot , "certs" ) ;
27+ await mkdir ( certDir , { recursive : true } ) ;
28+ await writeFile ( join ( certDir , "root-ca.pem" ) , "test root cert\n" , "utf8" ) ;
29+ await writeFile ( join ( certDir , "root-ca-key.pem" ) , "test root key\n" , "utf8" ) ;
30+ return {
31+ enabled : true ,
32+ required : false ,
33+ dbPath : ":memory:" ,
34+ blobDir : join ( testRoot , "blobs" ) ,
35+ certDir,
36+ sessionId : "debug-proxy-server-test" ,
37+ sourceProcess : "test" ,
38+ } ;
39+ }
40+
41+ async function startLargeBodyOrigin ( ) : Promise < {
42+ receivedRequestBody : ( ) => string ;
43+ responseBody : string ;
44+ stop : ( ) => Promise < void > ;
45+ url : string ;
46+ } > {
47+ let receivedBody = "" ;
48+ const responseBody = "r" . repeat ( 12_000 ) ;
49+ const server = createHttpServer ( ( req , res ) => {
50+ req . setEncoding ( "utf8" ) ;
51+ req . on ( "data" , ( chunk ) => {
52+ receivedBody += chunk ;
53+ } ) ;
54+ req . on ( "end" , ( ) => {
55+ res . writeHead ( 200 , {
56+ "content-length" : Buffer . byteLength ( responseBody ) ,
57+ "content-type" : "text/plain; charset=utf-8" ,
58+ } ) ;
59+ res . end ( responseBody ) ;
60+ } ) ;
61+ } ) ;
62+ await new Promise < void > ( ( resolve , reject ) => {
63+ server . once ( "error" , reject ) ;
64+ server . listen ( 0 , "127.0.0.1" , ( ) => {
65+ server . off ( "error" , reject ) ;
66+ resolve ( ) ;
67+ } ) ;
68+ } ) ;
69+ const address = server . address ( ) as AddressInfo ;
70+ return {
71+ receivedRequestBody : ( ) => receivedBody ,
72+ responseBody,
73+ stop : async ( ) =>
74+ await new Promise < void > ( ( resolve , reject ) => {
75+ server . close ( ( error ) => {
76+ if ( error ) {
77+ reject ( error ) ;
78+ return ;
79+ }
80+ resolve ( ) ;
81+ } ) ;
82+ } ) ,
83+ url : `http://127.0.0.1:${ address . port } /capture` ,
84+ } ;
85+ }
86+
87+ async function postThroughProxy ( params : {
88+ body : string ;
89+ proxyUrl : string ;
90+ targetUrl : string ;
91+ } ) : Promise < string > {
92+ const proxy = new URL ( params . proxyUrl ) ;
93+ return await new Promise < string > ( ( resolve , reject ) => {
94+ const req = httpRequest (
95+ {
96+ host : proxy . hostname ,
97+ port : Number ( proxy . port ) ,
98+ method : "POST" ,
99+ path : params . targetUrl ,
100+ headers : {
101+ connection : "close" ,
102+ "content-length" : Buffer . byteLength ( params . body ) ,
103+ "content-type" : "text/plain; charset=utf-8" ,
104+ } ,
105+ } ,
106+ ( res ) => {
107+ const chunks : Buffer [ ] = [ ] ;
108+ res . on ( "data" , ( chunk ) => {
109+ chunks . push ( Buffer . isBuffer ( chunk ) ? chunk : Buffer . from ( chunk ) ) ;
110+ } ) ;
111+ res . on ( "end" , ( ) => {
112+ resolve ( Buffer . concat ( chunks ) . toString ( "utf8" ) ) ;
113+ } ) ;
114+ } ,
115+ ) ;
116+ req . on ( "error" , reject ) ;
117+ req . end ( params . body ) ;
118+ } ) ;
119+ }
120+
121+ afterEach ( async ( ) => {
122+ await cleanupTestRoot ( ) ;
123+ } ) ;
4124
5125describe ( "parseConnectTarget" , ( ) => {
6126 it ( "parses bracketed IPv6 CONNECT targets safely" , ( ) => {
@@ -23,3 +143,44 @@ describe("parseConnectTarget", () => {
23143 expect ( ( ) => parseConnectTarget ( "api.openai.com:0x50" ) ) . toThrow ( "Invalid CONNECT target port" ) ;
24144 } ) ;
25145} ) ;
146+
147+ describe ( "startDebugProxyServer" , ( ) => {
148+ it ( "caps captured body previews while forwarding full request and response bodies" , async ( ) => {
149+ const settings = await makeSettings ( ) ;
150+ const origin = await startLargeBodyOrigin ( ) ;
151+ const proxy = await startDebugProxyServer ( { settings } ) ;
152+ const requestBody = "q" . repeat ( 12_000 ) ;
153+
154+ try {
155+ const responseBody = await postThroughProxy ( {
156+ body : requestBody ,
157+ proxyUrl : proxy . proxyUrl ,
158+ targetUrl : origin . url ,
159+ } ) ;
160+
161+ expect ( origin . receivedRequestBody ( ) ) . toBe ( requestBody ) ;
162+ expect ( responseBody ) . toBe ( origin . responseBody ) ;
163+ const events = getDebugProxyCaptureStore ( settings . dbPath , settings . blobDir ) . getSessionEvents (
164+ settings . sessionId ,
165+ 10 ,
166+ ) ;
167+ const capturedRequest = events . find ( ( event ) => event . kind === "request" ) ;
168+ const capturedResponse = events . find ( ( event ) => event . kind === "response" ) ;
169+ expect ( capturedRequest ?. dataText ) . toBe ( "q" . repeat ( 8192 ) ) ;
170+ expect ( capturedResponse ?. dataText ) . toBe ( "r" . repeat ( 8192 ) ) ;
171+ expect ( JSON . parse ( String ( capturedRequest ?. metaJson ) ) ) . toMatchObject ( {
172+ bodyBytes : 12_000 ,
173+ capturePreviewBytes : 8192 ,
174+ captureTruncated : true ,
175+ } ) ;
176+ expect ( JSON . parse ( String ( capturedResponse ?. metaJson ) ) ) . toMatchObject ( {
177+ bodyBytes : 12_000 ,
178+ capturePreviewBytes : 8192 ,
179+ captureTruncated : true ,
180+ } ) ;
181+ } finally {
182+ await proxy . stop ( ) ;
183+ await origin . stop ( ) ;
184+ }
185+ } ) ;
186+ } ) ;
0 commit comments