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

Add HTTP mocking functionality #21

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

CuriouslyCurious
Copy link
Contributor

@CuriouslyCurious CuriouslyCurious commented Apr 4, 2023

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 optional mock feature flag. I mostly just concentrated everything under the Server struct instead of in the run 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 the mock feature flag)

@xvxx
Copy link
Owner

xvxx commented Apr 9, 2023

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.

@CuriouslyCurious
Copy link
Contributor Author

CuriouslyCurious commented Apr 13, 2023

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 Serveris a bit verbose and uses the macro-created function, not the prettiest nor most obvious process.

"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 with_file() and subsequently from_file() in Response should maybe print an error when the file doesn't exist, as returning a 404 is the default behaviour for any path that does not exist meaning it gets a bit confusing if you are getting a 404 because the path doesn't exist or because the file doesn't exist.

I'll keep playing around with it and see if I can think of something good.

@CuriouslyCurious
Copy link
Contributor Author

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 run_once! and instead should be renamed mock! so it is clear what its purpose is and also can't be confused with run!. However, run_once! is quite descriptive of what it does and the doc does say what the differences are in functionality.

I also removed the mock feature flag as it isn't really necessary.

@CuriouslyCurious CuriouslyCurious marked this pull request as ready for review September 4, 2023 15:05
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

Successfully merging this pull request may close these issues.

2 participants