@@ -101,29 +101,24 @@ export class MockHttpClient extends AbstractHttpClient {
101101 const method = requestProps . method ?? HttpRequestMethod . Get ;
102102
103103 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 ] ) {
104+ if ( requestProps . body ) {
105+ const bodyString = this . serializeBody ( requestProps . body ) ;
106+ if ( this . _expectedRequestBodies [ bodyString ] ) {
112107 // Existing body identifier
113- const expectedBody = this . _expectedRequestBodies [ reqBody ] ;
108+ const expectedBody = this . _expectedRequestBodies [ bodyString ] ;
114109 if ( updateBodyIds ) {
115110 expectedBody . refCount ++ ;
116111 }
117112 bodyId = expectedBody . bodyId ;
118113 } else if ( updateBodyIds ) {
119114 // New body identifier
120115 bodyId = `body${ this . _bodyIdCounter ++ } ` ;
121- this . _expectedRequestBodies [ reqBody ] = {
116+ this . _expectedRequestBodies [ bodyString ] = {
122117 refCount : 1 ,
123118 bodyId : bodyId ,
124119 } ;
125120 } else {
126- throw new Error ( `Unexpected request body: ${ reqBody } ` ) ;
121+ throw new Error ( `Unexpected request body: ${ bodyString } ` ) ;
127122 }
128123 }
129124
@@ -139,6 +134,19 @@ export class MockHttpClient extends AbstractHttpClient {
139134 ( key ) => key . split ( "::" ) [ 1 ]
140135 ) ;
141136 }
137+
138+ serializeBody (
139+ body : string | Blob | BufferSource | FormData | URLSearchParams
140+ ) : string {
141+ if ( typeof body === "string" ) {
142+ return body ;
143+ } else if ( body instanceof URLSearchParams ) {
144+ return body . toString ( ) ;
145+ }
146+ throw new Error (
147+ "Unsupported request body type: MockHttpClient supports bodies of type string or URLSearchParams"
148+ ) ;
149+ }
142150}
143151
144152/**
0 commit comments