Currently we can use server := ghttp.NewServer() to create a test server, then use server.URL() or server.Addr() to point our clients to the test server.
In some situations code is not written in such a way where the URL can passed to a function, or perhaps paths should be conserved. It is common for libraries to be able to configure a http.Client, and therefore a http.Transport. The interface RoundTripper allows us to take a request, modify it in someway, and return the response/error
Proposal
Add a new method to ghttp.Server which returns a RoundTripper. This can then be used to build http clients, or wrap existing transports (i.e. in req's WrapRoundTriplink).
Because we need to send a real request to our test server, we still require an actual transport. Therefore, the method takes a RoundTripper. If this is nil then we can use http.DefaultTransport.
Summary
Currently we can use
server := ghttp.NewServer()
to create a test server, then useserver.URL()
orserver.Addr()
to point our clients to the test server.In some situations code is not written in such a way where the URL can passed to a function, or perhaps paths should be conserved. It is common for libraries to be able to configure a
http.Client
, and therefore ahttp.Transport
. The interfaceRoundTripper
allows us to take a request, modify it in someway, and return the response/errorProposal
Add a new method to
ghttp.Server
which returns aRoundTripper
. This can then be used to build http clients, or wrap existing transports (i.e. inreq
'sWrapRoundTrip
link).Because we need to send a real request to our test server, we still require an actual transport. Therefore, the method takes a
RoundTripper
. If this isnil
then we can usehttp.DefaultTransport
.For example:
Where the helper
RoundTripperFunc
is as such. Note there is an open issue to add this tonet/http
in https://github.com/golang/go/issues/38479.Example usage