Skip to content

Commit a3e4d6a

Browse files
committed
expanding mock response behavior to differentiate on http method
1 parent 8ed56e9 commit a3e4d6a

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

README.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ Following is a sample `responses.json` file:
7777
"responses": [
7878
{
7979
"url": "/v1.0/me",
80+
"method": "GET",
8081
"responseBody": {
8182
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity",
8283
"businessPhones": [
@@ -99,6 +100,7 @@ Following is a sample `responses.json` file:
99100
},
100101
{
101102
"url": "/v1.0/me/photo",
103+
"method": "GET",
102104
"responseCode": 404
103105
}
104106
]
@@ -110,13 +112,14 @@ The file defines an `responses` property with an array of responses. Each respon
110112
Property|Description|Required|Default value|Sample value
111113
--|--|:--:|--|--
112114
`url`|Server-relative URL to a Microsoft Graph API to respond to|yes||`/v1.0/me`
115+
`method`|Http verb used to match request in conjuction with `url`|yes||`GET`
113116
`responseBody`|Body to send as the response to the request|no|_empty_|See above
114117
`responseCode`|Response status code|no|`200`|`404`
115118
`responseHeaders`|Collection of headers to include in the response|no|_empty_|See above
116119

117120
#### Mock responses order
118121

119-
Mocks are matched in the order in which they are defined in the `responses.json` file, first matching response taking precedence over others. If you'd define multiple responses with the same URL, the first response would be used.
122+
Mocks are matched in the order in which they are defined in the `responses.json` file, first matching response taking precedence over others. If you'd define multiple responses with the same URL and method, the first matching response would be used.
120123

121124
For a configuration file like:
122125

@@ -125,17 +128,19 @@ For a configuration file like:
125128
"responses": [
126129
{
127130
"url": "/v1.0/me/photo",
131+
"method": "GET",
128132
"responseCode": 500
129133
},
130134
{
131135
"url": "/v1.0/me/photo",
136+
"method": "GET",
132137
"responseCode": 404
133138
}
134139
]
135140
}
136141
```
137142

138-
all requests to `/v1.0/me/photo` would respond with `500 Internal Server Error`.
143+
all `GET` requests to `/v1.0/me/photo` would respond with `500 Internal Server Error`.
139144

140145
> **Important**
141146
>
@@ -150,6 +155,7 @@ When defining mock responses, you can define a specific URL to mock, but also a
150155
"responses": [
151156
{
152157
"url": "/v1.0/users/*",
158+
"method": "GET",
153159
"responseBody": {
154160
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity",
155161
"businessPhones": [
@@ -171,7 +177,7 @@ When defining mock responses, you can define a specific URL to mock, but also a
171177
}
172178
```
173179

174-
would respond to `/v1.0/users/[email protected]` and `/v1.0/users/[email protected]` with the same mock response.
180+
would respond to`GET` requests for `/v1.0/users/[email protected]` and `/v1.0/users/[email protected]` with the same mock response.
175181

176182
If a URL of a mock response contains an `*`, it's used as a regular expression, where each `*` is converted into a `.*`, basically matching any sequence of characters. This is important to keep in mind, because if a pattern is too broad and defined before more specific mocks, it could unintetionally return unexpected responses, for example:
177183

@@ -180,6 +186,7 @@ If a URL of a mock response contains an `*`, it's used as a regular expression,
180186
"responses": [
181187
{
182188
"url": "/v1.0/users/*",
189+
"method": "GET",
183190
"responseBody": {
184191
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity",
185192
"businessPhones": [
@@ -199,6 +206,7 @@ If a URL of a mock response contains an `*`, it's used as a regular expression,
199206
},
200207
{
201208
"url": "/v1.0/users/48d31887-5fad-4d73-a9f5-3c356e68a038",
209+
"method": "GET",
202210
"responseBody": {
203211
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity",
204212
"businessPhones": [
@@ -220,13 +228,14 @@ If a URL of a mock response contains an `*`, it's used as a regular expression,
220228
}
221229
```
222230

223-
for request `/v1.0/users/48d31887-5fad-4d73-a9f5-3c356e68a038`, the proxy would return `Adele Vance` instead of `Megan Bowen`, because the asterisk at the end matches any series of characters. The correct way to define these responses, would be to change their order in the array:
231+
for request `GET /v1.0/users/48d31887-5fad-4d73-a9f5-3c356e68a038`, the proxy would return `Adele Vance` instead of `Megan Bowen`, because the asterisk at the end matches any series of characters. The correct way to define these responses, would be to change their order in the array:
224232

225233
```json
226234
{
227235
"responses": [
228236
{
229237
"url": "/v1.0/users/48d31887-5fad-4d73-a9f5-3c356e68a038",
238+
"method": "GET",
230239
"responseBody": {
231240
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity",
232241
"businessPhones": [
@@ -246,6 +255,7 @@ for request `/v1.0/users/48d31887-5fad-4d73-a9f5-3c356e68a038`, the proxy would
246255
},
247256
{
248257
"url": "/v1.0/users/*",
258+
"method": "GET",
249259
"responseBody": {
250260
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity",
251261
"businessPhones": [
@@ -280,6 +290,7 @@ For some requests you might want to respond with binary data like documents or i
280290
"responses": [
281291
{
282292
"url": "/v1.0/users/*/photo/$value",
293+
"method": "GET",
283294
"responseBody": "@picture.jpg",
284295
"responseHeaders": {
285296
"content-type": "image/jpeg"
@@ -289,7 +300,7 @@ For some requests you might want to respond with binary data like documents or i
289300
}
290301
```
291302

292-
When you call `/v1.0/users/[email protected]/photo/$value`, you'll get the image stored in the `picture.jpg` file in the current directory.
303+
When you call `GET /v1.0/users/[email protected]/photo/$value`, you'll get the image stored in the `picture.jpg` file in the current directory.
293304

294305
### Settings
295306

msgraph-chaos-proxy/ChaosEngine.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ _config.Responses is null ||
270270
}
271271

272272
var mockResponse = _config.Responses.FirstOrDefault(r => {
273+
if (r.Method != request.Method) return false;
273274
if (r.Url == request.RequestUri.AbsolutePath) {
274275
return true;
275276
}

msgraph-chaos-proxy/ChaosProxyConfiguration.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ public void Dispose()
9494
public class ChaosProxyMockResponse {
9595
[JsonPropertyName("url")]
9696
public string Url { get; set; } = string.Empty;
97+
[JsonPropertyName("method")]
98+
public string Method { get; set; } = string.Empty;
9799
[JsonPropertyName("responseCode")]
98100
public int? ResponseCode { get; set; } = 200;
99101
[JsonPropertyName("responseBody")]

msgraph-chaos-proxy/responses.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"responses": [
33
{
44
"url": "/v1.0/me",
5+
"method": "GET",
56
"responseBody": {
67
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity",
78
"businessPhones": [
@@ -19,8 +20,15 @@
1920
"id": "48d31887-5fad-4d73-a9f5-3c356e68a038"
2021
}
2122
},
23+
{
24+
"url": "/v1.0/me",
25+
"method": "PATCH",
26+
"responseBody": {},
27+
"responseCode": 204
28+
},
2229
{
2330
"url": "/v1.0/users/48d31887-5fad-4d73-a9f5-3c356e68a038",
31+
"method": "GET",
2432
"responseBody": {
2533
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity",
2634
"businessPhones": [
@@ -40,17 +48,20 @@
4048
},
4149
{
4250
"url": "/v1.0/me/photo",
51+
"method": "GET",
4352
"responseCode": 404
4453
},
4554
{
4655
"url": "/v1.0/users/*/photo/$value",
56+
"method": "GET",
4757
"responseBody": "@picture.jpg",
4858
"responseHeaders": {
4959
"content-type": "image/jpeg"
5060
}
5161
},
5262
{
5363
"url": "/v1.0/users/*",
64+
"method": "GET",
5465
"responseBody": {
5566
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity",
5667
"businessPhones": [

0 commit comments

Comments
 (0)