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

Easy way to create a Request to feed to unit tests? #41

Open
icefoxen opened this issue Nov 13, 2016 · 2 comments
Open

Easy way to create a Request to feed to unit tests? #41

icefoxen opened this issue Nov 13, 2016 · 2 comments

Comments

@icefoxen
Copy link

Pretty much what the title says. It would be nice to have a function that makes it easy to create a Request to hand to my various handler functions. As it is, creating a Request using new() requires a HttpRequest from hyper, which requires a NetworkStream... It'd be nice to have a good way to essentially forge a request with no network connection actually involved.

@icefoxen
Copy link
Author

icefoxen commented Nov 14, 2016

Okay, so here's the skinny...

Creating a Request requires a HttpRequest, which is a Hyper server::Request as documented here: http://hyper.rs/hyper/v0.9.12/hyper/server/request/struct.Request.html

Request::new() uses the deconstruct() method of the HttpRequest to get the various bits and pieces it needs. Namely, (SocketAddr, Method, Headers, RequestUri, HttpVersion, HttpReader<&'a mut BufReader<&'b mut NetworkStream>>). All of these are trivially constructed except for the HttpReader which contains the body of the request... BUT, it just takes any Read object, not necessarily a BufReader containing a NetworkStream. So we can easily create an in-memory buffer and build a HttpReader around that, and just go from there.

Addendum: It's not quite that straightforward since the actual Request type stores the HttpRequest, for some reason, and a bit of type-foolery has to be done to deal with this. Ownership also gets sticky.

@pyrossh
Copy link

pyrossh commented Nov 30, 2016

I guess the HttpRequest is removed in version 0.3.0. Something like supertest would be good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants