2014-06-25 02:39:37 +00:00
|
|
|
// Beego (http://beego.me/)
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-04-12 05:18:18 +00:00
|
|
|
// @description beego is an open-source, high-performance web framework for the Go programming language.
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-04-12 05:18:18 +00:00
|
|
|
// @link http://github.com/astaxie/beego for the canonical source repository
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-04-12 05:18:18 +00:00
|
|
|
// @license http://github.com/astaxie/beego/blob/master/LICENSE
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-04-12 05:18:18 +00:00
|
|
|
// @authors astaxie
|
2013-08-06 08:13:45 +00:00
|
|
|
package httplib
|
2013-08-03 14:20:09 +00:00
|
|
|
|
|
|
|
import (
|
2014-05-08 08:58:08 +00:00
|
|
|
"fmt"
|
2013-08-03 14:20:09 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetUrl(t *testing.T) {
|
2014-06-04 13:04:50 +00:00
|
|
|
resp, err := Get("http://beego.me").Debug(true).Response()
|
2013-08-03 14:20:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if resp.Body == nil {
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2014-06-04 13:04:50 +00:00
|
|
|
str, err := Get("http://beego.me").String()
|
2013-08-03 14:20:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(str) == 0 {
|
|
|
|
t.Fatal("has no info")
|
|
|
|
}
|
|
|
|
}
|
2014-05-08 08:58:08 +00:00
|
|
|
|
2014-05-20 09:34:52 +00:00
|
|
|
func ExamplePost(t *testing.T) {
|
2014-05-08 08:58:08 +00:00
|
|
|
b := Post("http://beego.me/").Debug(true)
|
|
|
|
b.Param("username", "astaxie")
|
|
|
|
b.Param("password", "hello")
|
2014-06-04 14:12:37 +00:00
|
|
|
b.PostFile("uploadfile", "httplib_test.go")
|
2014-05-08 08:58:08 +00:00
|
|
|
str, err := b.String()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fmt.Println(str)
|
|
|
|
}
|
2014-06-03 13:20:10 +00:00
|
|
|
|
|
|
|
func TestSimpleGetString(t *testing.T) {
|
|
|
|
fmt.Println("TestSimpleGetString==========================================")
|
2014-06-04 13:04:50 +00:00
|
|
|
html, err := Get("http://httpbin.org/headers").SetAgent("beegoooooo").String()
|
2014-06-03 13:20:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fmt.Println(html)
|
|
|
|
fmt.Println("TestSimpleGetString==========================================")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSimpleGetStringWithDefaultCookie(t *testing.T) {
|
|
|
|
fmt.Println("TestSimpleGetStringWithDefaultCookie==========================================")
|
2014-06-04 13:04:50 +00:00
|
|
|
html, err := Get("http://httpbin.org/cookies/set?k1=v1").SetEnableCookie(true).String()
|
2014-06-03 13:20:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fmt.Println(html)
|
2014-06-04 13:04:50 +00:00
|
|
|
html, err = Get("http://httpbin.org/cookies").SetEnableCookie(true).String()
|
2014-06-03 13:20:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fmt.Println(html)
|
|
|
|
fmt.Println("TestSimpleGetStringWithDefaultCookie==========================================")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultSetting(t *testing.T) {
|
|
|
|
fmt.Println("TestDefaultSetting==========================================")
|
|
|
|
var def BeegoHttpSettings
|
|
|
|
def.EnableCookie = true
|
|
|
|
//def.ShowDebug = true
|
|
|
|
def.UserAgent = "UserAgent"
|
|
|
|
//def.ConnectTimeout = 60*time.Second
|
|
|
|
//def.ReadWriteTimeout = 60*time.Second
|
2014-06-04 13:04:50 +00:00
|
|
|
def.Transport = nil //http.DefaultTransport
|
2014-06-03 13:20:10 +00:00
|
|
|
SetDefaultSetting(def)
|
|
|
|
|
2014-06-04 13:04:50 +00:00
|
|
|
html, err := Get("http://httpbin.org/headers").String()
|
2014-06-03 13:20:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fmt.Println(html)
|
2014-06-04 13:04:50 +00:00
|
|
|
html, err = Get("http://httpbin.org/headers").String()
|
2014-06-03 13:20:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fmt.Println(html)
|
|
|
|
fmt.Println("TestDefaultSetting==========================================")
|
|
|
|
}
|