Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #358 MockGeneratorPlugin errors if the response headers have a duplicate #372

Merged
merged 12 commits into from
Jan 11, 2024
Merged
2 changes: 1 addition & 1 deletion dev-proxy-abstractions/GraphBatchResponsePayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class GraphBatchResponsePayloadResponse
[JsonPropertyName("body")]
public dynamic? Body { get; set; }
[JsonPropertyName("headers")]
public Dictionary<string, string>? Headers { get; set; }
public List<KeyValuePair<string, string>>? Headers { get; set; }
}

public class GraphBatchResponsePayloadResponseBody
Expand Down
4 changes: 2 additions & 2 deletions dev-proxy-plugins/Behavior/RateLimitingPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ _urlsToWatch is null ||
if (_configuration.CustomResponse is not null)
{
var headers = _configuration.CustomResponse.Headers is not null ?
_configuration.CustomResponse.Headers.Select(h => new HttpHeader(h.Key, h.Value)) :
Array.Empty<HttpHeader>();
_configuration.CustomResponse.Headers.Select(kvp => new HttpHeader(kvp.Key, kvp.Value)).ToArray():
Array.Empty<HttpHeader>();

// allow custom throttling response
var responseCode = (HttpStatusCode)(_configuration.CustomResponse.StatusCode ?? 200);
Expand Down
10 changes: 6 additions & 4 deletions dev-proxy-plugins/MockResponses/GraphMockResponsePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected override async Task OnRequest(object? sender, ProxyRequestArgs e)
{
Id = request.Id,
Status = (int)HttpStatusCode.BadGateway,
Headers = headersDictionary,
Headers = headersDictionary.Select(x => new KeyValuePair<string, string>(x.Key, x.Value)).ToList(),
Body = new GraphBatchResponsePayloadResponseBody
{
Error = new GraphBatchResponsePayloadResponseBodyError
Expand All @@ -84,11 +84,13 @@ protected override async Task OnRequest(object? sender, ProxyRequestArgs e)

if (mockResponse.Response?.Headers is not null)
{
foreach (var key in mockResponse.Response.Headers.Keys)
//Add all the mocked headers into the response we want
foreach (var kvp in mockResponse.Response.Headers)
{
headersDictionary[key] = mockResponse.Response.Headers[key];
headersDictionary.Add(kvp.Key, kvp.Value);
}
}

// default the content type to application/json unless set in the mock response
if (!headersDictionary.Any(h => h.Key.Equals("content-type", StringComparison.OrdinalIgnoreCase)))
{
Expand Down Expand Up @@ -127,7 +129,7 @@ protected override async Task OnRequest(object? sender, ProxyRequestArgs e)
{
Id = request.Id,
Status = (int)statusCode,
Headers = headersDictionary,
Headers = headersDictionary.Select(x => new KeyValuePair<string, string>(x.Key, x.Value)).ToList(),
Body = body
};

Expand Down
2 changes: 1 addition & 1 deletion dev-proxy-plugins/MockResponses/MockResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ public class MockResponseResponse
[JsonPropertyName("body")]
public dynamic? Body { get; set; }
[JsonPropertyName("headers")]
public IDictionary<string, string>? Headers { get; set; }
public List<KeyValuePair<string, string>>? Headers { get; set; }
}
13 changes: 3 additions & 10 deletions dev-proxy-plugins/MockResponses/MockResponsePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using Titanium.Web.Proxy.EventArguments;
using Titanium.Web.Proxy.Http;
using Titanium.Web.Proxy.Models;
using Microsoft.DevProxy.Plugins.Behavior;
Expand Down Expand Up @@ -217,18 +216,12 @@ private void ProcessMockResponse(ProxyRequestArgs e, MockResponse matchingRespon

if (matchingResponse.Response?.Headers is not null)
{
foreach (var key in matchingResponse.Response.Headers.Keys)
foreach (HttpHeader headerToAdd in matchingResponse.Response.Headers.Select(kvp => new HttpHeader(kvp.Key, kvp.Value)))
{
// remove duplicate headers
var existingHeader = headers.FirstOrDefault(h => h.Name.Equals(key, StringComparison.OrdinalIgnoreCase));
if (existingHeader is not null)
{
headers.Remove(existingHeader);
}

headers.Add(new HttpHeader(key, matchingResponse.Response.Headers[key]));
headers.Add(headerToAdd);
}
}

// default the content type to application/json unless set in the mock response
if (!headers.Any(h => h.Name.Equals("content-type", StringComparison.OrdinalIgnoreCase)))
{
Expand Down
4 changes: 1 addition & 3 deletions dev-proxy-plugins/RandomErrors/GraphRandomErrorPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,7 @@ private void FailBatch(ProxyRequestArgs e)
var retryAfterDate = DateTime.Now.AddSeconds(retryAfterInSeconds);
var requestUrl = ProxyUtils.GetAbsoluteRequestUrlFromBatch(e.Session.HttpClient.Request.RequestUri, request.Url);
e.ThrottledRequests.Add(new ThrottlerInfo(GraphUtils.BuildThrottleKey(requestUrl), ShouldThrottle, retryAfterDate));
response.Headers = new Dictionary<string, string>{
{ "Retry-After", retryAfterInSeconds.ToString() }
};
response.Headers = new List<KeyValuePair<string, string>>{new ( "Retry-After", retryAfterInSeconds.ToString())};
}

responses.Add(response);
Expand Down
6 changes: 3 additions & 3 deletions dev-proxy-plugins/RequestLogs/MockGeneratorPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ request.Context is null ||
var methodAndUrl = GetMethodAndUrl(methodAndUrlString);
var response = request.Context.Session.HttpClient.Response;

var newHeaders = new List<KeyValuePair<string, string>>();
newHeaders.AddRange(response.Headers.Select(h => new KeyValuePair<string, string>(h.Name, h.Value)));
var mock = new MockResponse
{
Request = new()
Expand All @@ -61,9 +63,7 @@ request.Context is null ||
Response = new()
{
StatusCode = response.StatusCode,
Headers = response.Headers
.Select(h => new KeyValuePair<string, string>(h.Name, h.Value))
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value),
Headers = newHeaders,
Body = GetResponseBody(request.Context.Session).Result
}
};
Expand Down
Loading