Merge pull request #750 from smallfish/develop
Update httplib support read data from response buffer, add some testcase...
Showing
2 changed files
with
108 additions
and
9 deletions
| ... | @@ -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 | ... | ... |
| ... | @@ -15,27 +15,51 @@ | ... | @@ -15,27 +15,51 @@ |
| 15 | package httplib | 15 | package httplib |
| 16 | 16 | ||
| 17 | import ( | 17 | import ( |
| 18 | "io/ioutil" | ||
| 19 | "os" | ||
| 18 | "strings" | 20 | "strings" |
| 19 | "testing" | 21 | "testing" |
| 20 | ) | 22 | ) |
| 21 | 23 | ||
| 22 | func TestSimpleGet(t *testing.T) { | 24 | func TestResponse(t *testing.T) { |
| 23 | str, err := Get("http://httpbin.org/get").String() | 25 | req := Get("http://httpbin.org/get") |
| 26 | resp, err := req.Response() | ||
| 24 | if err != nil { | 27 | if err != nil { |
| 25 | t.Fatal(err) | 28 | t.Fatal(err) |
| 26 | } | 29 | } |
| 27 | t.Log(str) | 30 | t.Log(resp) |
| 31 | } | ||
| 32 | |||
| 33 | func TestGet(t *testing.T) { | ||
| 34 | req := Get("http://httpbin.org/get") | ||
| 35 | b, err := req.Bytes() | ||
| 36 | if err != nil { | ||
| 37 | t.Fatal(err) | ||
| 38 | } | ||
| 39 | t.Log(b) | ||
| 40 | |||
| 41 | s, err := req.String() | ||
| 42 | if err != nil { | ||
| 43 | t.Fatal(err) | ||
| 44 | } | ||
| 45 | t.Log(s) | ||
| 46 | |||
| 47 | if string(b) != s { | ||
| 48 | t.Fatal("request data not match") | ||
| 49 | } | ||
| 28 | } | 50 | } |
| 29 | 51 | ||
| 30 | func TestSimplePost(t *testing.T) { | 52 | func TestSimplePost(t *testing.T) { |
| 31 | v := "smallfish" | 53 | v := "smallfish" |
| 32 | req := Post("http://httpbin.org/post") | 54 | req := Post("http://httpbin.org/post") |
| 33 | req.Param("username", v) | 55 | req.Param("username", v) |
| 56 | |||
| 34 | str, err := req.String() | 57 | str, err := req.String() |
| 35 | if err != nil { | 58 | if err != nil { |
| 36 | t.Fatal(err) | 59 | t.Fatal(err) |
| 37 | } | 60 | } |
| 38 | t.Log(str) | 61 | t.Log(str) |
| 62 | |||
| 39 | n := strings.Index(str, v) | 63 | n := strings.Index(str, v) |
| 40 | if n == -1 { | 64 | if n == -1 { |
| 41 | t.Fatal(v + " not found in post") | 65 | t.Fatal(v + " not found in post") |
| ... | @@ -47,17 +71,35 @@ func TestPostFile(t *testing.T) { | ... | @@ -47,17 +71,35 @@ func TestPostFile(t *testing.T) { |
| 47 | req := Post("http://httpbin.org/post") | 71 | req := Post("http://httpbin.org/post") |
| 48 | req.Param("username", v) | 72 | req.Param("username", v) |
| 49 | req.PostFile("uploadfile", "httplib_test.go") | 73 | req.PostFile("uploadfile", "httplib_test.go") |
| 74 | |||
| 50 | str, err := req.String() | 75 | str, err := req.String() |
| 51 | if err != nil { | 76 | if err != nil { |
| 52 | t.Fatal(err) | 77 | t.Fatal(err) |
| 53 | } | 78 | } |
| 54 | t.Log(str) | 79 | t.Log(str) |
| 80 | |||
| 55 | n := strings.Index(str, v) | 81 | n := strings.Index(str, v) |
| 56 | if n == -1 { | 82 | if n == -1 { |
| 57 | t.Fatal(v + " not found in post") | 83 | t.Fatal(v + " not found in post") |
| 58 | } | 84 | } |
| 59 | } | 85 | } |
| 60 | 86 | ||
| 87 | func TestSimplePut(t *testing.T) { | ||
| 88 | str, err := Put("http://httpbin.org/put").String() | ||
| 89 | if err != nil { | ||
| 90 | t.Fatal(err) | ||
| 91 | } | ||
| 92 | t.Log(str) | ||
| 93 | } | ||
| 94 | |||
| 95 | func TestSimpleDelete(t *testing.T) { | ||
| 96 | str, err := Delete("http://httpbin.org/delete").String() | ||
| 97 | if err != nil { | ||
| 98 | t.Fatal(err) | ||
| 99 | } | ||
| 100 | t.Log(str) | ||
| 101 | } | ||
| 102 | |||
| 61 | func TestWithCookie(t *testing.T) { | 103 | func TestWithCookie(t *testing.T) { |
| 62 | v := "smallfish" | 104 | v := "smallfish" |
| 63 | str, err := Get("http://httpbin.org/cookies/set?k1=" + v).SetEnableCookie(true).String() | 105 | str, err := Get("http://httpbin.org/cookies/set?k1=" + v).SetEnableCookie(true).String() |
| ... | @@ -65,11 +107,13 @@ func TestWithCookie(t *testing.T) { | ... | @@ -65,11 +107,13 @@ func TestWithCookie(t *testing.T) { |
| 65 | t.Fatal(err) | 107 | t.Fatal(err) |
| 66 | } | 108 | } |
| 67 | t.Log(str) | 109 | t.Log(str) |
| 110 | |||
| 68 | str, err = Get("http://httpbin.org/cookies").SetEnableCookie(true).String() | 111 | str, err = Get("http://httpbin.org/cookies").SetEnableCookie(true).String() |
| 69 | if err != nil { | 112 | if err != nil { |
| 70 | t.Fatal(err) | 113 | t.Fatal(err) |
| 71 | } | 114 | } |
| 72 | t.Log(str) | 115 | t.Log(str) |
| 116 | |||
| 73 | n := strings.Index(str, v) | 117 | n := strings.Index(str, v) |
| 74 | if n == -1 { | 118 | if n == -1 { |
| 75 | t.Fatal(v + " not found in cookie") | 119 | t.Fatal(v + " not found in cookie") |
| ... | @@ -83,6 +127,7 @@ func TestWithUserAgent(t *testing.T) { | ... | @@ -83,6 +127,7 @@ func TestWithUserAgent(t *testing.T) { |
| 83 | t.Fatal(err) | 127 | t.Fatal(err) |
| 84 | } | 128 | } |
| 85 | t.Log(str) | 129 | t.Log(str) |
| 130 | |||
| 86 | n := strings.Index(str, v) | 131 | n := strings.Index(str, v) |
| 87 | if n == -1 { | 132 | if n == -1 { |
| 88 | t.Fatal(v + " not found in user-agent") | 133 | t.Fatal(v + " not found in user-agent") |
| ... | @@ -102,8 +147,47 @@ func TestWithSetting(t *testing.T) { | ... | @@ -102,8 +147,47 @@ func TestWithSetting(t *testing.T) { |
| 102 | t.Fatal(err) | 147 | t.Fatal(err) |
| 103 | } | 148 | } |
| 104 | t.Log(str) | 149 | t.Log(str) |
| 150 | |||
| 105 | n := strings.Index(str, v) | 151 | n := strings.Index(str, v) |
| 106 | if n == -1 { | 152 | if n == -1 { |
| 107 | t.Fatal(v + " not found in user-agent") | 153 | t.Fatal(v + " not found in user-agent") |
| 108 | } | 154 | } |
| 109 | } | 155 | } |
| 156 | |||
| 157 | func TestToJson(t *testing.T) { | ||
| 158 | req := Get("http://httpbin.org/ip") | ||
| 159 | resp, err := req.Response() | ||
| 160 | if err != nil { | ||
| 161 | t.Fatal(err) | ||
| 162 | } | ||
| 163 | t.Log(resp) | ||
| 164 | |||
| 165 | // httpbin will return http remote addr | ||
| 166 | type Ip struct { | ||
| 167 | Origin string `json:"origin"` | ||
| 168 | } | ||
| 169 | var ip Ip | ||
| 170 | err = req.ToJson(&ip) | ||
| 171 | if err != nil { | ||
| 172 | t.Fatal(err) | ||
| 173 | } | ||
| 174 | t.Log(ip.Origin) | ||
| 175 | |||
| 176 | if n := strings.Count(ip.Origin, "."); n != 3 { | ||
| 177 | t.Fatal("response is not valid ip") | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | func TestToFile(t *testing.T) { | ||
| 182 | f := "beego_testfile" | ||
| 183 | req := Get("http://httpbin.org/ip") | ||
| 184 | err := req.ToFile(f) | ||
| 185 | if err != nil { | ||
| 186 | t.Fatal(err) | ||
| 187 | } | ||
| 188 | defer os.Remove(f) | ||
| 189 | b, err := ioutil.ReadFile(f) | ||
| 190 | if n := strings.Index(string(b), "origin"); n == -1 { | ||
| 191 | t.Fatal(err) | ||
| 192 | } | ||
| 193 | } | ... | ... |
-
Please register or sign in to post a comment