pistache
pistache copied to clipboard
How to unit test fonctions that are bound to routes
Hello,
We are using Pistache for our Rest server and we would like to add some unit tests to our server. We particularly want to test input and output formats to check that errors are returned accordingly.
We have a class method that is bound to one of the route that we want to test. We thought of testing this method directly. However its signature requires a const Rest::Request&
and an Http::ResponseWriter
object. We can't find anywhere on the documentation how to create such objects.
In particular, Http::ResponseWriter
constructor is private and we couldn't find a builder class, so it appears that we can't create an object at all to call that method.
Do you have any pointer to the documentation or an example that could help us ? Either by showing how to build both Rest::Request
and Http::ResponseWriter
to call that method ; or by suggesting another way of testing our server routes (ideally, we don't want to start the server) ?
Thank you !
If you need to test your routes without regard to anything else, consider looking at the existing unit tests in pistache [1], particularly the "test_notfound_exactly_once" and "test_fixed_routes".
[1] https://github.com/oktal/pistache/blob/master/tests/router_test.cc
Thank you for guiding me to these unit tests.
In particular, I used test_notfound_exactly_once as an example. For each test I wanted to start a server and shut it down at the end of each test. However, it happens that I end up with an "Adress already in use" error, even if I use Tcp::Options::ReuseAddr
(as recommended in #468).
I got around that problem by starting the server only once and I now have a working unit tests for my server. Though maybe you would know what can explain this error and how to solve it.