1
0
mirror of https://github.com/astaxie/beego.git synced 2024-06-28 06:04:13 +00:00
Beego/httplib/httplib_test.go

106 lines
2.1 KiB
Go
Raw Normal View History

2014-06-25 02:39:37 +00:00
// Beego (http://beego.me/)
2014-04-12 05:18:18 +00:00
// @description beego is an open-source, high-performance web framework for the Go programming language.
2014-06-25 02:39:37 +00:00
2014-04-12 05:18:18 +00:00
// @link http://github.com/astaxie/beego for the canonical source repository
2014-06-25 02:39:37 +00:00
2014-04-12 05:18:18 +00:00
// @license http://github.com/astaxie/beego/blob/master/LICENSE
2014-06-25 02:39:37 +00:00
2014-04-12 05:18:18 +00:00
// @authors astaxie
package httplib
2013-08-03 14:20:09 +00:00
import (
2014-08-18 07:03:34 +00:00
"strings"
2013-08-03 14:20:09 +00:00
"testing"
)
2014-08-18 07:03:34 +00:00
func TestSimpleGet(t *testing.T) {
str, err := Get("http://httpbin.org/get").String()
2013-08-03 14:20:09 +00:00
if err != nil {
t.Fatal(err)
}
2014-08-18 07:03:34 +00:00
t.Log(str)
}
func TestSimplePost(t *testing.T) {
v := "smallfish"
req := Post("http://httpbin.org/post")
req.Param("username", v)
str, err := req.String()
2013-08-03 14:20:09 +00:00
if err != nil {
t.Fatal(err)
}
2014-08-18 07:03:34 +00:00
t.Log(str)
n := strings.Index(str, v)
if n == -1 {
t.Fatal(v + " not found in post")
2013-08-03 14:20:09 +00:00
}
2014-08-18 07:03:34 +00:00
}
2013-08-03 14:20:09 +00:00
2014-08-18 07:03:34 +00:00
func TestPostFile(t *testing.T) {
v := "smallfish"
req := Post("http://httpbin.org/post")
req.Param("username", v)
req.PostFile("uploadfile", "httplib_test.go")
str, err := req.String()
2013-08-03 14:20:09 +00:00
if err != nil {
t.Fatal(err)
}
2014-08-18 07:03:34 +00:00
t.Log(str)
n := strings.Index(str, v)
if n == -1 {
t.Fatal(v + " not found in post")
2013-08-03 14:20:09 +00:00
}
}
2014-05-08 08:58:08 +00:00
2014-08-18 07:03:34 +00:00
func TestWithCookie(t *testing.T) {
v := "smallfish"
str, err := Get("http://httpbin.org/cookies/set?k1=" + v).SetEnableCookie(true).String()
2014-05-08 08:58:08 +00:00
if err != nil {
t.Fatal(err)
}
2014-08-18 07:03:34 +00:00
t.Log(str)
str, err = Get("http://httpbin.org/cookies").SetEnableCookie(true).String()
if err != nil {
t.Fatal(err)
}
2014-08-18 07:03:34 +00:00
t.Log(str)
n := strings.Index(str, v)
if n == -1 {
t.Fatal(v + " not found in cookie")
}
}
2014-08-18 07:03:34 +00:00
func TestWithUserAgent(t *testing.T) {
v := "beego"
str, err := Get("http://httpbin.org/headers").SetUserAgent(v).String()
if err != nil {
t.Fatal(err)
}
2014-08-18 07:03:34 +00:00
t.Log(str)
n := strings.Index(str, v)
if n == -1 {
t.Fatal(v + " not found in user-agent")
}
}
2014-08-18 07:03:34 +00:00
func TestWithSetting(t *testing.T) {
v := "beego"
var setting BeegoHttpSettings
setting.EnableCookie = true
setting.UserAgent = v
setting.Transport = nil
SetDefaultSetting(setting)
2014-08-18 07:03:34 +00:00
str, err := Get("http://httpbin.org/get").String()
if err != nil {
t.Fatal(err)
}
2014-08-18 07:03:34 +00:00
t.Log(str)
n := strings.Index(str, v)
if n == -1 {
t.Fatal(v + " not found in user-agent")
}
}