@@ -27,7 +27,7 @@ public class MockResponseConfiguration
27
27
public bool BlockUnmockedRequests { get ; set ; } = false ;
28
28
29
29
[ JsonPropertyName ( "$schema" ) ]
30
- public string Schema { get ; set ; } = "https://raw.githubusercontent.com/microsoft/dev-proxy/main/schemas/v0.16.1 /mockresponseplugin.schema.json" ;
30
+ public string Schema { get ; set ; } = "https://raw.githubusercontent.com/microsoft/dev-proxy/main/schemas/v0.17.0 /mockresponseplugin.schema.json" ;
31
31
public IEnumerable < MockResponse > Mocks { get ; set ; } = Array . Empty < MockResponse > ( ) ;
32
32
}
33
33
@@ -159,7 +159,9 @@ _configuration.Mocks is null ||
159
159
if ( mockResponse . Request is null ) return false ;
160
160
161
161
if ( mockResponse . Request . Method != request . Method ) return false ;
162
- if ( mockResponse . Request . Url == request . Url && IsNthRequest ( mockResponse ) )
162
+ if ( mockResponse . Request . Url == request . Url &&
163
+ HasMatchingBody ( mockResponse , request ) &&
164
+ IsNthRequest ( mockResponse ) )
163
165
{
164
166
return true ;
165
167
}
@@ -173,7 +175,9 @@ _configuration.Mocks is null ||
173
175
174
176
//turn mock URL with wildcard into a regex and match against the request URL
175
177
var mockResponseUrlRegex = Regex . Escape ( mockResponse . Request . Url ) . Replace ( "\\ *" , ".*" ) ;
176
- return Regex . IsMatch ( request . Url , $ "^{ mockResponseUrlRegex } $") && IsNthRequest ( mockResponse ) ;
178
+ return Regex . IsMatch ( request . Url , $ "^{ mockResponseUrlRegex } $") &&
179
+ HasMatchingBody ( mockResponse , request ) &&
180
+ IsNthRequest ( mockResponse ) ;
177
181
} ) ;
178
182
179
183
if ( mockResponse is not null && mockResponse . Request is not null )
@@ -188,6 +192,30 @@ _configuration.Mocks is null ||
188
192
return mockResponse ;
189
193
}
190
194
195
+ private bool HasMatchingBody ( MockResponse mockResponse , Request request )
196
+ {
197
+ if ( request . Method == "GET" )
198
+ {
199
+ // GET requests don't have a body so we can't match on it
200
+ return true ;
201
+ }
202
+
203
+ if ( mockResponse . Request ? . BodyFragment is null )
204
+ {
205
+ // no body fragment to match on
206
+ return true ;
207
+ }
208
+
209
+ if ( ! request . HasBody || string . IsNullOrEmpty ( request . BodyString ) )
210
+ {
211
+ // mock defines a body fragment but the request has no body
212
+ // so it can't match
213
+ return false ;
214
+ }
215
+
216
+ return request . BodyString . Contains ( mockResponse . Request . BodyFragment , StringComparison . OrdinalIgnoreCase ) ;
217
+ }
218
+
191
219
private bool IsNthRequest ( MockResponse mockResponse )
192
220
{
193
221
if ( mockResponse . Request is null || mockResponse . Request . Nth is null )
0 commit comments