Skip to content

Commit

Permalink
Merge branch 'release/v0.2.8'
Browse files Browse the repository at this point in the history
  • Loading branch information
parnurzeal committed Aug 10, 2015
2 parents 3ac2238 + ae87faf commit 4d93c59
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

v0.2.8 (2015-08-10)

Bug/Fixes
* Added SetDebug and SetLogger for debug mode (PR #28 by @dafang)
* Ensure the response Body is reusable (PR #37 by alaingilbert)

v0.2.7 (2015-07-11)

Bug/Fixes
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ The callbacks work the same way as with `End`, except that a byte array is used
resp, bodyBytes, errs := gorequest.New().Get("http://example.com/").EndBytes()
```

## Debug

For deugging, GoRequest leverages `httputil` to dump details of every request/response. (Thanks to @dafang)

You can just use `SetDebug` to enable/disable debug mode and `SetLogger` to set your own choice of logger.

## Noted
As the underlying gorequest is based on http.Client in most usecases, gorequest.New() should be called once and reuse gorequest as much as possible.

Expand All @@ -198,7 +204,7 @@ If you find any improvement or issue you want to fix, feel free to send me a pul

Thanks to all contributers thus far:

@kemadz, @austinov, @figlief, @dickeyxxx, @killix, @jaytaylor, @na-ga
@kemadz, @austinov, @figlief, @dickeyxxx, @killix, @jaytaylor, @na-ga, @dafang, and @alaingilbert

Also, co-maintainer is needed here. If anyone is interested, please email me (parnurzeal at gmail.com)

Expand Down
47 changes: 44 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import (
"encoding/json"
"errors"
"io/ioutil"
"log"
"net"
"net/http"
"net/http/cookiejar"
"net/http/httputil"
"net/url"
"os"
"reflect"
"strings"
"time"
Expand Down Expand Up @@ -46,6 +49,8 @@ type SuperAgent struct {
Cookies []*http.Cookie
Errors []error
BasicAuth struct{ Username, Password string }
Debug bool
logger *log.Logger
}

// Used to create a new SuperAgent object.
Expand All @@ -65,10 +70,23 @@ func New() *SuperAgent {
Cookies: make([]*http.Cookie, 0),
Errors: nil,
BasicAuth: struct{ Username, Password string }{},
Debug: false,
logger: log.New(os.Stderr, "[gorequest]", log.LstdFlags),
}
return s
}

// Enable the debug mode which logs request/response detail
func (s *SuperAgent) SetDebug(enable bool) *SuperAgent {
s.Debug = enable
return s
}

func (s *SuperAgent) SetLogger(logger *log.Logger) *SuperAgent {
s.logger = logger
return s
}

// Clear SuperAgent data for another new request.
func (s *SuperAgent) ClearSuperAgent() {
s.Url = ""
Expand Down Expand Up @@ -293,9 +311,9 @@ func (s *SuperAgent) Timeout(timeout time.Duration) *SuperAgent {
// Set TLSClientConfig for underling Transport.
// One example is you can use it to disable security check (https):
//
// gorequest.New().TLSClientConfig(&tls.Config{ InsecureSkipVerify: true}).
// Get("https://disable-security-check.com").
// End()
// gorequest.New().TLSClientConfig(&tls.Config{ InsecureSkipVerify: true}).
// Get("https://disable-security-check.com").
// End()
//
func (s *SuperAgent) TLSClientConfig(config *tls.Config) *SuperAgent {
s.Transport.TLSClientConfig = config
Expand Down Expand Up @@ -563,15 +581,38 @@ func (s *SuperAgent) EndBytes(callback ...func(response Response, body []byte, e

// Set Transport
s.Client.Transport = s.Transport

// Log details of this request
if s.Debug {
dump, err := httputil.DumpRequest(req, true)
s.logger.SetPrefix("[http] ")
if err != nil {
s.logger.Printf("Error: %s", err.Error())
}
s.logger.Printf("HTTP Request: %s", string(dump))
}

// Send request
resp, err = s.Client.Do(req)

// Log details of this response
if s.Debug {
dump, err := httputil.DumpResponse(resp, true)
if nil != err {
s.logger.Println("Error: ", err.Error())
}
s.logger.Printf("HTTP Response: %s", string(dump))
}

if err != nil {
s.Errors = append(s.Errors, err)
return nil, nil, s.Errors
}
defer resp.Body.Close()

body, _ := ioutil.ReadAll(resp.Body)
// Reset resp.Body so it can be use again
resp.Body = ioutil.NopCloser(bytes.NewBuffer(body))
// deep copy response to give it to both return and callback func
respCallback := *resp
if len(callback) != 0 {
Expand Down
15 changes: 15 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ func TestGet(t *testing.T) {
End()
}

// testing that resp.Body is reusable
func TestResetBody(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Just some text"))
}))

defer ts.Close()

resp, _, _ := New().Get(ts.URL).End()
bodyBytes, _ := ioutil.ReadAll(resp.Body)
if string(bodyBytes) != "Just some text" {
t.Error("Expected to be able to reuse the response body")
}
}

// testing for POST method
func TestPost(t *testing.T) {
const case1_empty = "/"
Expand Down

0 comments on commit 4d93c59

Please sign in to comment.