rollback the ToFile func implement, and add testcase
Showing
2 changed files
with
26 additions
and
2 deletions
| ... | @@ -421,11 +421,15 @@ func (b *BeegoHttpRequest) ToFile(filename string) error { | ... | @@ -421,11 +421,15 @@ func (b *BeegoHttpRequest) ToFile(filename string) error { |
| 421 | } | 421 | } |
| 422 | defer f.Close() | 422 | defer f.Close() |
| 423 | 423 | ||
| 424 | data, err := b.Bytes() | 424 | resp, err := b.getResponse() |
| 425 | if err != nil { | 425 | if err != nil { |
| 426 | return err | 426 | return err |
| 427 | } | 427 | } |
| 428 | _, err = f.Write(data) | 428 | if resp.Body == nil { |
| 429 | return nil | ||
| 430 | } | ||
| 431 | defer resp.Body.Close() | ||
| 432 | _, err = io.Copy(f, resp.Body) | ||
| 429 | return err | 433 | return err |
| 430 | } | 434 | } |
| 431 | 435 | ... | ... |
| ... | @@ -15,6 +15,8 @@ | ... | @@ -15,6 +15,8 @@ |
| 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 | ) |
| ... | @@ -41,6 +43,10 @@ func TestGet(t *testing.T) { | ... | @@ -41,6 +43,10 @@ func TestGet(t *testing.T) { |
| 41 | t.Fatal(err) | 43 | t.Fatal(err) |
| 42 | } | 44 | } |
| 43 | t.Log(s) | 45 | t.Log(s) |
| 46 | |||
| 47 | if string(b) != s { | ||
| 48 | t.Fatal("request data not match") | ||
| 49 | } | ||
| 44 | } | 50 | } |
| 45 | 51 | ||
| 46 | func TestSimplePost(t *testing.T) { | 52 | func TestSimplePost(t *testing.T) { |
| ... | @@ -171,3 +177,17 @@ func TestToJson(t *testing.T) { | ... | @@ -171,3 +177,17 @@ func TestToJson(t *testing.T) { |
| 171 | t.Fatal("response is not valid ip") | 177 | t.Fatal("response is not valid ip") |
| 172 | } | 178 | } |
| 173 | } | 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