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

Conversation

mosoftwareenterprises
Copy link
Contributor

If a response is recorded and contains the same header key value pair more than once, then an exception is thrown.

This change removes it by only allowing distinct values of the keys.

While I was working with the mock responses file, I noticed you could not comment them out, so a small tweak to allow that too.

This can be tested by hitting the https://graph.microsoft.com/v1.0/users endpoint, and paginating through the list of users you find e.g.

       var graphServiceClient= new GraphServiceClient(requestAdapter);

            // send a get users request to the graph api; note that the results are paged
            var allUsers = new List<User>();
            var usersPage = await graphServiceClient.Users.Request().Filter("accountEnabled eq true").GetAsync();
            do
            {
                allUsers.AddRange(usersPage);
            }
            while (usersPage.NextPageRequest != null && (usersPage = await usersPage.NextPageRequest.GetAsync()).Any());

Mark Oliver and others added 2 commits October 30, 2023 16:15
…duplicate header

Resolved by only including distinct headers in the response list.
@mosoftwareenterprises mosoftwareenterprises requested a review from a team as a code owner October 30, 2023 16:23
@mosoftwareenterprises
Copy link
Contributor Author

@microsoft-github-policy-service agree

@sebastienlevert
Copy link
Contributor

Thanks for the PR @mosoftwareenterprises! I'm curious what are the headers that are present twice?

@mosoftwareenterprises
Copy link
Contributor Author

Thanks for the PR @mosoftwareenterprises! I'm curious what are the headers that are present twice?

I have just spent the last 20mins trying to replicate the issue, and could only do so via browsing in google,
It is the Set-Cookie header specifically in this case.
Screenshot 2023-10-30 173300

@sebastienlevert
Copy link
Contributor

Some scenarios will need multiple headers (on Graph, for instance with Prefer). I'm wondering if our data structure here is the right one. @waldekmastykarz or @gavinbarron would you mind chiming in?

@gavinbarron
Copy link
Contributor

oof. yeah, multiple headers with the same name are allowed, and is some cases required. Using a dictionary here is not really the right data structure.
We should probably be using something like List<KeyValuePair<string, string>> instead.
Or Dictionary<string,string[]> would also work but require more work to populate.

@waldekmastykarz
Copy link
Collaborator

Good point @gavinbarron. We should definitely change how we store headers to List<KeyValuePair<string, string>>. @mosoftwareenterprises, would you be willing to update your PR?

@mosoftwareenterprises
Copy link
Contributor Author

Yes @waldekmastykarz I will attempt an update to the PR.

Question though, as the changes are going to be further reaching than before, how do I regression test? I dont see any automated tests in the repo?

@waldekmastykarz
Copy link
Collaborator

Yes @waldekmastykarz I will attempt an update to the PR.

Question though, as the changes are going to be further reaching than before, how do I regression test? I dont see any automated tests in the repo?

At this stage we'll have to test it manually. Sorry we don't have a better answer at this moment

@waldekmastykarz waldekmastykarz marked this pull request as draft November 7, 2023 13:01
@waldekmastykarz
Copy link
Collaborator

Hey @mosoftwareenterprises, are you still working on this? Anything that we could help with?

@mosoftwareenterprises
Copy link
Contributor Author

Hey @waldekmastykarz . Sorry I have been so busy, I have not had time to finish it. I will commit the latest changes here, but I have not checked it through yet.

@@ -4,6 +4,8 @@
using System.Net;
using System.Text.Json;
using System.Text.RegularExpressions;
using Microsoft.EntityFrameworkCore.Metadata;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are we using EntityFrmeworkCore for something or is this a leftover?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Must be a leftover. Removed

@@ -18,7 +18,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.

It seems odd to define headers as a list of dictionaries. Shouldn't it be a list of key-value pairs instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using a Dictionary (which is in effect the same thing AFAIK), means we can use Linq to do "ToDictionary()"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This has now changed to a List<KeyValuePair<string,string>> due to the dictionary complicating some other areas.

@waldekmastykarz
Copy link
Collaborator

Hey @mosoftwareenterprises, are you still working on this?

@mosoftwareenterprises
Copy link
Contributor Author

Hey @waldekmastykarz, I will be picking this up again this week. So hopefully I will have something for you soon.

@waldekmastykarz
Copy link
Collaborator

Awesome! Looking forward to it! We've done some changes in the meantime, so please don't forget to pull the latest changes to avoid conflicts 😊

@mosoftwareenterprises mosoftwareenterprises marked this pull request as ready for review January 9, 2024 19:29
Copy link
Collaborator

@waldekmastykarz waldekmastykarz left a comment

Choose a reason for hiding this comment

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

Hey @mosoftwareenterprises, it seems like your PR includes some additional commits that aren't a part of your change. Could you please clean it up so that we can review it more easily? We appreciate your help

@waldekmastykarz waldekmastykarz marked this pull request as draft January 10, 2024 09:34
@mosoftwareenterprises
Copy link
Contributor Author

@waldekmastykarz I assume you are referring to the change to correct the CONTRIBUTING.md file which is incorrect, and the changes to add the serializer options in to ignore comments etc.
I have removed them both.
Let me know if you meant something else.

@mosoftwareenterprises mosoftwareenterprises marked this pull request as ready for review January 10, 2024 10:24
@waldekmastykarz
Copy link
Collaborator

@waldekmastykarz I assume you are referring to the change to correct the CONTRIBUTING.md file which is incorrect, and the changes to add the serializer options in to ignore comments etc. I have removed them both. Let me know if you meant something else.

Correct, I meant those files. I'll have another look just to double check we're not including anything else that shouldn't be there. Looking forward to checking out your work!

Copy link
Collaborator

@waldekmastykarz waldekmastykarz left a comment

Choose a reason for hiding this comment

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

Nice! Let's do a few changes before we continue

@@ -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.

dev-proxy-plugins/MockResponses/MockResponse.cs Outdated Show resolved Hide resolved
dev-proxy-plugins/MockResponses/MockResponsePlugin.cs Outdated Show resolved Hide resolved
@waldekmastykarz waldekmastykarz marked this pull request as draft January 10, 2024 11:31
Mark Oliver added 2 commits January 10, 2024 12:13
…s have a duplicate.

Changes following code review.
…s have a duplicate.

Changes following code review and further testing
@mosoftwareenterprises
Copy link
Contributor Author

I have tested this again, using a Confluence page (private) and found it was not always working, so additional changes made to cope with that. Ready for review again.

@mosoftwareenterprises mosoftwareenterprises marked this pull request as ready for review January 10, 2024 13:11
@waldekmastykarz waldekmastykarz merged commit e020e5b into microsoft:main Jan 11, 2024
4 checks passed
@waldekmastykarz
Copy link
Collaborator

Thanks @mosoftwareenterprises! We'll do some further adjustments to streamline it across all plugins. We appreciate your help ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants