@@ -4,12 +4,10 @@ import { describe, should } from 'micro-should';
44import { createServer } from 'node:http' ;
55import * as mftch from '../lib/esm/index.js' ;
66
7- const HTTP_PORT = 8001 ;
8- const URL = `http://127.0.0.1:${ HTTP_PORT } /` ;
97// NOTE: this will send real network requests to httpbin (to verify compat)
108const REAL_NETWORK = false ;
119
12- function httpServer ( cb ) {
10+ function httpServer ( port , cb ) {
1311 const server = createServer ( async ( req , res ) => {
1412 if ( req . method !== 'POST' || req . headers [ 'content-type' ] !== 'application/json' ) {
1513 res . writeHead ( 405 ) ;
@@ -20,23 +18,40 @@ function httpServer(cb) {
2018 const buf = [ ] ;
2119 for await ( const chunk of req ) buf . push ( chunk ) ;
2220 const body = Buffer . concat ( buf ) . toString ( 'utf8' ) ;
23- res . end ( JSON . stringify ( await cb ( JSON . parse ( body ) , req . headers ) ) ) ;
21+ const response = await cb ( JSON . parse ( body ) , req . headers ) ;
22+ res . end ( JSON . stringify ( response ) ) ;
2423 } ) ;
2524 server . on ( 'error' , ( err ) => console . log ( 'HTTP ERR' , err ) ) ;
2625 const stop = ( ) =>
2726 new Promise ( ( resolve , reject ) => {
2827 server . close ( async ( err ) => {
2928 await sleep ( 100 ) ; // this somehow broken, without it new server will throw ECONNRESET because old server not fully closed.
29+ // also, bun will silently use old server even after stopping, so we use different ports for different tests
3030 if ( err ) reject ( err ) ;
3131 else resolve ( ) ;
3232 } ) ;
3333 server . closeAllConnections ( ) ;
3434 } ) ;
35- return new Promise ( ( resolve ) => server . listen ( HTTP_PORT , ( t ) => resolve ( stop ) ) ) ;
35+ const url = `http://127.0.0.1:${ port } /` ;
36+ return new Promise ( ( resolve ) => server . listen ( port , ( t ) => resolve ( { stop, url } ) ) ) ;
3637}
3738
3839const sleep = ( ms ) => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
3940
41+ const cleanHeaders = ( headers ) => {
42+ // these changes between node, bun and deno
43+ const {
44+ 'accept-encoding' : _0 ,
45+ 'sec-fetch-mode' : _1 ,
46+ 'user-agent' : _2 ,
47+ connection : _3 ,
48+ host : _4 ,
49+ 'accept-language' : _5 ,
50+ ...rest
51+ } = headers ;
52+ return rest ;
53+ } ;
54+
4055describe ( 'Network' , ( ) => {
4156 describe ( 'Limit' , ( ) => {
4257 const { limit } = mftch . _TEST ;
@@ -195,7 +210,7 @@ describe('Network', () => {
195210
196211 should ( 'ftch' , async ( ) => {
197212 const serverLog = [ ] ;
198- const stop = await httpServer ( async ( r ) => {
213+ const { stop, url } = await httpServer ( 8001 , async ( r ) => {
199214 if ( r . sleep ) await sleep ( r . sleep ) ;
200215 serverLog . push ( r . res ) ;
201216 return { res : r . res } ;
@@ -214,7 +229,7 @@ describe('Network', () => {
214229 killswitch : ( ) => ENABLED ,
215230 } ) ;
216231 const t = async ( fn , body , opts = { } ) => {
217- const res = await fn ( URL , {
232+ const res = await fn ( url , {
218233 method : 'POST' ,
219234 headers : { 'Content-Type' : 'application/json' } ,
220235 body : JSON . stringify ( body ) ,
@@ -319,14 +334,14 @@ describe('Network', () => {
319334 } ) ;
320335 should ( 'jsonrpc' , async ( ) => {
321336 const serverLog = [ ] ;
322- const stop = await httpServer ( async ( r , headers ) => {
323- serverLog . push ( { r, headers } ) ;
337+ const { stop, url } = await httpServer ( 8002 , async ( r , headers ) => {
338+ serverLog . push ( { r, headers : cleanHeaders ( headers ) } ) ;
324339 if ( Array . isArray ( r ) )
325340 return r . map ( ( i ) => ( Array . isArray ( i . params ) ? i . params [ 0 ] : i . params . res ) ) ;
326341 return Array . isArray ( r . params ) ? r . params [ 0 ] : r . params . res ;
327342 } ) ;
328343 const f = mftch . ftch ( fetch ) ;
329- const rpc = mftch . jsonrpc ( f , URL , {
344+ const rpc = mftch . jsonrpc ( f , url , {
330345 headers : { Test : '1' } ,
331346 } ) ;
332347 // Basic
@@ -350,15 +365,9 @@ describe('Network', () => {
350365 params : [ { jsonrpc : '2.0' , id : 0 , result : 1 } , 1 , true , [ 1 , 2 , 3 ] ] ,
351366 } ,
352367 headers : {
353- host : '127.0.0.1:8001' ,
354- connection : 'keep-alive' ,
355368 'content-type' : 'application/json' ,
356369 test : '1' ,
357370 accept : '*/*' ,
358- 'accept-language' : '*' ,
359- 'sec-fetch-mode' : 'cors' ,
360- 'user-agent' : 'node' ,
361- 'accept-encoding' : 'gzip, deflate' ,
362371 'content-length' : '101' ,
363372 } ,
364373 } ,
@@ -370,15 +379,9 @@ describe('Network', () => {
370379 params : { res : { jsonrpc : '2.0' , id : 0 , result : 1 } , A : 1 } ,
371380 } ,
372381 headers : {
373- host : '127.0.0.1:8001' ,
374- connection : 'keep-alive' ,
375382 'content-type' : 'application/json' ,
376383 test : '1' ,
377384 accept : '*/*' ,
378- 'accept-language' : '*' ,
379- 'sec-fetch-mode' : 'cors' ,
380- 'user-agent' : 'node' ,
381- 'accept-encoding' : 'gzip, deflate' ,
382385 'content-length' : '98' ,
383386 } ,
384387 } ,
@@ -390,22 +393,16 @@ describe('Network', () => {
390393 params : [ { jsonrpc : '2.0' , id : 0 , error : { code : 0 , message : 'test' } } ] ,
391394 } ,
392395 headers : {
393- host : '127.0.0.1:8001' ,
394- connection : 'keep-alive' ,
395396 'content-type' : 'application/json' ,
396397 test : '1' ,
397398 accept : '*/*' ,
398- 'accept-language' : '*' ,
399- 'sec-fetch-mode' : 'cors' ,
400- 'user-agent' : 'node' ,
401- 'accept-encoding' : 'gzip, deflate' ,
402399 'content-length' : '111' ,
403400 } ,
404401 } ,
405402 ] ) ;
406403 serverLog . splice ( 0 , serverLog . length ) ;
407404 // Batch
408- const rpcBatch = mftch . jsonrpc ( f , URL , {
405+ const rpcBatch = mftch . jsonrpc ( f , url , {
409406 headers : { Test : '1' } ,
410407 batchSize : 2 ,
411408 } ) ;
@@ -484,13 +481,13 @@ describe('Network', () => {
484481 } ) ;
485482 should ( 'replayable' , async ( ) => {
486483 const serverLog = [ ] ;
487- const stop = await httpServer ( async ( r ) => {
484+ const { stop, url } = await httpServer ( 8003 , async ( r ) => {
488485 if ( r . sleep ) await sleep ( r . sleep ) ;
489486 serverLog . push ( r . res ) ;
490487 return { res : r . res } ;
491488 } ) ;
492489 const t = async ( fn , body , opts = { } ) => {
493- const res = await fn ( URL , {
490+ const res = await fn ( url , {
494491 method : 'POST' ,
495492 headers : { 'Content-Type' : 'application/json' } ,
496493 body : JSON . stringify ( body ) ,
@@ -506,7 +503,7 @@ describe('Network', () => {
506503 const logs = replayCapture . export ( ) ;
507504 deepStrictEqual (
508505 logs ,
509- '{"{\\"url\\":\\"http://127.0.0.1:8001 /\\",\\"opt\\":{\\"method\\":\\"POST\\",\\"headers\\":{\\"Content-Type\\":\\"application/json\\"},\\"body\\":\\"{\\\\\\"res\\\\\\":1}\\"}}":"{\\"res\\":1}","{\\"url\\":\\"http://127.0.0.1:8001 /\\",\\"opt\\":{\\"method\\":\\"POST\\",\\"headers\\":{\\"Content-Type\\":\\"application/json\\"},\\"body\\":\\"{\\\\\\"res\\\\\\":2}\\"}}":"{\\"res\\":2}"}'
506+ '{"{\\"url\\":\\"http://127.0.0.1:8003 /\\",\\"opt\\":{\\"method\\":\\"POST\\",\\"headers\\":{\\"Content-Type\\":\\"application/json\\"},\\"body\\":\\"{\\\\\\"res\\\\\\":1}\\"}}":"{\\"res\\":1}","{\\"url\\":\\"http://127.0.0.1:8003 /\\",\\"opt\\":{\\"method\\":\\"POST\\",\\"headers\\":{\\"Content-Type\\":\\"application/json\\"},\\"body\\":\\"{\\\\\\"res\\\\\\":2}\\"}}":"{\\"res\\":2}"}'
510507 ) ;
511508 const replayTest = mftch . replayable ( ftch , JSON . parse ( logs ) ) ;
512509 deepStrictEqual ( await t ( replayTest , { res : 1 } ) , { res : 1 } ) ;
0 commit comments