de875293 by smallfish

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

1 parent c4fa1792
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
16 // 16 //
17 // import "github.com/astaxie/beego/context" 17 // import "github.com/astaxie/beego/context"
18 // 18 //
19 // b:=httplib.Post("http://beego.me/") 19 // b := httplib.Post("http://beego.me/")
20 // b.Param("username","astaxie") 20 // b.Param("username","astaxie")
21 // b.Param("password","123456") 21 // b.Param("password","123456")
22 // b.PostFile("uploadfile1", "httplib.pdf") 22 // b.PostFile("uploadfile1", "httplib.pdf")
...@@ -76,41 +76,46 @@ func SetDefaultSetting(setting BeegoHttpSettings) { ...@@ -76,41 +76,46 @@ func SetDefaultSetting(setting BeegoHttpSettings) {
76 // Get returns *BeegoHttpRequest with GET method. 76 // Get returns *BeegoHttpRequest with GET method.
77 func Get(url string) *BeegoHttpRequest { 77 func Get(url string) *BeegoHttpRequest {
78 var req http.Request 78 var req http.Request
79 var resp http.Response
79 req.Method = "GET" 80 req.Method = "GET"
80 req.Header = http.Header{} 81 req.Header = http.Header{}
81 return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting} 82 return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting, &resp, nil}
82 } 83 }
83 84
84 // Post returns *BeegoHttpRequest with POST method. 85 // Post returns *BeegoHttpRequest with POST method.
85 func Post(url string) *BeegoHttpRequest { 86 func Post(url string) *BeegoHttpRequest {
86 var req http.Request 87 var req http.Request
88 var resp http.Response
87 req.Method = "POST" 89 req.Method = "POST"
88 req.Header = http.Header{} 90 req.Header = http.Header{}
89 return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting} 91 return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting, &resp, nil}
90 } 92 }
91 93
92 // Put returns *BeegoHttpRequest with PUT method. 94 // Put returns *BeegoHttpRequest with PUT method.
93 func Put(url string) *BeegoHttpRequest { 95 func Put(url string) *BeegoHttpRequest {
94 var req http.Request 96 var req http.Request
97 var resp http.Response
95 req.Method = "PUT" 98 req.Method = "PUT"
96 req.Header = http.Header{} 99 req.Header = http.Header{}
97 return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting} 100 return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting, &resp, nil}
98 } 101 }
99 102
100 // Delete returns *BeegoHttpRequest DELETE GET method. 103 // Delete returns *BeegoHttpRequest DELETE GET method.
101 func Delete(url string) *BeegoHttpRequest { 104 func Delete(url string) *BeegoHttpRequest {
102 var req http.Request 105 var req http.Request
106 var resp http.Response
103 req.Method = "DELETE" 107 req.Method = "DELETE"
104 req.Header = http.Header{} 108 req.Header = http.Header{}
105 return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting} 109 return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting, &resp, nil}
106 } 110 }
107 111
108 // Head returns *BeegoHttpRequest with HEAD method. 112 // Head returns *BeegoHttpRequest with HEAD method.
109 func Head(url string) *BeegoHttpRequest { 113 func Head(url string) *BeegoHttpRequest {
110 var req http.Request 114 var req http.Request
115 var resp http.Response
111 req.Method = "HEAD" 116 req.Method = "HEAD"
112 req.Header = http.Header{} 117 req.Header = http.Header{}
113 return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting} 118 return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting, &resp, nil}
114 } 119 }
115 120
116 // BeegoHttpSettings 121 // BeegoHttpSettings
...@@ -132,6 +137,8 @@ type BeegoHttpRequest struct { ...@@ -132,6 +137,8 @@ type BeegoHttpRequest struct {
132 params map[string]string 137 params map[string]string
133 files map[string]string 138 files map[string]string
134 setting BeegoHttpSettings 139 setting BeegoHttpSettings
140 resp *http.Response
141 body []byte
135 } 142 }
136 143
137 // Change request settings 144 // 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 {
247 } 254 }
248 255
249 func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { 256 func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
257 if b.resp.StatusCode != 0 {
258 return b.resp, nil
259 }
250 var paramBody string 260 var paramBody string
251 if len(b.params) > 0 { 261 if len(b.params) > 0 {
252 var buf bytes.Buffer 262 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) {
365 if err != nil { 375 if err != nil {
366 return nil, err 376 return nil, err
367 } 377 }
378 b.resp = resp
368 return resp, nil 379 return resp, nil
369 } 380 }
370 381
...@@ -382,6 +393,9 @@ func (b *BeegoHttpRequest) String() (string, error) { ...@@ -382,6 +393,9 @@ func (b *BeegoHttpRequest) String() (string, error) {
382 // Bytes returns the body []byte in response. 393 // Bytes returns the body []byte in response.
383 // it calls Response inner. 394 // it calls Response inner.
384 func (b *BeegoHttpRequest) Bytes() ([]byte, error) { 395 func (b *BeegoHttpRequest) Bytes() ([]byte, error) {
396 if b.body != nil {
397 return b.body, nil
398 }
385 resp, err := b.getResponse() 399 resp, err := b.getResponse()
386 if err != nil { 400 if err != nil {
387 return nil, err 401 return nil, err
...@@ -394,6 +408,7 @@ func (b *BeegoHttpRequest) Bytes() ([]byte, error) { ...@@ -394,6 +408,7 @@ func (b *BeegoHttpRequest) Bytes() ([]byte, error) {
394 if err != nil { 408 if err != nil {
395 return nil, err 409 return nil, err
396 } 410 }
411 b.body = data
397 return data, nil 412 return data, nil
398 } 413 }
399 414
...@@ -406,15 +421,11 @@ func (b *BeegoHttpRequest) ToFile(filename string) error { ...@@ -406,15 +421,11 @@ func (b *BeegoHttpRequest) ToFile(filename string) error {
406 } 421 }
407 defer f.Close() 422 defer f.Close()
408 423
409 resp, err := b.getResponse() 424 data, err := b.Bytes()
410 if err != nil { 425 if err != nil {
411 return err 426 return err
412 } 427 }
413 if resp.Body == nil { 428 _, err = f.Write(data)
414 return nil
415 }
416 defer resp.Body.Close()
417 _, err = io.Copy(f, resp.Body)
418 return err 429 return err
419 } 430 }
420 431
......
...@@ -19,23 +19,41 @@ import ( ...@@ -19,23 +19,41 @@ import (
19 "testing" 19 "testing"
20 ) 20 )
21 21
22 func TestSimpleGet(t *testing.T) { 22 func TestResponse(t *testing.T) {
23 str, err := Get("http://httpbin.org/get").String() 23 req := Get("http://httpbin.org/get")
24 resp, err := req.Response()
24 if err != nil { 25 if err != nil {
25 t.Fatal(err) 26 t.Fatal(err)
26 } 27 }
27 t.Log(str) 28 t.Log(resp)
29 }
30
31 func TestGet(t *testing.T) {
32 req := Get("http://httpbin.org/get")
33 b, err := req.Bytes()
34 if err != nil {
35 t.Fatal(err)
36 }
37 t.Log(b)
38
39 s, err := req.String()
40 if err != nil {
41 t.Fatal(err)
42 }
43 t.Log(s)
28 } 44 }
29 45
30 func TestSimplePost(t *testing.T) { 46 func TestSimplePost(t *testing.T) {
31 v := "smallfish" 47 v := "smallfish"
32 req := Post("http://httpbin.org/post") 48 req := Post("http://httpbin.org/post")
33 req.Param("username", v) 49 req.Param("username", v)
50
34 str, err := req.String() 51 str, err := req.String()
35 if err != nil { 52 if err != nil {
36 t.Fatal(err) 53 t.Fatal(err)
37 } 54 }
38 t.Log(str) 55 t.Log(str)
56
39 n := strings.Index(str, v) 57 n := strings.Index(str, v)
40 if n == -1 { 58 if n == -1 {
41 t.Fatal(v + " not found in post") 59 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) {
47 req := Post("http://httpbin.org/post") 65 req := Post("http://httpbin.org/post")
48 req.Param("username", v) 66 req.Param("username", v)
49 req.PostFile("uploadfile", "httplib_test.go") 67 req.PostFile("uploadfile", "httplib_test.go")
68
50 str, err := req.String() 69 str, err := req.String()
51 if err != nil { 70 if err != nil {
52 t.Fatal(err) 71 t.Fatal(err)
53 } 72 }
54 t.Log(str) 73 t.Log(str)
74
55 n := strings.Index(str, v) 75 n := strings.Index(str, v)
56 if n == -1 { 76 if n == -1 {
57 t.Fatal(v + " not found in post") 77 t.Fatal(v + " not found in post")
58 } 78 }
59 } 79 }
60 80
81 func TestSimplePut(t *testing.T) {
82 str, err := Put("http://httpbin.org/put").String()
83 if err != nil {
84 t.Fatal(err)
85 }
86 t.Log(str)
87 }
88
89 func TestSimpleDelete(t *testing.T) {
90 str, err := Delete("http://httpbin.org/delete").String()
91 if err != nil {
92 t.Fatal(err)
93 }
94 t.Log(str)
95 }
96
61 func TestWithCookie(t *testing.T) { 97 func TestWithCookie(t *testing.T) {
62 v := "smallfish" 98 v := "smallfish"
63 str, err := Get("http://httpbin.org/cookies/set?k1=" + v).SetEnableCookie(true).String() 99 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) {
65 t.Fatal(err) 101 t.Fatal(err)
66 } 102 }
67 t.Log(str) 103 t.Log(str)
104
68 str, err = Get("http://httpbin.org/cookies").SetEnableCookie(true).String() 105 str, err = Get("http://httpbin.org/cookies").SetEnableCookie(true).String()
69 if err != nil { 106 if err != nil {
70 t.Fatal(err) 107 t.Fatal(err)
71 } 108 }
72 t.Log(str) 109 t.Log(str)
110
73 n := strings.Index(str, v) 111 n := strings.Index(str, v)
74 if n == -1 { 112 if n == -1 {
75 t.Fatal(v + " not found in cookie") 113 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) {
83 t.Fatal(err) 121 t.Fatal(err)
84 } 122 }
85 t.Log(str) 123 t.Log(str)
124
86 n := strings.Index(str, v) 125 n := strings.Index(str, v)
87 if n == -1 { 126 if n == -1 {
88 t.Fatal(v + " not found in user-agent") 127 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) {
102 t.Fatal(err) 141 t.Fatal(err)
103 } 142 }
104 t.Log(str) 143 t.Log(str)
144
105 n := strings.Index(str, v) 145 n := strings.Index(str, v)
106 if n == -1 { 146 if n == -1 {
107 t.Fatal(v + " not found in user-agent") 147 t.Fatal(v + " not found in user-agent")
108 } 148 }
109 } 149 }
150
151 func TestToJson(t *testing.T) {
152 req := Get("http://httpbin.org/ip")
153 resp, err := req.Response()
154 if err != nil {
155 t.Fatal(err)
156 }
157 t.Log(resp)
158
159 // httpbin will return http remote addr
160 type Ip struct {
161 Origin string `json:"origin"`
162 }
163 var ip Ip
164 err = req.ToJson(&ip)
165 if err != nil {
166 t.Fatal(err)
167 }
168 t.Log(ip.Origin)
169
170 if n := strings.Count(ip.Origin, "."); n != 3 {
171 t.Fatal("response is not valid ip")
172 }
173 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!