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<Dictionary<string, string>>? Headers { get; set; }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of creating a separate Dictionary for each key-value pair, let's use a List<Tuple<string, string>> which seems like a less overhead. Alternatively, we could use List<HttpHeader>, assuming we can serialize them easily.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at HttpHeader, it is not very nice to serialise, so opting for the Tuple

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this to a List<KeyValuePair<string,string>> instead of Tuple. Made it more readable.

}

public class GraphBatchResponsePayloadResponseBody
Expand Down
6 changes: 3 additions & 3 deletions dev-proxy-plugins/Behavior/RateLimitingPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,9 @@ _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>();
var headers = _configuration.CustomResponse.ResponseHeaders is not null ?
_configuration.CustomResponse.ResponseHeaders.SelectMany(dict => dict.Select(kv => new HttpHeader(kv.Key, kv.Value))).ToArray():
Array.Empty<HttpHeader>();

// allow custom throttling response
var responseCode = (HttpStatusCode)(_configuration.CustomResponse.StatusCode ?? 200);
Expand Down
11 changes: 4 additions & 7 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 = new List<Dictionary<string, string>> { headersDictionary },
Body = new GraphBatchResponsePayloadResponseBody
{
Error = new GraphBatchResponsePayloadResponseBodyError
Expand All @@ -82,12 +82,9 @@ protected override async Task OnRequest(object? sender, ProxyRequestArgs e)
statusCode = (HttpStatusCode)mockResponse.Response.StatusCode;
}

if (mockResponse.Response?.Headers is not null)
if (mockResponse.Response?.ResponseHeaders is not null)
{
foreach (var key in mockResponse.Response.Headers.Keys)
{
headersDictionary[key] = mockResponse.Response.Headers[key];
}
mockResponse.Response.ResponseHeaders.SelectMany(dict => dict.Select(kv => headersDictionary[kv.Key] = kv.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 +124,7 @@ protected override async Task OnRequest(object? sender, ProxyRequestArgs e)
{
Id = request.Id,
Status = (int)statusCode,
Headers = headersDictionary,
Headers = new List<Dictionary<string, string>> { headersDictionary },
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<IDictionary<string, string>>? ResponseHeaders { get; set; }
waldekmastykarz marked this conversation as resolved.
Show resolved Hide resolved
}
14 changes: 4 additions & 10 deletions dev-proxy-plugins/MockResponses/MockResponsePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,20 +215,14 @@ private void ProcessMockResponse(ProxyRequestArgs e, MockResponse matchingRespon
statusCode = (HttpStatusCode)matchingResponse.Response.StatusCode;
}

if (matchingResponse.Response?.Headers is not null)
if (matchingResponse.Response?.ResponseHeaders is not null)
{
foreach (var key in matchingResponse.Response.Headers.Keys)
foreach (var key in matchingResponse.Response.ResponseHeaders.SelectMany(dict => dict.Select(kv => new HttpHeader(kv.Key, kv.Value))))
waldekmastykarz marked this conversation as resolved.
Show resolved Hide resolved
{
// 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(key);
}
}

// 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: 2 additions & 2 deletions dev-proxy-plugins/RandomErrors/GraphRandomErrorPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ 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>{
response.Headers = new List<Dictionary<string, string>>{new Dictionary<string, string>{
{ "Retry-After", retryAfterInSeconds.ToString() }
};
}};
}

responses.Add(response);
Expand Down
4 changes: 2 additions & 2 deletions dev-proxy-plugins/RequestLogs/MockGeneratorPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ request.Context is null ||
Response = new()
{
StatusCode = response.StatusCode,
Headers = response.Headers
ResponseHeaders = new List<IDictionary<string, string>> {response.Headers
.Select(h => new KeyValuePair<string, string>(h.Name, h.Value))
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value),
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value) },
Body = GetResponseBody(request.Context.Session).Result
}
};
Expand Down
Loading