-
Notifications
You must be signed in to change notification settings - Fork 144
Make Request testing easier #203
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
Conversation
|
I don't think this is actually ready to merge yet. I think I may have written a bug where the body of the request doesn't actually make it into the |
|
Here's a minimal failing example, which I've taken from the use tiny_http::{Method, MockRequest, Request, Response, Server, StatusCode};
use std::io::Cursor;
let mut request = MockRequest::new()
.with_method(Method::Post)
.with_path("/api/widgets")
.with_body("42");
struct TestServer {
listener: Server,
}
let server = TestServer {
listener: Server::http("0.0.0.0:0").unwrap(),
};
impl TestServer {
fn handle_request(&self, mut request: Request) -> Response<Cursor<Vec<u8>>> {
let mut request_payload = String::new();
request
.as_reader()
.read_to_string(&mut request_payload)
.unwrap();
assert_eq!(request_payload, "42");
Response::from_string("test")
}
}
let response = server.handle_request(request.into());
assert_eq!(response.status_code(), StatusCode(200));The Notably, I see the string—and even the byte array—inside I'm now somewhat suspicious of @bradfier Any ideas? |
|
Believe I fixed the issue. By default, I'm getting results that are more like what I'd expect from my downstream code, so I think this is ready for consideration again. |
|
I think this is pretty great, thanks for putting in the effort! Clippy had one comment where you can replace the What do you think about putting this under |
I think this is a good idea, except for two problems I've found after trying to actually implement it just now. The first problem is that The other problem is that I expect that making the entire |
|
This would be so easily solved if I had a look around and if we take Actix as inspiration I think we can just add a Bikeshedding a bit, I can't decide if |
I didn't understand what you meant when you mentioned this earlier, but this problem bit me in an unrelated situation the other day, and now I agree that this is an issue. Anyway, I renamed These changes work for me, so unless you've got other concerns I think this is ready to merge. |
bradfier
left a comment
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.
Other than the one line above this is great, happy to merge!
| fn default() -> Self { | ||
| TestRequest { | ||
| body: "", | ||
| remote_addr: "0.0.0.0:0".parse().unwrap(), |
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.
Just one more thing! It feels picky, but 0.0.0.0 isn't actually a valid IP address in this context, we should default to 127.0.0.1.
|
I realised there was no good excuse to push such a minor change off onto you, thanks again for the patch! |
Fixes #189.
MockRequestso downstream users can buildRequest-like objects for testing without exposingRequesttoo muchMockRequest(i.e. the user can chain methods together to build the request they want)into_reader()andheaders()toResponseso the user can inspect test results