Skip to content

Commit

Permalink
Merge pull request #2 from jonathannegrin/master
Browse files Browse the repository at this point in the history
Include methods to override default http clients to make requests
  • Loading branch information
fulldump authored May 14, 2018
2 parents 3d86db1 + 7356442 commit c12d188
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
13 changes: 13 additions & 0 deletions apitest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -22,6 +23,7 @@ func New(h http.Handler) *Apitest {

func NewWithBase(base string) *Apitest {
return &Apitest{
client: http.DefaultClient,
Base: base,
}
}
Expand All @@ -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++ {
Expand All @@ -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()
Expand Down
12 changes: 10 additions & 2 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type Request struct {
http.Request
apitest *Apitest
client *http.Client
}

func NewRequest(method, urlStr string, a *Apitest) *Request {
Expand All @@ -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}

}

Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit c12d188

Please sign in to comment.