diff --git a/apitest.go b/apitest.go index 9e7733e..4c1b751 100644 --- a/apitest.go +++ b/apitest.go @@ -11,6 +11,7 @@ type Apitest struct { Handler http.Handler // handler to test Server *httptest.Server // testing server Base string // Base uri to make requests + client *http.Client // Default Http Client to use in requests clients chan *http.Client // http clients } @@ -22,6 +23,7 @@ func New(h http.Handler) *Apitest { func NewWithBase(base string) *Apitest { return &Apitest{ + client: http.DefaultClient, Base: base, } } @@ -39,6 +41,7 @@ func NewWithPool(h http.Handler, n int) *Apitest { Handler: h, Server: s, clients: make(chan *http.Client, n), + client: http.DefaultClient, } for i := 0; i < cap(a.clients); i++ { @@ -55,6 +58,16 @@ func NewWithPool(h http.Handler, n int) *Apitest { return a } +func (a *Apitest) WithHttpClient(client *http.Client) *Apitest { + a.client = client + if a.clients != nil { + for i := 0; i < cap(a.clients); i++ { + a.clients <- client + } + } + return a +} + func (a *Apitest) Destroy() { if nil != a.Server { a.Server.Close() diff --git a/request.go b/request.go index 40436f4..ff025b1 100644 --- a/request.go +++ b/request.go @@ -12,6 +12,7 @@ import ( type Request struct { http.Request apitest *Apitest + client *http.Client } func NewRequest(method, urlStr string, a *Apitest) *Request { @@ -21,7 +22,7 @@ func NewRequest(method, urlStr string, a *Apitest) *Request { panic(err) } - return &Request{*http_request, a} + return &Request{*http_request, a, a.client} } @@ -89,12 +90,19 @@ func (r *Request) WithBodyJson(o interface{}) *Request { return r } +func (r *Request) WithHttpClient(client *http.Client) *Request { + + r.client = client + + return r +} + func (r *Request) Do() *Response { tee := &tee{Buffer: r.Request.Body} r.Request.Body = tee - res, err := http.DefaultClient.Do(&r.Request) + res, err := r.client.Do(&r.Request) if err != nil { panic(err) }