Merge pull request #749 from smallfish/master
rename SetAgent and ToXML, and update some testcase
Showing
2 changed files
with
57 additions
and
56 deletions
| ... | @@ -126,7 +126,7 @@ func (b *BeegoHttpRequest) SetEnableCookie(enable bool) *BeegoHttpRequest { | ... | @@ -126,7 +126,7 @@ func (b *BeegoHttpRequest) SetEnableCookie(enable bool) *BeegoHttpRequest { |
| 126 | } | 126 | } |
| 127 | 127 | ||
| 128 | // SetUserAgent sets User-Agent header field | 128 | // SetUserAgent sets User-Agent header field |
| 129 | func (b *BeegoHttpRequest) SetAgent(useragent string) *BeegoHttpRequest { | 129 | func (b *BeegoHttpRequest) SetUserAgent(useragent string) *BeegoHttpRequest { |
| 130 | b.setting.UserAgent = useragent | 130 | b.setting.UserAgent = useragent |
| 131 | return b | 131 | return b |
| 132 | } | 132 | } |
| ... | @@ -410,7 +410,7 @@ func (b *BeegoHttpRequest) ToJson(v interface{}) error { | ... | @@ -410,7 +410,7 @@ func (b *BeegoHttpRequest) ToJson(v interface{}) error { |
| 410 | 410 | ||
| 411 | // ToXml returns the map that marshals from the body bytes as xml in response . | 411 | // ToXml returns the map that marshals from the body bytes as xml in response . |
| 412 | // it calls Response inner. | 412 | // it calls Response inner. |
| 413 | func (b *BeegoHttpRequest) ToXML(v interface{}) error { | 413 | func (b *BeegoHttpRequest) ToXml(v interface{}) error { |
| 414 | data, err := b.Bytes() | 414 | data, err := b.Bytes() |
| 415 | if err != nil { | 415 | if err != nil { |
| 416 | return err | 416 | return err | ... | ... |
| ... | @@ -10,94 +10,95 @@ | ... | @@ -10,94 +10,95 @@ |
| 10 | package httplib | 10 | package httplib |
| 11 | 11 | ||
| 12 | import ( | 12 | import ( |
| 13 | "fmt" | 13 | "strings" |
| 14 | "io/ioutil" | ||
| 15 | "testing" | 14 | "testing" |
| 16 | ) | 15 | ) |
| 17 | 16 | ||
| 18 | func TestGetUrl(t *testing.T) { | 17 | func TestSimpleGet(t *testing.T) { |
| 19 | resp, err := Get("http://beego.me").Debug(true).Response() | 18 | str, err := Get("http://httpbin.org/get").String() |
| 20 | if err != nil { | 19 | if err != nil { |
| 21 | t.Fatal(err) | 20 | t.Fatal(err) |
| 22 | } | 21 | } |
| 23 | if resp.Body == nil { | 22 | t.Log(str) |
| 24 | t.Fatal("body is nil") | 23 | } |
| 25 | } | 24 | |
| 26 | data, err := ioutil.ReadAll(resp.Body) | 25 | func TestSimplePost(t *testing.T) { |
| 27 | defer resp.Body.Close() | 26 | v := "smallfish" |
| 27 | req := Post("http://httpbin.org/post") | ||
| 28 | req.Param("username", v) | ||
| 29 | str, err := req.String() | ||
| 28 | if err != nil { | 30 | if err != nil { |
| 29 | t.Fatal(err) | 31 | t.Fatal(err) |
| 30 | } | 32 | } |
| 31 | if len(data) == 0 { | 33 | t.Log(str) |
| 32 | t.Fatal("data is no") | 34 | n := strings.Index(str, v) |
| 35 | if n == -1 { | ||
| 36 | t.Fatal(v + " not found in post") | ||
| 33 | } | 37 | } |
| 38 | } | ||
| 34 | 39 | ||
| 35 | str, err := Get("http://beego.me").String() | 40 | func TestPostFile(t *testing.T) { |
| 41 | v := "smallfish" | ||
| 42 | req := Post("http://httpbin.org/post") | ||
| 43 | req.Param("username", v) | ||
| 44 | req.PostFile("uploadfile", "httplib_test.go") | ||
| 45 | str, err := req.String() | ||
| 36 | if err != nil { | 46 | if err != nil { |
| 37 | t.Fatal(err) | 47 | t.Fatal(err) |
| 38 | } | 48 | } |
| 39 | if len(str) == 0 { | 49 | t.Log(str) |
| 40 | t.Fatal("has no info") | 50 | n := strings.Index(str, v) |
| 51 | if n == -1 { | ||
| 52 | t.Fatal(v + " not found in post") | ||
| 41 | } | 53 | } |
| 42 | } | 54 | } |
| 43 | 55 | ||
| 44 | func ExamplePost(t *testing.T) { | 56 | func TestWithCookie(t *testing.T) { |
| 45 | b := Post("http://beego.me/").Debug(true) | 57 | v := "smallfish" |
| 46 | b.Param("username", "astaxie") | 58 | str, err := Get("http://httpbin.org/cookies/set?k1=" + v).SetEnableCookie(true).String() |
| 47 | b.Param("password", "hello") | ||
| 48 | b.PostFile("uploadfile", "httplib_test.go") | ||
| 49 | str, err := b.String() | ||
| 50 | if err != nil { | 59 | if err != nil { |
| 51 | t.Fatal(err) | 60 | t.Fatal(err) |
| 52 | } | 61 | } |
| 53 | fmt.Println(str) | 62 | t.Log(str) |
| 54 | } | 63 | str, err = Get("http://httpbin.org/cookies").SetEnableCookie(true).String() |
| 55 | |||
| 56 | func TestSimpleGetString(t *testing.T) { | ||
| 57 | fmt.Println("TestSimpleGetString==========================================") | ||
| 58 | html, err := Get("http://httpbin.org/headers").SetAgent("beegoooooo").String() | ||
| 59 | if err != nil { | 64 | if err != nil { |
| 60 | t.Fatal(err) | 65 | t.Fatal(err) |
| 61 | } | 66 | } |
| 62 | fmt.Println(html) | 67 | t.Log(str) |
| 63 | fmt.Println("TestSimpleGetString==========================================") | 68 | n := strings.Index(str, v) |
| 69 | if n == -1 { | ||
| 70 | t.Fatal(v + " not found in cookie") | ||
| 71 | } | ||
| 64 | } | 72 | } |
| 65 | 73 | ||
| 66 | func TestSimpleGetStringWithDefaultCookie(t *testing.T) { | 74 | func TestWithUserAgent(t *testing.T) { |
| 67 | fmt.Println("TestSimpleGetStringWithDefaultCookie==========================================") | 75 | v := "beego" |
| 68 | html, err := Get("http://httpbin.org/cookies/set?k1=v1").SetEnableCookie(true).String() | 76 | str, err := Get("http://httpbin.org/headers").SetUserAgent(v).String() |
| 69 | if err != nil { | 77 | if err != nil { |
| 70 | t.Fatal(err) | 78 | t.Fatal(err) |
| 71 | } | 79 | } |
| 72 | fmt.Println(html) | 80 | t.Log(str) |
| 73 | html, err = Get("http://httpbin.org/cookies").SetEnableCookie(true).String() | 81 | n := strings.Index(str, v) |
| 74 | if err != nil { | 82 | if n == -1 { |
| 75 | t.Fatal(err) | 83 | t.Fatal(v + " not found in user-agent") |
| 76 | } | 84 | } |
| 77 | fmt.Println(html) | ||
| 78 | fmt.Println("TestSimpleGetStringWithDefaultCookie==========================================") | ||
| 79 | } | 85 | } |
| 80 | 86 | ||
| 81 | func TestDefaultSetting(t *testing.T) { | 87 | func TestWithSetting(t *testing.T) { |
| 82 | fmt.Println("TestDefaultSetting==========================================") | 88 | v := "beego" |
| 83 | var def BeegoHttpSettings | 89 | var setting BeegoHttpSettings |
| 84 | def.EnableCookie = true | 90 | setting.EnableCookie = true |
| 85 | //def.ShowDebug = true | 91 | setting.UserAgent = v |
| 86 | def.UserAgent = "UserAgent" | 92 | setting.Transport = nil |
| 87 | //def.ConnectTimeout = 60*time.Second | 93 | SetDefaultSetting(setting) |
| 88 | //def.ReadWriteTimeout = 60*time.Second | ||
| 89 | def.Transport = nil //http.DefaultTransport | ||
| 90 | SetDefaultSetting(def) | ||
| 91 | 94 | ||
| 92 | html, err := Get("http://httpbin.org/headers").String() | 95 | str, err := Get("http://httpbin.org/get").String() |
| 93 | if err != nil { | 96 | if err != nil { |
| 94 | t.Fatal(err) | 97 | t.Fatal(err) |
| 95 | } | 98 | } |
| 96 | fmt.Println(html) | 99 | t.Log(str) |
| 97 | html, err = Get("http://httpbin.org/headers").String() | 100 | n := strings.Index(str, v) |
| 98 | if err != nil { | 101 | if n == -1 { |
| 99 | t.Fatal(err) | 102 | t.Fatal(v + " not found in user-agent") |
| 100 | } | 103 | } |
| 101 | fmt.Println(html) | ||
| 102 | fmt.Println("TestDefaultSetting==========================================") | ||
| 103 | } | 104 | } | ... | ... |
-
Please register or sign in to post a comment