-
Notifications
You must be signed in to change notification settings - Fork 10
URL-Encoded Form Support #57
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
base: main
Are you sure you want to change the base?
Conversation
|
Thanks for the contribution @MATATAT! I'll have a look today (both versions) and provide feedback later. FYI, the DCO check is failing as our organization requires commits to be signed. I think |
Signed-off-by: Matt Macdonald <matt.david.macdonald@gmail.com>
Signed-off-by: Matt Macdonald <matt.david.macdonald@gmail.com>
Signed-off-by: Matt Macdonald <matt.david.macdonald@gmail.com>
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.
Thanks @MATATAT!
Since form data is body content, this can be simplified to be an additional When body convenience method for the existing BodyMatcher, instead of adding a new matcher and FormBody struct.
I would suggest the following implementation for When (accepting multiple kv-pairs for convenience):
/// Body convenience methods.
impl When {
// ...
/// URL encoded form body.
pub fn form(
self,
params: impl IntoIterator<Item = (impl Into<String>, impl Into<String>)>,
) -> Self {
let params = params
.into_iter()
.map(|(key, value)| (key.into(), value.into()))
.collect::<Vec<(String, String)>>();
let body = serde_urlencoded::to_string(params).unwrap();
self.push(matchers::body(Body::bytes(body)));
self
}
}Usage:
let mut mocks = MockSet::new();
mocks.mock(|when, then| {
when.post()
.header("content-type", "application/x-www-form-urlencoded")
.form([("hello", "world")]);
then.text("hello world");
});I tested and this appears to work, let me know what you think.
|
Thanks @declark1, let me take a look at this. I believe I had something like this before and there was an issue where the matcher didn't end up being consistent since the HashMap doesn't enforce insertion order. So when the body matcher would compare the byte order was sporadically inconsistent. I'll look back through my commits when I was doing this via an extension trait and see if I left any comments about this (or if my implementation was different). |
|
Hey! Sorry it took me a while to circle back around on this. The problem I was running into (and was able to repro this with your suggested changes) are that using the Body matcher the values must maintain their insert order. Since I do like your approach to the inputs, so I'd like to incorporate that, but I do think there will need to be a separate matcher for the form body. I might have some time this weekend so I'll make some changes and push another commit. |
Added support for URL encoded forms to framework. This was based off a quick extension trait that I was using in a project I'm working on.
Alternatively, I wrote a separate version of this that follows the
httpmockinterface more closely. It was a lot more invasive than this change. Here is a link to the diff if you'd like to consider that approach as well.