@@ -20,14 +20,14 @@ public class MockResponseConfiguration
20
20
[ JsonIgnore ]
21
21
public bool NoMocks { get ; set ; } = false ;
22
22
[ JsonIgnore ]
23
- public string MocksFile { get ; set ; } = "responses .json" ;
23
+ public string MocksFile { get ; set ; } = "mocks .json" ;
24
24
[ JsonIgnore ]
25
25
public bool BlockUnmockedRequests { get ; set ; } = false ;
26
26
27
27
[ JsonPropertyName ( "$schema" ) ]
28
- public string Schema { get ; set ; } = "https://raw.githubusercontent.com/microsoft/dev-proxy/main/schemas/v1.0/responses .schema.json" ;
29
- [ JsonPropertyName ( "responses " ) ]
30
- public IEnumerable < MockResponse > Responses { get ; set ; } = Array . Empty < MockResponse > ( ) ;
28
+ public string Schema { get ; set ; } = "https://raw.githubusercontent.com/microsoft/dev-proxy/main/schemas/v1.0/mockresponseplugin .schema.json" ;
29
+ [ JsonPropertyName ( "mocks " ) ]
30
+ public IEnumerable < MockResponse > Mocks { get ; set ; } = Array . Empty < MockResponse > ( ) ;
31
31
}
32
32
33
33
public class MockResponsePlugin : BaseProxyPlugin
@@ -48,8 +48,10 @@ public MockResponsePlugin()
48
48
_noMocks . AddAlias ( "-n" ) ;
49
49
_noMocks . ArgumentHelpName = "no mocks" ;
50
50
51
- _mocksFile = new Option < string ? > ( "--mocks-file" , "Provide a file populated with mock responses" ) ;
52
- _mocksFile . ArgumentHelpName = "mocks file" ;
51
+ _mocksFile = new Option < string ? > ( "--mocks-file" , "Provide a file populated with mock responses" )
52
+ {
53
+ ArgumentHelpName = "mocks file"
54
+ } ;
53
55
}
54
56
55
57
public override void Register ( IPluginEvents pluginEvents ,
@@ -120,14 +122,20 @@ protected virtual Task OnRequest(object? sender, ProxyRequestArgs e)
120
122
{
121
123
ProcessMockResponse ( e . Session , new MockResponse
122
124
{
123
- Method = request . Method ,
124
- Url = request . Url ,
125
- ResponseCode = 502 ,
126
- ResponseBody = new GraphErrorResponseBody ( new GraphErrorResponseError
125
+ Request = new ( )
126
+ {
127
+ Url = request . Url ,
128
+ Method = request . Method
129
+ } ,
130
+ Response = new ( )
127
131
{
128
- Code = "Bad Gateway" ,
129
- Message = $ "No mock response found for { request . Method } { request . Url } "
130
- } )
132
+ StatusCode = 502 ,
133
+ Body = new GraphErrorResponseBody ( new GraphErrorResponseError
134
+ {
135
+ Code = "Bad Gateway" ,
136
+ Message = $ "No mock response found for { request . Method } { request . Url } "
137
+ } )
138
+ }
131
139
} ) ;
132
140
state . HasBeenSet = true ;
133
141
}
@@ -139,57 +147,59 @@ protected virtual Task OnRequest(object? sender, ProxyRequestArgs e)
139
147
private MockResponse ? GetMatchingMockResponse ( Request request )
140
148
{
141
149
if ( _configuration . NoMocks ||
142
- _configuration . Responses is null ||
143
- ! _configuration . Responses . Any ( ) )
150
+ _configuration . Mocks is null ||
151
+ ! _configuration . Mocks . Any ( ) )
144
152
{
145
153
return null ;
146
154
}
147
155
148
- var mockResponse = _configuration . Responses . FirstOrDefault ( mockResponse =>
156
+ var mockResponse = _configuration . Mocks . FirstOrDefault ( mockResponse =>
149
157
{
150
- if ( mockResponse . Method != request . Method ) return false ;
151
- if ( mockResponse . Url == request . Url && IsNthRequest ( mockResponse ) )
158
+ if ( mockResponse . Request is null ) return false ;
159
+
160
+ if ( mockResponse . Request . Method != request . Method ) return false ;
161
+ if ( mockResponse . Request . Url == request . Url && IsNthRequest ( mockResponse ) )
152
162
{
153
163
return true ;
154
164
}
155
165
156
166
// check if the URL contains a wildcard
157
167
// if it doesn't, it's not a match for the current request for sure
158
- if ( ! mockResponse . Url . Contains ( '*' ) )
168
+ if ( ! mockResponse . Request . Url . Contains ( '*' ) )
159
169
{
160
170
return false ;
161
171
}
162
172
163
173
//turn mock URL with wildcard into a regex and match against the request URL
164
- var mockResponseUrlRegex = Regex . Escape ( mockResponse . Url ) . Replace ( "\\ *" , ".*" ) ;
174
+ var mockResponseUrlRegex = Regex . Escape ( mockResponse . Request . Url ) . Replace ( "\\ *" , ".*" ) ;
165
175
return Regex . IsMatch ( request . Url , $ "^{ mockResponseUrlRegex } $") && IsNthRequest ( mockResponse ) ;
166
176
} ) ;
167
177
168
- if ( mockResponse is not null )
178
+ if ( mockResponse is not null && mockResponse . Request is not null )
169
179
{
170
- if ( ! _appliedMocks . ContainsKey ( mockResponse . Url ) )
180
+ if ( ! _appliedMocks . ContainsKey ( mockResponse . Request . Url ) )
171
181
{
172
- _appliedMocks . Add ( mockResponse . Url , 0 ) ;
182
+ _appliedMocks . Add ( mockResponse . Request . Url , 0 ) ;
173
183
}
174
- _appliedMocks [ mockResponse . Url ] ++ ;
184
+ _appliedMocks [ mockResponse . Request . Url ] ++ ;
175
185
}
176
186
177
187
return mockResponse ;
178
188
}
179
189
180
190
private bool IsNthRequest ( MockResponse mockResponse )
181
191
{
182
- if ( mockResponse . Nth is null )
192
+ if ( mockResponse . Request is null || mockResponse . Request . Nth is null )
183
193
{
184
194
// mock doesn't define an Nth property so it always qualifies
185
195
return true ;
186
196
}
187
197
188
- var nth = 0 ;
189
- _appliedMocks . TryGetValue ( mockResponse . Url , out nth ) ;
198
+ int nth ;
199
+ _appliedMocks . TryGetValue ( mockResponse . Request . Url , out nth ) ;
190
200
nth ++ ;
191
201
192
- return mockResponse . Nth == nth ;
202
+ return mockResponse . Request . Nth == nth ;
193
203
}
194
204
195
205
private void ProcessMockResponse ( SessionEventArgs e , MockResponse matchingResponse )
@@ -199,14 +209,14 @@ private void ProcessMockResponse(SessionEventArgs e, MockResponse matchingRespon
199
209
string requestDate = DateTime . Now . ToString ( ) ;
200
210
var headers = ProxyUtils . BuildGraphResponseHeaders ( e . HttpClient . Request , requestId , requestDate ) ;
201
211
HttpStatusCode statusCode = HttpStatusCode . OK ;
202
- if ( matchingResponse . ResponseCode is not null )
212
+ if ( matchingResponse . Response ? . StatusCode is not null )
203
213
{
204
- statusCode = ( HttpStatusCode ) matchingResponse . ResponseCode ;
214
+ statusCode = ( HttpStatusCode ) matchingResponse . Response . StatusCode ;
205
215
}
206
216
207
- if ( matchingResponse . ResponseHeaders is not null )
217
+ if ( matchingResponse . Response ? . Headers is not null )
208
218
{
209
- foreach ( var key in matchingResponse . ResponseHeaders . Keys )
219
+ foreach ( var key in matchingResponse . Response . Headers . Keys )
210
220
{
211
221
// remove duplicate headers
212
222
var existingHeader = headers . FirstOrDefault ( h => h . Name . Equals ( key , StringComparison . OrdinalIgnoreCase ) ) ;
@@ -215,7 +225,7 @@ private void ProcessMockResponse(SessionEventArgs e, MockResponse matchingRespon
215
225
headers . Remove ( existingHeader ) ;
216
226
}
217
227
218
- headers . Add ( new HttpHeader ( key , matchingResponse . ResponseHeaders [ key ] ) ) ;
228
+ headers . Add ( new HttpHeader ( key , matchingResponse . Response . Headers [ key ] ) ) ;
219
229
}
220
230
}
221
231
// default the content type to application/json unless set in the mock response
@@ -224,9 +234,9 @@ private void ProcessMockResponse(SessionEventArgs e, MockResponse matchingRespon
224
234
headers . Add ( new HttpHeader ( "content-type" , "application/json" ) ) ;
225
235
}
226
236
227
- if ( matchingResponse . ResponseBody is not null )
237
+ if ( matchingResponse . Response ? . Body is not null )
228
238
{
229
- var bodyString = JsonSerializer . Serialize ( matchingResponse . ResponseBody ) as string ;
239
+ var bodyString = JsonSerializer . Serialize ( matchingResponse . Response . Body ) as string ;
230
240
// we get a JSON string so need to start with the opening quote
231
241
if ( bodyString ? . StartsWith ( "\" @" ) ?? false )
232
242
{
@@ -245,7 +255,7 @@ private void ProcessMockResponse(SessionEventArgs e, MockResponse matchingRespon
245
255
{
246
256
var bodyBytes = File . ReadAllBytes ( filePath ) ;
247
257
e . GenericResponse ( bodyBytes , statusCode , headers ) ;
248
- _logger ? . LogRequest ( new [ ] { $ "{ matchingResponse . ResponseCode ?? 200 } { matchingResponse . Url } " } , MessageType . Mocked , new LoggingContext ( e ) ) ;
258
+ _logger ? . LogRequest ( [ $ "{ matchingResponse . Response . StatusCode ?? 200 } { matchingResponse . Request ? . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
249
259
return ;
250
260
}
251
261
}
@@ -256,6 +266,6 @@ private void ProcessMockResponse(SessionEventArgs e, MockResponse matchingRespon
256
266
}
257
267
e . GenericResponse ( body ?? string . Empty , statusCode , headers ) ;
258
268
259
- _logger ? . LogRequest ( new [ ] { $ "{ matchingResponse . ResponseCode ?? 200 } { matchingResponse . Url } " } , MessageType . Mocked , new LoggingContext ( e ) ) ;
269
+ _logger ? . LogRequest ( [ $ "{ matchingResponse . Response ? . StatusCode ?? 200 } { matchingResponse . Request ? . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
260
270
}
261
271
}
0 commit comments