Skip to content

Commit e020e5b

Browse files
mosoftwareenterprisesMark Oliverwaldekmastykarz
authored
Fixes #358 MockGeneratorPlugin errors if the response headers have a duplicate (#372)
* Fixes #358 MockGeneratorPlugin errors if the response has a duplicate header Resolved by only including distinct headers in the response list. * Fixes #358 MockGeneratorPlugin errors if the response has a duplicate header Headers are now List of Dictionaries * Revert some styling changes * Fixes #358 MockGeneratorPlugin errors if the response headers have a duplicate. Remove code that is not specific to this fix. * Fixes #358 MockGeneratorPlugin errors if the response headers have a duplicate. Changes following code review. * Fixes #358 MockGeneratorPlugin errors if the response headers have a duplicate. Changes following code review and further testing --------- Co-authored-by: Mark Oliver <[email protected]> Co-authored-by: Waldek Mastykarz <[email protected]>
1 parent cc78894 commit e020e5b

File tree

7 files changed

+17
-24
lines changed

7 files changed

+17
-24
lines changed

dev-proxy-abstractions/GraphBatchResponsePayload.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class GraphBatchResponsePayloadResponse
2020
[JsonPropertyName("body")]
2121
public dynamic? Body { get; set; }
2222
[JsonPropertyName("headers")]
23-
public Dictionary<string, string>? Headers { get; set; }
23+
public List<KeyValuePair<string, string>>? Headers { get; set; }
2424
}
2525

2626
public class GraphBatchResponsePayloadResponseBody

dev-proxy-plugins/Behavior/RateLimitingPlugin.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ _urlsToWatch is null ||
203203
if (_configuration.CustomResponse is not null)
204204
{
205205
var headers = _configuration.CustomResponse.Headers is not null ?
206-
_configuration.CustomResponse.Headers.Select(h => new HttpHeader(h.Key, h.Value)) :
207-
Array.Empty<HttpHeader>();
206+
_configuration.CustomResponse.Headers.Select(kvp => new HttpHeader(kvp.Key, kvp.Value)).ToArray():
207+
Array.Empty<HttpHeader>();
208208

209209
// allow custom throttling response
210210
var responseCode = (HttpStatusCode)(_configuration.CustomResponse.StatusCode ?? 200);

dev-proxy-plugins/MockResponses/GraphMockResponsePlugin.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ protected override async Task OnRequest(object? sender, ProxyRequestArgs e)
6060
{
6161
Id = request.Id,
6262
Status = (int)HttpStatusCode.BadGateway,
63-
Headers = headersDictionary,
63+
Headers = headersDictionary.Select(x => new KeyValuePair<string, string>(x.Key, x.Value)).ToList(),
6464
Body = new GraphBatchResponsePayloadResponseBody
6565
{
6666
Error = new GraphBatchResponsePayloadResponseBodyError
@@ -84,11 +84,13 @@ protected override async Task OnRequest(object? sender, ProxyRequestArgs e)
8484

8585
if (mockResponse.Response?.Headers is not null)
8686
{
87-
foreach (var key in mockResponse.Response.Headers.Keys)
87+
//Add all the mocked headers into the response we want
88+
foreach (var kvp in mockResponse.Response.Headers)
8889
{
89-
headersDictionary[key] = mockResponse.Response.Headers[key];
90+
headersDictionary.Add(kvp.Key, kvp.Value);
9091
}
9192
}
93+
9294
// default the content type to application/json unless set in the mock response
9395
if (!headersDictionary.Any(h => h.Key.Equals("content-type", StringComparison.OrdinalIgnoreCase)))
9496
{
@@ -127,7 +129,7 @@ protected override async Task OnRequest(object? sender, ProxyRequestArgs e)
127129
{
128130
Id = request.Id,
129131
Status = (int)statusCode,
130-
Headers = headersDictionary,
132+
Headers = headersDictionary.Select(x => new KeyValuePair<string, string>(x.Key, x.Value)).ToList(),
131133
Body = body
132134
};
133135

dev-proxy-plugins/MockResponses/MockResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ public class MockResponseResponse
3030
[JsonPropertyName("body")]
3131
public dynamic? Body { get; set; }
3232
[JsonPropertyName("headers")]
33-
public IDictionary<string, string>? Headers { get; set; }
33+
public List<KeyValuePair<string, string>>? Headers { get; set; }
3434
}

dev-proxy-plugins/MockResponses/MockResponsePlugin.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using System.Text.Json;
1010
using System.Text.Json.Serialization;
1111
using System.Text.RegularExpressions;
12-
using Titanium.Web.Proxy.EventArguments;
1312
using Titanium.Web.Proxy.Http;
1413
using Titanium.Web.Proxy.Models;
1514
using Microsoft.DevProxy.Plugins.Behavior;
@@ -217,18 +216,12 @@ private void ProcessMockResponse(ProxyRequestArgs e, MockResponse matchingRespon
217216

218217
if (matchingResponse.Response?.Headers is not null)
219218
{
220-
foreach (var key in matchingResponse.Response.Headers.Keys)
219+
foreach (HttpHeader headerToAdd in matchingResponse.Response.Headers.Select(kvp => new HttpHeader(kvp.Key, kvp.Value)))
221220
{
222-
// remove duplicate headers
223-
var existingHeader = headers.FirstOrDefault(h => h.Name.Equals(key, StringComparison.OrdinalIgnoreCase));
224-
if (existingHeader is not null)
225-
{
226-
headers.Remove(existingHeader);
227-
}
228-
229-
headers.Add(new HttpHeader(key, matchingResponse.Response.Headers[key]));
221+
headers.Add(headerToAdd);
230222
}
231223
}
224+
232225
// default the content type to application/json unless set in the mock response
233226
if (!headers.Any(h => h.Name.Equals("content-type", StringComparison.OrdinalIgnoreCase)))
234227
{

dev-proxy-plugins/RandomErrors/GraphRandomErrorPlugin.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,7 @@ private void FailBatch(ProxyRequestArgs e)
147147
var retryAfterDate = DateTime.Now.AddSeconds(retryAfterInSeconds);
148148
var requestUrl = ProxyUtils.GetAbsoluteRequestUrlFromBatch(e.Session.HttpClient.Request.RequestUri, request.Url);
149149
e.ThrottledRequests.Add(new ThrottlerInfo(GraphUtils.BuildThrottleKey(requestUrl), ShouldThrottle, retryAfterDate));
150-
response.Headers = new Dictionary<string, string>{
151-
{ "Retry-After", retryAfterInSeconds.ToString() }
152-
};
150+
response.Headers = new List<KeyValuePair<string, string>>{new ( "Retry-After", retryAfterInSeconds.ToString())};
153151
}
154152

155153
responses.Add(response);

dev-proxy-plugins/RequestLogs/MockGeneratorPlugin.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ request.Context is null ||
5151
var methodAndUrl = GetMethodAndUrl(methodAndUrlString);
5252
var response = request.Context.Session.HttpClient.Response;
5353

54+
var newHeaders = new List<KeyValuePair<string, string>>();
55+
newHeaders.AddRange(response.Headers.Select(h => new KeyValuePair<string, string>(h.Name, h.Value)));
5456
var mock = new MockResponse
5557
{
5658
Request = new()
@@ -61,9 +63,7 @@ request.Context is null ||
6163
Response = new()
6264
{
6365
StatusCode = response.StatusCode,
64-
Headers = response.Headers
65-
.Select(h => new KeyValuePair<string, string>(h.Name, h.Value))
66-
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value),
66+
Headers = newHeaders,
6767
Body = GetResponseBody(request.Context.Session).Result
6868
}
6969
};

0 commit comments

Comments
 (0)