Commit de875293 authored by smallfish's avatar smallfish

Update httplib support read data from response buffer, add some testcases

parent c4fa1792
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
// //
// import "github.com/astaxie/beego/context" // import "github.com/astaxie/beego/context"
// //
// b:=httplib.Post("http://beego.me/") // b := httplib.Post("http://beego.me/")
// b.Param("username","astaxie") // b.Param("username","astaxie")
// b.Param("password","123456") // b.Param("password","123456")
// b.PostFile("uploadfile1", "httplib.pdf") // b.PostFile("uploadfile1", "httplib.pdf")
...@@ -76,41 +76,46 @@ func SetDefaultSetting(setting BeegoHttpSettings) { ...@@ -76,41 +76,46 @@ func SetDefaultSetting(setting BeegoHttpSettings) {
// Get returns *BeegoHttpRequest with GET method. // Get returns *BeegoHttpRequest with GET method.
func Get(url string) *BeegoHttpRequest { func Get(url string) *BeegoHttpRequest {
var req http.Request var req http.Request
var resp http.Response
req.Method = "GET" req.Method = "GET"
req.Header = http.Header{} 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. // Post returns *BeegoHttpRequest with POST method.
func Post(url string) *BeegoHttpRequest { func Post(url string) *BeegoHttpRequest {
var req http.Request var req http.Request
var resp http.Response
req.Method = "POST" req.Method = "POST"
req.Header = http.Header{} 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. // Put returns *BeegoHttpRequest with PUT method.
func Put(url string) *BeegoHttpRequest { func Put(url string) *BeegoHttpRequest {
var req http.Request var req http.Request
var resp http.Response
req.Method = "PUT" req.Method = "PUT"
req.Header = http.Header{} 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. // Delete returns *BeegoHttpRequest DELETE GET method.
func Delete(url string) *BeegoHttpRequest { func Delete(url string) *BeegoHttpRequest {
var req http.Request var req http.Request
var resp http.Response
req.Method = "DELETE" req.Method = "DELETE"
req.Header = http.Header{} 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. // Head returns *BeegoHttpRequest with HEAD method.
func Head(url string) *BeegoHttpRequest { func Head(url string) *BeegoHttpRequest {
var req http.Request var req http.Request
var resp http.Response
req.Method = "HEAD" req.Method = "HEAD"
req.Header = http.Header{} 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 // BeegoHttpSettings
...@@ -132,6 +137,8 @@ type BeegoHttpRequest struct { ...@@ -132,6 +137,8 @@ type BeegoHttpRequest struct {
params map[string]string params map[string]string
files map[string]string files map[string]string
setting BeegoHttpSettings setting BeegoHttpSettings
resp *http.Response
body []byte
} }
// Change request settings // Change request settings
...@@ -247,6 +254,9 @@ func (b *BeegoHttpRequest) Body(data interface{}) *BeegoHttpRequest { ...@@ -247,6 +254,9 @@ func (b *BeegoHttpRequest) Body(data interface{}) *BeegoHttpRequest {
} }
func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
if b.resp.StatusCode != 0 {
return b.resp, nil
}
var paramBody string var paramBody string
if len(b.params) > 0 { if len(b.params) > 0 {
var buf bytes.Buffer var buf bytes.Buffer
...@@ -365,6 +375,7 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { ...@@ -365,6 +375,7 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
b.resp = resp
return resp, nil return resp, nil
} }
...@@ -382,6 +393,9 @@ func (b *BeegoHttpRequest) String() (string, error) { ...@@ -382,6 +393,9 @@ func (b *BeegoHttpRequest) String() (string, error) {
// Bytes returns the body []byte in response. // Bytes returns the body []byte in response.
// it calls Response inner. // it calls Response inner.
func (b *BeegoHttpRequest) Bytes() ([]byte, error) { func (b *BeegoHttpRequest) Bytes() ([]byte, error) {
if b.body != nil {
return b.body, nil
}
resp, err := b.getResponse() resp, err := b.getResponse()
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -394,6 +408,7 @@ func (b *BeegoHttpRequest) Bytes() ([]byte, error) { ...@@ -394,6 +408,7 @@ func (b *BeegoHttpRequest) Bytes() ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
b.body = data
return data, nil return data, nil
} }
...@@ -406,15 +421,11 @@ func (b *BeegoHttpRequest) ToFile(filename string) error { ...@@ -406,15 +421,11 @@ func (b *BeegoHttpRequest) ToFile(filename string) error {
} }
defer f.Close() defer f.Close()
resp, err := b.getResponse() data, err := b.Bytes()
if err != nil { if err != nil {
return err return err
} }
if resp.Body == nil { _, err = f.Write(data)
return nil
}
defer resp.Body.Close()
_, err = io.Copy(f, resp.Body)
return err return err
} }
......
...@@ -19,23 +19,41 @@ import ( ...@@ -19,23 +19,41 @@ import (
"testing" "testing"
) )
func TestSimpleGet(t *testing.T) { func TestResponse(t *testing.T) {
str, err := Get("http://httpbin.org/get").String() req := Get("http://httpbin.org/get")
resp, err := req.Response()
if err != nil { if err != nil {
t.Fatal(err) 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) { func TestSimplePost(t *testing.T) {
v := "smallfish" v := "smallfish"
req := Post("http://httpbin.org/post") req := Post("http://httpbin.org/post")
req.Param("username", v) req.Param("username", v)
str, err := req.String() str, err := req.String()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
t.Log(str) t.Log(str)
n := strings.Index(str, v) n := strings.Index(str, v)
if n == -1 { if n == -1 {
t.Fatal(v + " not found in post") t.Fatal(v + " not found in post")
...@@ -47,17 +65,35 @@ func TestPostFile(t *testing.T) { ...@@ -47,17 +65,35 @@ func TestPostFile(t *testing.T) {
req := Post("http://httpbin.org/post") req := Post("http://httpbin.org/post")
req.Param("username", v) req.Param("username", v)
req.PostFile("uploadfile", "httplib_test.go") req.PostFile("uploadfile", "httplib_test.go")
str, err := req.String() str, err := req.String()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
t.Log(str) t.Log(str)
n := strings.Index(str, v) n := strings.Index(str, v)
if n == -1 { if n == -1 {
t.Fatal(v + " not found in post") 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) { func TestWithCookie(t *testing.T) {
v := "smallfish" v := "smallfish"
str, err := Get("http://httpbin.org/cookies/set?k1=" + v).SetEnableCookie(true).String() str, err := Get("http://httpbin.org/cookies/set?k1=" + v).SetEnableCookie(true).String()
...@@ -65,11 +101,13 @@ func TestWithCookie(t *testing.T) { ...@@ -65,11 +101,13 @@ func TestWithCookie(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
t.Log(str) t.Log(str)
str, err = Get("http://httpbin.org/cookies").SetEnableCookie(true).String() str, err = Get("http://httpbin.org/cookies").SetEnableCookie(true).String()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
t.Log(str) t.Log(str)
n := strings.Index(str, v) n := strings.Index(str, v)
if n == -1 { if n == -1 {
t.Fatal(v + " not found in cookie") t.Fatal(v + " not found in cookie")
...@@ -83,6 +121,7 @@ func TestWithUserAgent(t *testing.T) { ...@@ -83,6 +121,7 @@ func TestWithUserAgent(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
t.Log(str) t.Log(str)
n := strings.Index(str, v) n := strings.Index(str, v)
if n == -1 { if n == -1 {
t.Fatal(v + " not found in user-agent") t.Fatal(v + " not found in user-agent")
...@@ -102,8 +141,33 @@ func TestWithSetting(t *testing.T) { ...@@ -102,8 +141,33 @@ func TestWithSetting(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
t.Log(str) t.Log(str)
n := strings.Index(str, v) n := strings.Index(str, v)
if n == -1 { if n == -1 {
t.Fatal(v + " not found in user-agent") 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")
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment