-
Notifications
You must be signed in to change notification settings - Fork 13
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
Add HTTP mocking functionality #21
base: master
Are you sure you want to change the base?
Conversation
Super cool! I probably should have done it this way from the start, it's a great idea. I am busy with life right now, but I will sit down with this hopefully sometime this month and play around with it further. On first glance it looks solid. |
I've toyed around with it a little bit and while it works it needs either a good example or some quality of life (probably both). The way you have to construct a "Short" example: use minreq;
use std::net::{Ipv4Addr, SocketAddrV4};
use vial::{routes, Router, Server};
routes! {
GET "/" => |_| Response::from_file("src/main.rs");
}
fn main() -> Result<(), minreq::Error> {
let expected = include_str!("main.rs");
// All this is setup of the server.
let mut router = Router::new();
vial_add_to_router(&mut router); // !?!
let addr = SocketAddrV4::new(Ipv4Addr::LOCALHOST, 0);
let mut server = Server::new(router, addr.into(), 1, None);
// Need to get and format the address before moving the server to a thread
let addr = format!("http://{}/", server.addr().to_string());
let thread = std::thread::spawn(move || server.run_once());
// Request
let resp = minreq::get(addr).send()?;
_ = thread.join();
assert_eq!(resp.as_str()?, expected);
Ok(())
} As an aside, I think I'll keep playing around with it and see if I can think of something good. |
Finally got back to this. Added a macro so now it is a bit more ergonomic to use. There is some argument to be made that the macro shouldn't be named I also removed the |
Hiya!
vial
is so small and handy I've thought about using it as a dev dependency for local integration tests, so called "HTTP mocking", for a while. It would be an excellent fit compared to existing alternatives. As in most cases you just need to serve a simple page that returns something you expect and that is exactly what vial does.This PR mostly concerns how
server.rs
is structured and adding an optionalmock
feature flag. I mostly just concentrated everything under theServer
struct instead of in therun
function and made a bunch of stuff public.I ran into some race-conditions when jury-rigging this a while back, but these may have been fixed when you changed how state's are managed as I've not run into it yet on the latest release. Might need some more testing.
I've left this as a draft since some neat examples of the "mocking" use-case may want to be written and so you can also go through it all to ensure I am not doing something completely insane.
(Note: The tests in
mock_test.rs
is behind themock
feature flag)