From de87529387a23d0c6145f6ffc3b5df427ac79c1c Mon Sep 17 00:00:00 2001 From: smallfish Date: Mon, 18 Aug 2014 21:01:49 +0800 Subject: [PATCH 1/2] Update httplib support read data from response buffer, add some testcases --- httplib/httplib.go | 35 ++++++++++++++------- httplib/httplib_test.go | 70 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 90 insertions(+), 15 deletions(-) diff --git a/httplib/httplib.go b/httplib/httplib.go index f641222d..18e4caf3 100644 --- a/httplib/httplib.go +++ b/httplib/httplib.go @@ -16,7 +16,7 @@ // // import "github.com/astaxie/beego/context" // -// b:=httplib.Post("http://beego.me/") +// b := httplib.Post("http://beego.me/") // b.Param("username","astaxie") // b.Param("password","123456") // b.PostFile("uploadfile1", "httplib.pdf") @@ -76,41 +76,46 @@ func SetDefaultSetting(setting BeegoHttpSettings) { // Get returns *BeegoHttpRequest with GET method. func Get(url string) *BeegoHttpRequest { var req http.Request + var resp http.Response req.Method = "GET" req.Header = http.Header{} - return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting} + return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting, &resp, nil} } // Post returns *BeegoHttpRequest with POST method. func Post(url string) *BeegoHttpRequest { var req http.Request + var resp http.Response req.Method = "POST" req.Header = http.Header{} - return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting} + return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting, &resp, nil} } // Put returns *BeegoHttpRequest with PUT method. func Put(url string) *BeegoHttpRequest { var req http.Request + var resp http.Response req.Method = "PUT" req.Header = http.Header{} - return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting} + return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting, &resp, nil} } // Delete returns *BeegoHttpRequest DELETE GET method. func Delete(url string) *BeegoHttpRequest { var req http.Request + var resp http.Response req.Method = "DELETE" req.Header = http.Header{} - return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting} + return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting, &resp, nil} } // Head returns *BeegoHttpRequest with HEAD method. func Head(url string) *BeegoHttpRequest { var req http.Request + var resp http.Response req.Method = "HEAD" req.Header = http.Header{} - return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting} + return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting, &resp, nil} } // BeegoHttpSettings @@ -132,6 +137,8 @@ type BeegoHttpRequest struct { params map[string]string files map[string]string setting BeegoHttpSettings + resp *http.Response + body []byte } // Change request settings @@ -247,6 +254,9 @@ func (b *BeegoHttpRequest) Body(data interface{}) *BeegoHttpRequest { } func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { + if b.resp.StatusCode != 0 { + return b.resp, nil + } var paramBody string if len(b.params) > 0 { var buf bytes.Buffer @@ -365,6 +375,7 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { if err != nil { return nil, err } + b.resp = resp return resp, nil } @@ -382,6 +393,9 @@ func (b *BeegoHttpRequest) String() (string, error) { // Bytes returns the body []byte in response. // it calls Response inner. func (b *BeegoHttpRequest) Bytes() ([]byte, error) { + if b.body != nil { + return b.body, nil + } resp, err := b.getResponse() if err != nil { return nil, err @@ -394,6 +408,7 @@ func (b *BeegoHttpRequest) Bytes() ([]byte, error) { if err != nil { return nil, err } + b.body = data return data, nil } @@ -406,15 +421,11 @@ func (b *BeegoHttpRequest) ToFile(filename string) error { } defer f.Close() - resp, err := b.getResponse() + data, err := b.Bytes() if err != nil { return err } - if resp.Body == nil { - return nil - } - defer resp.Body.Close() - _, err = io.Copy(f, resp.Body) + _, err = f.Write(data) return err } diff --git a/httplib/httplib_test.go b/httplib/httplib_test.go index abf03ace..b352bd4a 100644 --- a/httplib/httplib_test.go +++ b/httplib/httplib_test.go @@ -19,23 +19,41 @@ import ( "testing" ) -func TestSimpleGet(t *testing.T) { - str, err := Get("http://httpbin.org/get").String() +func TestResponse(t *testing.T) { + req := Get("http://httpbin.org/get") + resp, err := req.Response() if err != nil { t.Fatal(err) } - t.Log(str) + t.Log(resp) +} + +func TestGet(t *testing.T) { + req := Get("http://httpbin.org/get") + b, err := req.Bytes() + if err != nil { + t.Fatal(err) + } + t.Log(b) + + s, err := req.String() + if err != nil { + t.Fatal(err) + } + t.Log(s) } func TestSimplePost(t *testing.T) { v := "smallfish" req := Post("http://httpbin.org/post") req.Param("username", v) + str, err := req.String() if err != nil { t.Fatal(err) } t.Log(str) + n := strings.Index(str, v) if n == -1 { t.Fatal(v + " not found in post") @@ -47,17 +65,35 @@ func TestPostFile(t *testing.T) { req := Post("http://httpbin.org/post") req.Param("username", v) req.PostFile("uploadfile", "httplib_test.go") + str, err := req.String() if err != nil { t.Fatal(err) } t.Log(str) + n := strings.Index(str, v) if n == -1 { t.Fatal(v + " not found in post") } } +func TestSimplePut(t *testing.T) { + str, err := Put("http://httpbin.org/put").String() + if err != nil { + t.Fatal(err) + } + t.Log(str) +} + +func TestSimpleDelete(t *testing.T) { + str, err := Delete("http://httpbin.org/delete").String() + if err != nil { + t.Fatal(err) + } + t.Log(str) +} + func TestWithCookie(t *testing.T) { v := "smallfish" str, err := Get("http://httpbin.org/cookies/set?k1=" + v).SetEnableCookie(true).String() @@ -65,11 +101,13 @@ func TestWithCookie(t *testing.T) { t.Fatal(err) } t.Log(str) + str, err = Get("http://httpbin.org/cookies").SetEnableCookie(true).String() if err != nil { t.Fatal(err) } t.Log(str) + n := strings.Index(str, v) if n == -1 { t.Fatal(v + " not found in cookie") @@ -83,6 +121,7 @@ func TestWithUserAgent(t *testing.T) { t.Fatal(err) } t.Log(str) + n := strings.Index(str, v) if n == -1 { t.Fatal(v + " not found in user-agent") @@ -102,8 +141,33 @@ func TestWithSetting(t *testing.T) { t.Fatal(err) } t.Log(str) + n := strings.Index(str, v) if n == -1 { t.Fatal(v + " not found in user-agent") } } + +func TestToJson(t *testing.T) { + req := Get("http://httpbin.org/ip") + resp, err := req.Response() + if err != nil { + t.Fatal(err) + } + t.Log(resp) + + // httpbin will return http remote addr + type Ip struct { + Origin string `json:"origin"` + } + var ip Ip + err = req.ToJson(&ip) + if err != nil { + t.Fatal(err) + } + t.Log(ip.Origin) + + if n := strings.Count(ip.Origin, "."); n != 3 { + t.Fatal("response is not valid ip") + } +} From c16b7be9ac333ce0db2cb755f5e05d4e6d4c823d Mon Sep 17 00:00:00 2001 From: smallfish Date: Mon, 18 Aug 2014 21:29:45 +0800 Subject: [PATCH 2/2] rollback the ToFile func implement, and add testcase --- httplib/httplib.go | 8 ++++++-- httplib/httplib_test.go | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/httplib/httplib.go b/httplib/httplib.go index 18e4caf3..1c0d5544 100644 --- a/httplib/httplib.go +++ b/httplib/httplib.go @@ -421,11 +421,15 @@ func (b *BeegoHttpRequest) ToFile(filename string) error { } defer f.Close() - data, err := b.Bytes() + resp, err := b.getResponse() if err != nil { return err } - _, err = f.Write(data) + if resp.Body == nil { + return nil + } + defer resp.Body.Close() + _, err = io.Copy(f, resp.Body) return err } diff --git a/httplib/httplib_test.go b/httplib/httplib_test.go index b352bd4a..2d49d875 100644 --- a/httplib/httplib_test.go +++ b/httplib/httplib_test.go @@ -15,6 +15,8 @@ package httplib import ( + "io/ioutil" + "os" "strings" "testing" ) @@ -41,6 +43,10 @@ func TestGet(t *testing.T) { t.Fatal(err) } t.Log(s) + + if string(b) != s { + t.Fatal("request data not match") + } } func TestSimplePost(t *testing.T) { @@ -171,3 +177,17 @@ func TestToJson(t *testing.T) { t.Fatal("response is not valid ip") } } + +func TestToFile(t *testing.T) { + f := "beego_testfile" + req := Get("http://httpbin.org/ip") + err := req.ToFile(f) + if err != nil { + t.Fatal(err) + } + defer os.Remove(f) + b, err := ioutil.ReadFile(f) + if n := strings.Index(string(b), "origin"); n == -1 { + t.Fatal(err) + } +}