alexliesenfeld / httpmock

HTTP mocking library for Rust.
MIT License
436 stars 40 forks source link

Expose base URL on MockServer #11

Closed Ch00k closed 3 years ago

Ch00k commented 3 years ago

I am implementing a REST client, and using httpmock to test it. I instantiate my client by passing the address of the server (base URL), which is an HTTP(S) URL, like so:

cl = MyClient::new("http://localhost:8042")

Then my client implements various methods, that send HTTP requests to various paths, relative to the base URL. In each of my tests, in order to instantiate my client, I have to compile the base URL manually every time:

let mock_server = MockServer::start();
let url = format!("http://{}:{}", &mock_server.host(), &mock_server.port());

cl = MyClient::new("http://localhost:8042")

// this does format!("{}/{}", self.base_url, "/patients"); under the hood
let resp = cl.list_patients();

Not such a big deal really, it's only one line, but I thought that for convenience MockServer could expose the base URL in a method. For example by making the path argument in url function optional (if path is None, return the base URL).

If that makes sense, I'd be more than happy to make a PR. I am very new to Rust, so it could be that the way I am designing my client (or the way I am testing it) is suboptimal. I'd appreciate an advice on how to do it the right way :)

alexliesenfeld commented 3 years ago

Hi. There is a url method in the MockServer structure (this method), as shown in this example. Does that solve your problem?

Ch00k commented 3 years ago

Well, I just realized that I could do mock_server.url(""), and that will get me the base URL :)