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

Fix content negotiation #1009

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Fix content negotiation #1009

wants to merge 1 commit into from

Conversation

martincostello
Copy link
Member

@martincostello martincostello commented Feb 24, 2025

Fix inability to return different responses based on content negotiation via HTTP request headers by including a hash of their names and values in the key for the registration.


As per the test, I found this while doing some work where I needed to intercept responses to the GitHub API. The Get a pull request has a behaviour where different values for the Accept header change the format of the response.

I had the need to get the JSON representation of a pull request and the diff for it in the same operation. This isn't currently possible using the "simple" API as the HTTP request headers aren't taken into account when storing the interception registrations, which created a Last-One-Wins situation meaning you couldn't have a response for the same HTTP method and URL.

While it was possible using the For() overloads, this has the user fall off a cliff in terms of usability and needs them to write all the matching logic themselves.

In addition to the key changes, the collections used for header collections weren't being copied, meaning that the values also got mutated by reference when the user changed the registration. This part could potentially be a breaking behaviour change if someone is mutating a builder post-request expecting the matched values to change, but I don't think that's the behaviour originally intended in the design so is accidental.

Fix inability to return different responses based on content negotiation via HTTP request headers by including a hash of their names and values in the key for the registration.
@martincostello martincostello added bug A defect in the code or documentation. .NET Pull requests that update .net code labels Feb 24, 2025
@martincostello martincostello added this to the v5.1.1 milestone Feb 24, 2025
Copy link

codecov bot commented Feb 24, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.32%. Comparing base (32d486a) to head (a7ed243).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1009      +/-   ##
==========================================
+ Coverage   99.30%   99.32%   +0.01%     
==========================================
  Files          15       15              
  Lines         868      886      +18     
  Branches      198      204       +6     
==========================================
+ Hits          862      880      +18     
  Misses          4        4              
  Partials        2        2              
Flag Coverage Δ
linux 99.32% <100.00%> (+0.01%) ⬆️
macos 99.32% <100.00%> (+0.01%) ⬆️
windows 99.32% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

martincostello added a commit to martincostello/costellobot that referenced this pull request Feb 24, 2025
- Test changes from justeattakeaway/httpclient-interception#1009 to fix getting a pull request and its diff at the same time.
- Make HTTP interceptions for the GitHub API always specify the `Accept` header that is expected.
@martincostello
Copy link
Member Author

Tested the changes in the app where I found this, and with the build from the PR it works as I'd expect now: martincostello/costellobot#2068

@martincostello martincostello marked this pull request as ready for review February 24, 2025 18:08
@Copilot Copilot bot review requested due to automatic review settings February 24, 2025 18:08
@martincostello martincostello requested a review from a team as a code owner February 24, 2025 18:08
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

martincostello added a commit to martincostello/costellobot that referenced this pull request Feb 25, 2025
- Test changes from justeattakeaway/httpclient-interception#1009 to fix getting a pull request and its diff at the same time.
- Make HTTP interceptions for the GitHub API always specify the `Accept` header that is expected.
martincostello added a commit to martincostello/costellobot that referenced this pull request Feb 25, 2025
Test changes from justeattakeaway/httpclient-interception#1009 to fix getting a pull request and its diff at the same time.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A defect in the code or documentation. .NET Pull requests that update .net code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant