Skip to content

Commit

Permalink
Save request payload
Browse files Browse the repository at this point in the history
  • Loading branch information
fulldump committed Oct 3, 2017
1 parent 57ca44d commit 86e1ce0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
8 changes: 6 additions & 2 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,15 @@ func (r *Request) WithBodyJson(o interface{}) *Request {

func (r *Request) Do() *Response {

tee := &tee{Buffer: r.Request.Body}
r.Request.Body = tee

res, err := http.DefaultClient.Do(&r.Request)
if err != nil {
panic(err)
}

return &Response{Response: *res, body_bytes: nil}
return &Response{Response: *res, response_body_bytes: nil, request_body_bytes: tee.Bytes}
}

func (r *Request) DoAsync(f func(*Response)) {
Expand All @@ -106,10 +109,11 @@ func (r *Request) DoAsync(f func(*Response)) {
if err != nil {
panic(err)
}
wresponse := &Response{*response, r.apitest, c, nil}
wresponse := &Response{*response, r.apitest, c, nil, nil}
f(wresponse)

wresponse.BodyClose()

r.apitest.clients <- c
}

21 changes: 15 additions & 6 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,31 @@ import (

type Response struct {
http.Response
apitest *Apitest
client *http.Client
body_bytes []byte
apitest *Apitest
client *http.Client
response_body_bytes []byte
request_body_bytes []byte
}

func (r *Response) BodyClose() {
io.Copy(ioutil.Discard, r.Body)
r.Body.Close()
}

func (r *Response) BodyRequestBytes() []byte {
return r.request_body_bytes
}

func (r *Response) BodyRequestString() string {
return string(r.BodyRequestBytes())
}

func (r *Response) BodyBytes() []byte {

if nil == r.body_bytes {
if nil == r.response_body_bytes {
body, err := ioutil.ReadAll(r.Body)

r.body_bytes = body
r.response_body_bytes = body

if err != nil {
panic(err)
Expand All @@ -34,7 +43,7 @@ func (r *Response) BodyBytes() []byte {

}

return r.body_bytes
return r.response_body_bytes
}

func (r *Response) BodyString() string {
Expand Down
22 changes: 22 additions & 0 deletions tee.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package apitest

import "io"

type tee struct {
Buffer io.ReadCloser
Bytes []byte
}

func (t *tee) Read(p []byte) (n int, err error) {

n, err = t.Buffer.Read(p)

t.Bytes = append(t.Bytes, p[:n]...)

return
}

func (t *tee) Close() (err error) {
return t.Buffer.Close()

}

0 comments on commit 86e1ce0

Please sign in to comment.