-
-
Notifications
You must be signed in to change notification settings - Fork 8
Add unit-testing example for POST requests #1
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
Comments
hi @wbprice sorry I just now noticed this. There are few ways of creating requests with the http crate. the new fn and the builder fn. The Here's a quick sketch
I have an example of a unit test here if you are looking for inspiration. |
Thanks! I eventually figured something out. The tricky thing was that
When I write better tests I'll open a PR to this or the parent repo. |
Thanks @wbprice. good to hear. I'm curious. What was the error you got with |
It's worth noting that I'm using With a handler and test written like so:
I get the following error at compilation.
|
Ah bingo! The lambda-http crate also re-exports the http crate types under Try |
Also I see you are trying to gain access to something like a path parameter. You can gain access to |
also what you're doing with body parsing couldn't request.payload. that should handle both form post and application/json request bodies, deserializing to a serde deserliaizable type ( including |
Thanks, that's really helpful!
In testing, this logs:
I'm guessing the behavior for getting the path variable from the URI would normally be configured by Serverless in API Gateway. Is there a way to mock this behavior in a unit test? I'm guessing I should start looking at creating a custom context for the request? |
I like where you're taking this. I don't have an immediate answer but I'll get back to you. I just wanted to let you know I at least saw your question. |
So methods of populating extension methods that extend from api gateway events are not currently exposed (intentionally) as public methods because they may change in the future and aren't meant for public consumption. In a unit test scenario you do have some options though The extension interface is defined as a trait which just happens to be implemented by Define a method that your handler calls to extract what fields you need from path parameters fn extract_path_params<R>(request: R) -> WhatYouNeed where R: RequestExt {
let params = request.path_parameters();
extractWhatYouNeed(params)
} Note path_parameters (and other ext methods) return a StrMap type. You can construct these from a HashMap<String, Vec> at the moment. inside the handler you could pass the request to struct MockPathParams(HashMap<String, Vec<String>>);
impl RequestExt for MockPathParams {
fn path_parameters(&self) -> StrMap {
self.0.into()
}
fn query_string_parameters(&self) -> StrMap {
unreachable!()
}
// unreachable! impls for other RequestExt methods as well.
} then in a unit test where you would normally pass the request to It's understandable this is a bit awkward for now but I'm going to see if I can take this usecase and think about it a bit more. For now this solution may get you unblocked. |
alternatively you could extract what info you need from the handler to a method to another method which is then a bit more straightforward to drive a unit tests |
Thanks for your help with this! I don't completely understand the workaround but I think I'll make a note to study up on traits and revisit this later. In the meantime your comments have been really helpful. |
Hi,
I'm trying to write a unit test for a
POST
request handler that echos a JSON payload.I'm expecting to be able to set the method, headers, etc (similar to the request builder as described in the http crate documentation) but I get error messages like the following:
Can you point me to any documentation that may help with this?
My code attached for background:
The text was updated successfully, but these errors were encountered: