optimize test
This commit is contained in:
parent
eac6c3a4cd
commit
48eea4de95
@ -1,66 +0,0 @@
|
|||||||
// Copyright 2014 beego Author. All Rights Reserved.
|
|
||||||
//
|
|
||||||
// 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
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// 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 toolbox
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestParse(t *testing.T) {
|
|
||||||
defer ClearTask()
|
|
||||||
|
|
||||||
tk := NewTask("taska", "0/30 * * * * *", func() error { fmt.Println("hello world"); return nil })
|
|
||||||
err := tk.Run()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
AddTask("taska", tk)
|
|
||||||
StartTask()
|
|
||||||
time.Sleep(6 * time.Second)
|
|
||||||
StopTask()
|
|
||||||
}
|
|
||||||
|
|
||||||
//func TestSpec(t *testing.T) {
|
|
||||||
// defer ClearTask()
|
|
||||||
//
|
|
||||||
// wg := &sync.WaitGroup{}
|
|
||||||
// wg.Add(2)
|
|
||||||
// tk1 := NewTask("tk1", "0 12 * * * *", func() error { fmt.Println("tk1"); return nil })
|
|
||||||
// tk2 := NewTask("tk2", "0,10,20 * * * * *", func() error { fmt.Println("tk2"); wg.Done(); return nil })
|
|
||||||
// tk3 := NewTask("tk3", "0 10 * * * *", func() error { fmt.Println("tk3"); wg.Done(); return nil })
|
|
||||||
//
|
|
||||||
// AddTask("tk1", tk1)
|
|
||||||
// AddTask("tk2", tk2)
|
|
||||||
// AddTask("tk3", tk3)
|
|
||||||
// StartTask()
|
|
||||||
// defer StopTask()
|
|
||||||
//
|
|
||||||
// select {
|
|
||||||
// case <-time.After(200 * time.Second):
|
|
||||||
// t.FailNow()
|
|
||||||
// case <-wait(wg):
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func wait(wg *sync.WaitGroup) chan bool {
|
|
||||||
// ch := make(chan bool)
|
|
||||||
// go func() {
|
|
||||||
// wg.Wait()
|
|
||||||
// ch <- true
|
|
||||||
// }()
|
|
||||||
// return ch
|
|
||||||
//}
|
|
||||||
@ -19,6 +19,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -33,9 +34,7 @@ import (
|
|||||||
func TestResponse(t *testing.T) {
|
func TestResponse(t *testing.T) {
|
||||||
req := Get("http://httpbin.org/get")
|
req := Get("http://httpbin.org/get")
|
||||||
resp, err := req.Response()
|
resp, err := req.Response()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Log(resp)
|
t.Log(resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,9 +52,7 @@ func TestDoRequest(t *testing.T) {
|
|||||||
startTime := time.Now().UnixNano() / int64(time.Millisecond)
|
startTime := time.Now().UnixNano() / int64(time.Millisecond)
|
||||||
|
|
||||||
_, err := req.Response()
|
_, err := req.Response()
|
||||||
if err == nil {
|
require.Error(t, err)
|
||||||
t.Fatal("Response should have yielded an error")
|
|
||||||
}
|
|
||||||
|
|
||||||
endTime := time.Now().UnixNano() / int64(time.Millisecond)
|
endTime := time.Now().UnixNano() / int64(time.Millisecond)
|
||||||
elapsedTime := endTime - startTime
|
elapsedTime := endTime - startTime
|
||||||
@ -69,20 +66,13 @@ func TestDoRequest(t *testing.T) {
|
|||||||
func TestGet(t *testing.T) {
|
func TestGet(t *testing.T) {
|
||||||
req := Get("http://httpbin.org/get")
|
req := Get("http://httpbin.org/get")
|
||||||
b, err := req.Bytes()
|
b, err := req.Bytes()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Log(b)
|
t.Log(b)
|
||||||
|
|
||||||
s, err := req.String()
|
s, err := req.String()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Log(s)
|
t.Log(s)
|
||||||
|
require.Equal(t, string(b), s)
|
||||||
if string(b) != s {
|
|
||||||
t.Fatal("request data not match")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSimplePost(t *testing.T) {
|
func TestSimplePost(t *testing.T) {
|
||||||
@ -91,15 +81,11 @@ func TestSimplePost(t *testing.T) {
|
|||||||
req.Param("username", v)
|
req.Param("username", v)
|
||||||
|
|
||||||
str, err := req.String()
|
str, err := req.String()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Log(str)
|
t.Log(str)
|
||||||
|
|
||||||
n := strings.Index(str, v)
|
n := strings.Index(str, v)
|
||||||
if n == -1 {
|
require.NotEqual(t, -1, n)
|
||||||
t.Fatal(v + " not found in post")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// func TestPostFile(t *testing.T) {
|
// func TestPostFile(t *testing.T) {
|
||||||
@ -123,40 +109,30 @@ func TestSimplePost(t *testing.T) {
|
|||||||
|
|
||||||
func TestSimplePut(t *testing.T) {
|
func TestSimplePut(t *testing.T) {
|
||||||
str, err := Put("http://httpbin.org/put").String()
|
str, err := Put("http://httpbin.org/put").String()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Log(str)
|
t.Log(str)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSimpleDelete(t *testing.T) {
|
func TestSimpleDelete(t *testing.T) {
|
||||||
str, err := Delete("http://httpbin.org/delete").String()
|
str, err := Delete("http://httpbin.org/delete").String()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Log(str)
|
t.Log(str)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSimpleDeleteParam(t *testing.T) {
|
func TestSimpleDeleteParam(t *testing.T) {
|
||||||
str, err := Delete("http://httpbin.org/delete").Param("key", "val").String()
|
str, err := Delete("http://httpbin.org/delete").Param("key", "val").String()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Log(str)
|
t.Log(str)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWithCookie(t *testing.T) {
|
func TestWithCookie(t *testing.T) {
|
||||||
v := "smallfish"
|
v := "smallfish"
|
||||||
str, err := Get("http://httpbin.org/cookies/set?k1=" + v).SetEnableCookie(true).String()
|
str, err := Get("http://httpbin.org/cookies/set?k1=" + v).SetEnableCookie(true).String()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Log(str)
|
t.Log(str)
|
||||||
|
|
||||||
str, err = Get("http://httpbin.org/cookies").SetEnableCookie(true).String()
|
str, err = Get("http://httpbin.org/cookies").SetEnableCookie(true).String()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Log(str)
|
t.Log(str)
|
||||||
|
|
||||||
n := strings.Index(str, v)
|
n := strings.Index(str, v)
|
||||||
@ -167,9 +143,7 @@ func TestWithCookie(t *testing.T) {
|
|||||||
|
|
||||||
func TestWithBasicAuth(t *testing.T) {
|
func TestWithBasicAuth(t *testing.T) {
|
||||||
str, err := Get("http://httpbin.org/basic-auth/user/passwd").SetBasicAuth("user", "passwd").String()
|
str, err := Get("http://httpbin.org/basic-auth/user/passwd").SetBasicAuth("user", "passwd").String()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Log(str)
|
t.Log(str)
|
||||||
n := strings.Index(str, "authenticated")
|
n := strings.Index(str, "authenticated")
|
||||||
if n == -1 {
|
if n == -1 {
|
||||||
@ -180,9 +154,7 @@ func TestWithBasicAuth(t *testing.T) {
|
|||||||
func TestWithUserAgent(t *testing.T) {
|
func TestWithUserAgent(t *testing.T) {
|
||||||
v := "beego"
|
v := "beego"
|
||||||
str, err := Get("http://httpbin.org/headers").SetUserAgent(v).String()
|
str, err := Get("http://httpbin.org/headers").SetUserAgent(v).String()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Log(str)
|
t.Log(str)
|
||||||
|
|
||||||
n := strings.Index(str, v)
|
n := strings.Index(str, v)
|
||||||
@ -210,9 +182,7 @@ func TestWithSetting(t *testing.T) {
|
|||||||
SetDefaultSetting(setting)
|
SetDefaultSetting(setting)
|
||||||
|
|
||||||
str, err := Get("http://httpbin.org/get").String()
|
str, err := Get("http://httpbin.org/get").String()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Log(str)
|
t.Log(str)
|
||||||
|
|
||||||
n := strings.Index(str, v)
|
n := strings.Index(str, v)
|
||||||
@ -224,9 +194,7 @@ func TestWithSetting(t *testing.T) {
|
|||||||
func TestToJson(t *testing.T) {
|
func TestToJson(t *testing.T) {
|
||||||
req := Get("http://httpbin.org/ip")
|
req := Get("http://httpbin.org/ip")
|
||||||
resp, err := req.Response()
|
resp, err := req.Response()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Log(resp)
|
t.Log(resp)
|
||||||
|
|
||||||
// httpbin will return http remote addr
|
// httpbin will return http remote addr
|
||||||
@ -235,9 +203,7 @@ func TestToJson(t *testing.T) {
|
|||||||
}
|
}
|
||||||
var ip IP
|
var ip IP
|
||||||
err = req.ToJSON(&ip)
|
err = req.ToJSON(&ip)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Log(ip.Origin)
|
t.Log(ip.Origin)
|
||||||
ips := strings.Split(ip.Origin, ",")
|
ips := strings.Split(ip.Origin, ",")
|
||||||
if len(ips) == 0 {
|
if len(ips) == 0 {
|
||||||
@ -254,9 +220,7 @@ func TestToFile(t *testing.T) {
|
|||||||
f := "beego_testfile"
|
f := "beego_testfile"
|
||||||
req := Get("http://httpbin.org/ip")
|
req := Get("http://httpbin.org/ip")
|
||||||
err := req.ToFile(f)
|
err := req.ToFile(f)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer os.Remove(f)
|
defer os.Remove(f)
|
||||||
b, err := ioutil.ReadFile(f)
|
b, err := ioutil.ReadFile(f)
|
||||||
if n := bytes.Index(b, []byte("origin")); n == -1 {
|
if n := bytes.Index(b, []byte("origin")); n == -1 {
|
||||||
@ -268,9 +232,7 @@ func TestToFileDir(t *testing.T) {
|
|||||||
f := "./files/beego_testfile"
|
f := "./files/beego_testfile"
|
||||||
req := Get("http://httpbin.org/ip")
|
req := Get("http://httpbin.org/ip")
|
||||||
err := req.ToFile(f)
|
err := req.ToFile(f)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll("./files")
|
defer os.RemoveAll("./files")
|
||||||
b, err := ioutil.ReadFile(f)
|
b, err := ioutil.ReadFile(f)
|
||||||
if n := bytes.Index(b, []byte("origin")); n == -1 {
|
if n := bytes.Index(b, []byte("origin")); n == -1 {
|
||||||
@ -282,9 +244,7 @@ func TestHeader(t *testing.T) {
|
|||||||
req := Get("http://httpbin.org/headers")
|
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")
|
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()
|
str, err := req.String()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Log(str)
|
t.Log(str)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user