Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
wgliang committed Nov 28, 2016
1 parent a7925bc commit 5b44389
Show file tree
Hide file tree
Showing 3 changed files with 271 additions and 0 deletions.
15 changes: 15 additions & 0 deletions cfg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"debug": false,
"hostname": "example",
"tags": "cop=goappmonitor,owt=inf,pdl=falcon,module=example",
"step": 60,
"bases":["runtime","debug"],
"push": {
"enabled": true,
"api": "http://127.0.0.1:1988/v1/push"
},
"http": {
"enabled": true,
"listen": "0.0.0.0:2015"
}
}
36 changes: 36 additions & 0 deletions http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package goappmonitor

import (
"io/ioutil"
"net/http"
"testing"
"time"
)

var Urls []string

func TestRenderJson(t *testing.T) {
Urls = []string{"/pfc/proc/metrics/json",
"/pfc/proc/metrics/falcon",
"/pfc/proc/metrics/",
"/pfc/proc/metrics/size",
"/pfc/health",
"/pfc/version",
"/pfc/config",
"/pfc/config/reload"}
go func() {
for _, v := range Urls {
resp, err := http.Get("http://127.0.0.1:2015" + v)
if err != nil {
t.Fatal(err)
}

defer resp.Body.Close()
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
}
}()
time.Sleep(time.Second * 10)
}
220 changes: 220 additions & 0 deletions httplib_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
// 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 goappmonitor

import (
"io/ioutil"
"os"
"strings"
"testing"
"time"
)

func TestResponse(t *testing.T) {
req := Get("http://httpbin.org/get")
resp, err := req.Response()
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")
}
}

func TestSimplePost(t *testing.T) {
v := "smallfish"
req := Post("http://httpbin.org/post")
req.Param("username", v)

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")
}
}

//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")
// }
//}

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)
}

func TestWithCookie(t *testing.T) {
v := "smallfish"
str, err := Get("http://httpbin.org/cookies/set?k1=" + v).SetEnableCookie(true).String()
if err != nil {
t.Fatal(err)
}
t.Log(str)

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 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")
}
}

func TestWithUserAgent(t *testing.T) {
v := "httplib"
str, err := Get("http://httpbin.org/headers").SetUserAgent(v).String()
if err != nil {
t.Fatal(err)
}
t.Log(str)

n := strings.Index(str, v)
if n == -1 {
t.Fatal(v + " not found in user-agent")
}
}

func TestWithSetting(t *testing.T) {
v := "httplib"
var setting BeegoHttpSettings
setting.ShowDebug = false
setting.EnableCookie = true
setting.UserAgent = v
setting.Transport = nil
setting.ReadWriteTimeout = 5 * time.Second
setting.ConnectTimeout = 5 * time.Second
SetDefaultSetting(setting)

str, err := Get("http://httpbin.org/get").String()
if err != nil {
t.Fatal(err)
}
t.Log(str)

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
type IP struct {
Origin string `json:"origin"`
}
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 := "httplib_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)
}

0 comments on commit 5b44389

Please sign in to comment.