@@ -85,7 +85,11 @@ export class MockHttpClient extends AbstractHttpClient {
8585 response : MockHttpResponse ,
8686 requestProps : HttpRequestInit = { }
8787 ) : MockHttpClient {
88- const key = this . _getKeyFromRequest ( response . url , requestProps , true ) ;
88+ const key = this . _getKeyFromRequest (
89+ requestProps . url ?? response . url ,
90+ requestProps ,
91+ true
92+ ) ;
8993 const expected = this . _expectedResponses [ key ] ?? [ ] ;
9094 expected . push ( response ) ;
9195 this . _expectedResponses [ key ] = expected ;
@@ -101,29 +105,24 @@ export class MockHttpClient extends AbstractHttpClient {
101105 const method = requestProps . method ?? HttpRequestMethod . Get ;
102106
103107 let bodyId : string = "" ;
104- const reqBody = requestProps . body ;
105- if ( reqBody ) {
106- if ( typeof reqBody !== "string" ) {
107- throw new Error (
108- "MockHttpClient only supports string request bodies"
109- ) ;
110- }
111- if ( this . _expectedRequestBodies [ reqBody ] ) {
108+ if ( requestProps . body ) {
109+ const bodyString = this . serializeBody ( requestProps . body ) ;
110+ if ( this . _expectedRequestBodies [ bodyString ] ) {
112111 // Existing body identifier
113- const expectedBody = this . _expectedRequestBodies [ reqBody ] ;
112+ const expectedBody = this . _expectedRequestBodies [ bodyString ] ;
114113 if ( updateBodyIds ) {
115114 expectedBody . refCount ++ ;
116115 }
117116 bodyId = expectedBody . bodyId ;
118117 } else if ( updateBodyIds ) {
119118 // New body identifier
120119 bodyId = `body${ this . _bodyIdCounter ++ } ` ;
121- this . _expectedRequestBodies [ reqBody ] = {
120+ this . _expectedRequestBodies [ bodyString ] = {
122121 refCount : 1 ,
123122 bodyId : bodyId ,
124123 } ;
125124 } else {
126- throw new Error ( `Unexpected request body: ${ reqBody } ` ) ;
125+ throw new Error ( `Unexpected request body: ${ bodyString } ` ) ;
127126 }
128127 }
129128
@@ -139,6 +138,19 @@ export class MockHttpClient extends AbstractHttpClient {
139138 ( key ) => key . split ( "::" ) [ 1 ]
140139 ) ;
141140 }
141+
142+ serializeBody (
143+ body : string | Blob | BufferSource | FormData | URLSearchParams
144+ ) : string {
145+ if ( typeof body === "string" ) {
146+ return body ;
147+ } else if ( body instanceof URLSearchParams ) {
148+ return body . toString ( ) ;
149+ }
150+ throw new Error (
151+ "Unsupported request body type: MockHttpClient supports bodies of type string or URLSearchParams"
152+ ) ;
153+ }
142154}
143155
144156/**
0 commit comments