Testing the REST API Server
In addition to manually checking the server with curl, let’s add some structured tests using Go’s testing package. Go provides the package net/http/httptest with additional types and functions for testing HTTP servers.
One approach for testing HTTP servers is testing each handler function individually by using the type httptest.ResponseRecorder. This type allows the recording of an HTTP response for analysis or tests. This approach is useful if you’re using the DefaultServeMux as the server multiplexer.
Because you implemented your own multiplexer function, newMux, you can use a different approach that allows integrated testing, including the route dispatching. You’ll use the type httptest.Server and instantiate ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access