@@ -21,6 +21,49 @@ export function urlForHttpServer(httpServer: Server): string {
2121 return `http://${ hostname } :${ port } ` ;
2222}
2323
24+ function createHttpRequest (
25+ method : HttpMethod ,
26+ url : string ,
27+ headers : Headers ,
28+ bodyContent : string ,
29+ ) : HttpRequest {
30+ const createRequest = ( ) : HttpRequest => ( {
31+ method,
32+ url,
33+ headers,
34+ body : new ReadableStream ( {
35+ start ( controller ) {
36+ controller . enqueue ( new TextEncoder ( ) . encode ( bodyContent ) ) ;
37+ controller . close ( ) ;
38+ } ,
39+ } ) ,
40+ query : new URLSearchParams ( new URL ( url ) . search ) ,
41+ params : { } ,
42+ user : null ,
43+ arrayBuffer : async ( ) => {
44+ return Buffer . from ( bodyContent ) . buffer ;
45+ } ,
46+ text : async ( ) => {
47+ return bodyContent ;
48+ } ,
49+ json : async ( ) => {
50+ return JSON . parse ( bodyContent ) ;
51+ } ,
52+ blob : async ( ) => {
53+ throw new Error ( 'Not implemented' ) ;
54+ } ,
55+ bodyUsed : false ,
56+ formData : async ( ) => {
57+ throw new Error ( 'Not implemented' ) ;
58+ } ,
59+ clone : ( ) => {
60+ return createRequest ( ) ;
61+ } ,
62+ } ) ;
63+
64+ return createRequest ( ) ;
65+ }
66+
2467export const createMockServer = ( handler : HttpHandler ) => {
2568 return ( req : IncomingMessage , res : ServerResponse ) => {
2669 let body = '' ;
@@ -32,39 +75,12 @@ export const createMockServer = (handler: HttpHandler) => {
3275 headers . set ( key , value as string ) ;
3376 }
3477
35- const azReq : HttpRequest = {
36- method : ( req . method as HttpMethod ) || null ,
37- url : new URL ( req . url || '' , 'http://localhost' ) . toString ( ) ,
78+ const azReq = createHttpRequest (
79+ ( req . method as HttpMethod ) || null ,
80+ new URL ( req . url || '' , 'http://localhost' ) . toString ( ) ,
3881 headers ,
39- body : new ReadableStream ( {
40- start ( controller ) {
41- controller . enqueue ( new TextEncoder ( ) . encode ( body ) ) ;
42- controller . close ( ) ;
43- } ,
44- } ) ,
45- query : new URLSearchParams ( req . url ) ,
46- params : { } ,
47- user : null ,
48- arrayBuffer : async ( ) => {
49- return Buffer . from ( body ) . buffer ;
50- } ,
51- text : async ( ) => {
52- return body ;
53- } ,
54- json : async ( ) => {
55- return JSON . parse ( body ) ;
56- } ,
57- blob : async ( ) => {
58- throw new Error ( 'Not implemented' ) ;
59- } ,
60- bodyUsed : false ,
61- formData : async ( ) => {
62- throw new Error ( 'Not implemented' ) ;
63- } ,
64- clone : ( ) => {
65- throw new Error ( 'Not implemented' ) ;
66- } ,
67- } ;
82+ body ,
83+ ) ;
6884
6985 const context = new InvocationContext ( {
7086 invocationId : 'mock' ,
0 commit comments