1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-10 21:40:54 +00:00
Beego/httplib/httplib_test.go

219 lines
4.3 KiB
Go
Raw Normal View History

2014-08-18 08:41:43 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package httplib
2013-08-03 14:20:09 +00:00
import (
"io/ioutil"
"os"
2014-08-18 07:03:34 +00:00
"strings"
2013-08-03 14:20:09 +00:00
"testing"
2015-09-04 15:03:10 +00:00
"time"
2013-08-03 14:20:09 +00:00
)
func TestResponse(t *testing.T) {
req := Get("http://httpbin.org/get")
resp, err := req.Response()
2013-08-03 14:20:09 +00:00
if err != nil {
t.Fatal(err)
}
t.Log(resp)
}
func TestGet(t *testing.T) {
req := Get("http://httpbin.org/get")
b, err := req.Bytes()
if err != nil {
t.Fatal(err)
}
t.Log(b)
s, err := req.String()
if err != nil {
t.Fatal(err)
}
t.Log(s)
if string(b) != s {
t.Fatal("request data not match")
}
2014-08-18 07:03:34 +00:00
}
func TestSimplePost(t *testing.T) {
v := "smallfish"
req := Post("http://httpbin.org/post")
req.Param("username", v)
2014-08-18 07:03:34 +00:00
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)
2014-08-18 07:03:34 +00:00
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-10-30 03:16:09 +00:00
//func TestPostFile(t *testing.T) {
// v := "smallfish"
// req := Post("http://httpbin.org/post")
// req.Debug(true)
// req.Param("username", v)
// req.PostFile("uploadfile", "httplib_test.go")
// str, err := req.String()
// if err != nil {
// t.Fatal(err)
// }
// t.Log(str)
// n := strings.Index(str, v)
// if n == -1 {
// t.Fatal(v + " not found in post")
// }
//}
2014-05-08 08:58:08 +00:00
func TestSimplePut(t *testing.T) {
str, err := Put("http://httpbin.org/put").String()
if err != nil {
t.Fatal(err)
}
t.Log(str)
}
func TestSimpleDelete(t *testing.T) {
str, err := Delete("http://httpbin.org/delete").String()
if err != nil {
t.Fatal(err)
}
t.Log(str)
}
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)
2014-08-18 07:03:34 +00:00
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)
2014-08-18 07:03:34 +00:00
n := strings.Index(str, v)
if n == -1 {
t.Fatal(v + " not found in cookie")
}
}
func TestWithBasicAuth(t *testing.T) {
str, err := Get("http://httpbin.org/basic-auth/user/passwd").SetBasicAuth("user", "passwd").String()
if err != nil {
t.Fatal(err)
}
t.Log(str)
n := strings.Index(str, "authenticated")
if n == -1 {
t.Fatal("authenticated not found in response")
}
}
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)
2014-08-18 07:03:34 +00:00
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"
2015-09-11 14:28:28 +00:00
var setting BeegoHTTPSettings
2014-08-18 07:03:34 +00:00
setting.EnableCookie = true
setting.UserAgent = v
setting.Transport = nil
2015-09-04 15:03:10 +00:00
setting.ReadWriteTimeout = 5 * time.Second
2014-08-18 07:03:34 +00:00
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)
2014-08-18 07:03:34 +00:00
n := strings.Index(str, v)
if n == -1 {
t.Fatal(v + " not found in user-agent")
}
}
func TestToJson(t *testing.T) {
req := Get("http://httpbin.org/ip")
resp, err := req.Response()
if err != nil {
t.Fatal(err)
}
t.Log(resp)
// httpbin will return http remote addr
2015-09-11 14:28:28 +00:00
type IP struct {
Origin string `json:"origin"`
}
2015-09-11 14:28:28 +00:00
var ip IP
err = req.ToJSON(&ip)
if err != nil {
t.Fatal(err)
}
t.Log(ip.Origin)
if n := strings.Count(ip.Origin, "."); n != 3 {
t.Fatal("response is not valid ip")
}
}
func TestToFile(t *testing.T) {
f := "beego_testfile"
req := Get("http://httpbin.org/ip")
err := req.ToFile(f)
if err != nil {
t.Fatal(err)
}
defer os.Remove(f)
b, err := ioutil.ReadFile(f)
if n := strings.Index(string(b), "origin"); n == -1 {
t.Fatal(err)
}
}
func TestHeader(t *testing.T) {
req := Get("http://httpbin.org/headers")
req.Header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36")
str, err := req.String()
if err != nil {
t.Fatal(err)
}
t.Log(str)
}