Merge pull request #749 from smallfish/master
rename SetAgent and ToXML, and update some testcase
This commit is contained in:
		
						commit
						05089be427
					
				| @ -126,7 +126,7 @@ func (b *BeegoHttpRequest) SetEnableCookie(enable bool) *BeegoHttpRequest { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // SetUserAgent sets User-Agent header field | // SetUserAgent sets User-Agent header field | ||||||
| func (b *BeegoHttpRequest) SetAgent(useragent string) *BeegoHttpRequest { | func (b *BeegoHttpRequest) SetUserAgent(useragent string) *BeegoHttpRequest { | ||||||
| 	b.setting.UserAgent = useragent | 	b.setting.UserAgent = useragent | ||||||
| 	return b | 	return b | ||||||
| } | } | ||||||
| @ -410,7 +410,7 @@ func (b *BeegoHttpRequest) ToJson(v interface{}) error { | |||||||
| 
 | 
 | ||||||
| // ToXml returns the map that marshals from the body bytes as xml in response . | // ToXml returns the map that marshals from the body bytes as xml in response . | ||||||
| // it calls Response inner. | // it calls Response inner. | ||||||
| func (b *BeegoHttpRequest) ToXML(v interface{}) error { | func (b *BeegoHttpRequest) ToXml(v interface{}) error { | ||||||
| 	data, err := b.Bytes() | 	data, err := b.Bytes() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return err | 		return err | ||||||
|  | |||||||
| @ -10,94 +10,95 @@ | |||||||
| package httplib | package httplib | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"fmt" | 	"strings" | ||||||
| 	"io/ioutil" |  | ||||||
| 	"testing" | 	"testing" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func TestGetUrl(t *testing.T) { | func TestSimpleGet(t *testing.T) { | ||||||
| 	resp, err := Get("http://beego.me").Debug(true).Response() | 	str, err := Get("http://httpbin.org/get").String() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Fatal(err) | 		t.Fatal(err) | ||||||
| 	} | 	} | ||||||
| 	if resp.Body == nil { | 	t.Log(str) | ||||||
| 		t.Fatal("body is nil") | } | ||||||
| 	} |  | ||||||
| 	data, err := ioutil.ReadAll(resp.Body) |  | ||||||
| 	defer resp.Body.Close() |  | ||||||
| 	if err != nil { |  | ||||||
| 		t.Fatal(err) |  | ||||||
| 	} |  | ||||||
| 	if len(data) == 0 { |  | ||||||
| 		t.Fatal("data is no") |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	str, err := Get("http://beego.me").String() | func TestSimplePost(t *testing.T) { | ||||||
|  | 	v := "smallfish" | ||||||
|  | 	req := Post("http://httpbin.org/post") | ||||||
|  | 	req.Param("username", v) | ||||||
|  | 	str, err := req.String() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Fatal(err) | 		t.Fatal(err) | ||||||
| 	} | 	} | ||||||
| 	if len(str) == 0 { | 	t.Log(str) | ||||||
| 		t.Fatal("has no info") | 	n := strings.Index(str, v) | ||||||
|  | 	if n == -1 { | ||||||
|  | 		t.Fatal(v + " not found in post") | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func ExamplePost(t *testing.T) { | func TestPostFile(t *testing.T) { | ||||||
| 	b := Post("http://beego.me/").Debug(true) | 	v := "smallfish" | ||||||
| 	b.Param("username", "astaxie") | 	req := Post("http://httpbin.org/post") | ||||||
| 	b.Param("password", "hello") | 	req.Param("username", v) | ||||||
| 	b.PostFile("uploadfile", "httplib_test.go") | 	req.PostFile("uploadfile", "httplib_test.go") | ||||||
| 	str, err := b.String() | 	str, err := req.String() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Fatal(err) | 		t.Fatal(err) | ||||||
| 	} | 	} | ||||||
| 	fmt.Println(str) | 	t.Log(str) | ||||||
|  | 	n := strings.Index(str, v) | ||||||
|  | 	if n == -1 { | ||||||
|  | 		t.Fatal(v + " not found in post") | ||||||
|  | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestSimpleGetString(t *testing.T) { | func TestWithCookie(t *testing.T) { | ||||||
| 	fmt.Println("TestSimpleGetString==========================================") | 	v := "smallfish" | ||||||
| 	html, err := Get("http://httpbin.org/headers").SetAgent("beegoooooo").String() | 	str, err := Get("http://httpbin.org/cookies/set?k1=" + v).SetEnableCookie(true).String() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Fatal(err) | 		t.Fatal(err) | ||||||
| 	} | 	} | ||||||
| 	fmt.Println(html) | 	t.Log(str) | ||||||
| 	fmt.Println("TestSimpleGetString==========================================") | 	str, err = Get("http://httpbin.org/cookies").SetEnableCookie(true).String() | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Fatal(err) | ||||||
|  | 	} | ||||||
|  | 	t.Log(str) | ||||||
|  | 	n := strings.Index(str, v) | ||||||
|  | 	if n == -1 { | ||||||
|  | 		t.Fatal(v + " not found in cookie") | ||||||
|  | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestSimpleGetStringWithDefaultCookie(t *testing.T) { | func TestWithUserAgent(t *testing.T) { | ||||||
| 	fmt.Println("TestSimpleGetStringWithDefaultCookie==========================================") | 	v := "beego" | ||||||
| 	html, err := Get("http://httpbin.org/cookies/set?k1=v1").SetEnableCookie(true).String() | 	str, err := Get("http://httpbin.org/headers").SetUserAgent(v).String() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Fatal(err) | 		t.Fatal(err) | ||||||
| 	} | 	} | ||||||
| 	fmt.Println(html) | 	t.Log(str) | ||||||
| 	html, err = Get("http://httpbin.org/cookies").SetEnableCookie(true).String() | 	n := strings.Index(str, v) | ||||||
| 	if err != nil { | 	if n == -1 { | ||||||
| 		t.Fatal(err) | 		t.Fatal(v + " not found in user-agent") | ||||||
| 	} | 	} | ||||||
| 	fmt.Println(html) |  | ||||||
| 	fmt.Println("TestSimpleGetStringWithDefaultCookie==========================================") |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestDefaultSetting(t *testing.T) { | func TestWithSetting(t *testing.T) { | ||||||
| 	fmt.Println("TestDefaultSetting==========================================") | 	v := "beego" | ||||||
| 	var def BeegoHttpSettings | 	var setting BeegoHttpSettings | ||||||
| 	def.EnableCookie = true | 	setting.EnableCookie = true | ||||||
| 	//def.ShowDebug = true | 	setting.UserAgent = v | ||||||
| 	def.UserAgent = "UserAgent" | 	setting.Transport = nil | ||||||
| 	//def.ConnectTimeout = 60*time.Second | 	SetDefaultSetting(setting) | ||||||
| 	//def.ReadWriteTimeout = 60*time.Second |  | ||||||
| 	def.Transport = nil //http.DefaultTransport |  | ||||||
| 	SetDefaultSetting(def) |  | ||||||
| 
 | 
 | ||||||
| 	html, err := Get("http://httpbin.org/headers").String() | 	str, err := Get("http://httpbin.org/get").String() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Fatal(err) | 		t.Fatal(err) | ||||||
| 	} | 	} | ||||||
| 	fmt.Println(html) | 	t.Log(str) | ||||||
| 	html, err = Get("http://httpbin.org/headers").String() | 	n := strings.Index(str, v) | ||||||
| 	if err != nil { | 	if n == -1 { | ||||||
| 		t.Fatal(err) | 		t.Fatal(v + " not found in user-agent") | ||||||
| 	} | 	} | ||||||
| 	fmt.Println(html) |  | ||||||
| 	fmt.Println("TestDefaultSetting==========================================") |  | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user