LukeMathWalker / wiremock-rs

HTTP mocking to test Rust applications.
Apache License 2.0
607 stars 69 forks source link

feat: implement Debug trait for MockServer #134

Closed mbechto closed 7 months ago

mbechto commented 8 months ago

Description of changes:

Provides an implementation for the std::fmt::Debug trait for wiremock::MockServer. I thought let's keep it simple, so it just prints the address:

MockServer { address: 127.0.0.1:54181 }

Motivation: cucumber-rs requires all implementations of cucumber::World to implement Debug. Given all members implement Debug, it can be derived:

#[derive(Default, Debug, cucumber::World)]
World { // keeps all state of a test scenario, therefore mock_server fits well
  ...
  pub mock_server: Option<wiremock::MockServer>, // <- compilation error
}

Of course it is possible to implement Debug for World, but less ergonomic. Hence this PR.

Please tell me what you think - I am happy to provide any changes if required!

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

LukeMathWalker commented 8 months ago

I'm quite happy to add a Debug implementation, but I would prefer to see a #[derive(Debug)] on MockServer and a manual implementation of Debug on BareMockServer, which prints just the address as you currently do.

mbechto commented 8 months ago

Sure, can do. Since it is a tuple struct, it would look like this:

MockServer(address: 127.0.0.1:54942)

or, if implemented canonically:

MockServer(BareMockServer { address: 127.0.0.1:54942 })

Which version do you prefer @LukeMathWalker?

LukeMathWalker commented 8 months ago

Let's go for the canonical version, since it'll include the variant information which can be useful when debugging.

mbechto commented 8 months ago

It turned out not as elegant as I hoped... the main problem being that it's not possible to implement Debug for PooledMockServer, which is a custom type consisting of deadpool::managed::Object<T>. Therefore, I needed to implement Debug for the InnerServer enum.

And I have now added a test for it, please have a look.

What do you think?

LukeMathWalker commented 8 months ago

deadpool's Object implements Debug if the type managed by the pool implements Debug and the manager itself implements Debug—see https://docs.rs/deadpool/latest/deadpool/managed/struct.Object.html#impl-Debug-for-Object%3CM%3E

So it should be enough to derive Debug for https://github.com/LukeMathWalker/wiremock-rs/blob/eef4eb5ecc592f3532c79abfa02b0171909bc7a1/src/mock_server/pool.rs#L52

mbechto commented 8 months ago

Nice, I wasn't aware of that, thanks!

Using #[derive(Debug)] on MockServer all the way down to MockServerPoolManager gives us this rather lengthy (but indeed very accurate) string:

MockServer(Pooled(Object { inner: Some(ObjectInner { obj: BareMockServer { address: 127.0.0.1:60130 }, metrics: Metrics { created: Instant { tv_sec: 261427, tv_nsec: 447625625 }, recycled: None, recycle_count: 0 } }) }))

And in the unpooled case simply:

MockServer(Bare(BareMockServer { address: 127.0.0.1:60166 }))

Is that fine?

LukeMathWalker commented 8 months ago

Yup, let's keep maximum fidelity as our first pass at implementing Debug.

mbechto commented 8 months ago

Please review @LukeMathWalker