-
Notifications
You must be signed in to change notification settings - Fork 38.8k
MockMvc request builders should expose HttpHeaders #35588
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
Open
mieseprem
wants to merge
3
commits into
spring-projects:main
Choose a base branch
from
mieseprem:fixes_#35576
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+136
−22
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
MultiValueMap<String, Object>
matches to howMockHttpServletRequest
manages header values, allowing Collections, arrays, as well as date and number values, which it then formats when headers are obtained. So this change is unlikely to go without causing regressions.The code in this method could match the behavior of
getDateHeader
andgetIntHeader
ofMockHttpServletRequest
, in reverse by formatting to String values in the same way, to ensure backwards compatibility.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid heading in the wrong direction, I’ll briefly confirm what I’ve understood:
Working internally with
HttpHeaders
isn’t the best idea because of potential regressions. It’s better to continue usingMultiValueMap
internally.So, if we still want to offer a method
headers(Consumer<HttpHeaders>)
, we should consume theHttpHeaders
and then call the existingheaders(HttpHeaders)
method. I can change it in that direction.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That unfortunately does not work.
If you create a new instance of
HttpHeaders
and provide that to theConsumer<HttpHeaders>
, the consumer can only add new headers. It cannot remove or process existing headers.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I think I’ve got it now. Yes — my approach indeed won’t work.
The challenge is "extracting" the
HttpHeaders
from the Consumer without first converting the existingMultiValueMap
toHttpHeaders
and then converting back after consumption. That seems cumbersome. I’ll think about possible solutions.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think there’s any sensible way to implement this? As long as the internal headers map is a
MultiValueMap<String, Object>
, I can’t see how to make it compatible with theMultiValueMap<String, String>
used byHttpHeaders
. I can’t simply change the value type of objects already placed into the builder’s headers map into their string representations — that would almost certainly cause regressions.For a moment I considered an implementation along these lines, but that was based on the incorrect assumption that the Consumer would only contain
HttpHeaders
instructions for purely String‑based header values (to be clear, this code produces the wrong result):If I'm not (again) totally wrong, this should be a test that need to pass:
I think for this to work at all, roughly the following would need to happen:
MultiValueMap<String, Object>
entry aMultiValueMap<String, String>
one (this step alone seems very complex — I’m thinking of your point aboutgetDateHeader(String)
andgetIntHeader(String)
)HttpHeaders
object from aMultiValueMap<String, String>
(call that X)Either I’m overthinking this, I’ve gone completely off track, or I’m perhaps not the right person to implement this task 🤷
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry if this wasn't clear, but my suggestion was not to change to
MultiValueMap
. KeepHttpHeaders
internally, as you have done, and improve the way values are formatted. So instead of a simpleString#valueOf
, we would need to match the formatting that would be done otherwise inMockHttpServletRequest
.For this you can trace the code to add and obtain headers in
MockHttpServletRequest
. You will see thatdoAddHeaderValue
recognizes collections and arrays. AlsogetDateHeader
andgetIntHeader
recognize various additional value types.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, got it. So I will give it another try 👍