canonical / pebble

Take control of your internal daemons!
https://canonical-pebble.readthedocs-hosted.com/
GNU General Public License v3.0
143 stars 54 forks source link

feat(client): allow tests to assign a fake requester #319

Closed flotter closed 10 months ago

flotter commented 10 months ago

The client test suite used by all client command tests fakes the doer today, which is a private internal Requester implementation detail. Derived projects using the client Requester for extended commands needs to fake the Requester interface.

This PR adds a client method SetRequester(rw Requester) to allow test code to fake the requester. This has to be public to allow an external package to fake this.

Pending work remain to properly refactor the existing client test code to Fake the Requester instead of the doer. This is captured as an outstanding issue here: https://github.com/canonical/pebble/issues/315

flotter commented 10 months ago

I will take a slightly different approach as used elsewhere in Pebble:

func (s *BasePebbleSuite) RedirectClientToTestServer(handler func(http.ResponseWriter, *http.Request)) {
        server := httptest.NewServer(http.HandlerFunc(handler))
        s.BaseTest.AddCleanup(func() { server.Close() })
        cli.ClientConfig.BaseURL = server.URL
        s.BaseTest.AddCleanup(func() { cli.ClientConfig.BaseURL = "" })
}