-
Notifications
You must be signed in to change notification settings - Fork 571
/
content_checker_test.go
108 lines (96 loc) · 2.19 KB
/
content_checker_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"net"
"net/http"
"testing"
"time"
)
func TestBaseCheckOk(t *testing.T) {
checker := NewBaseChecker([]string{"Hello", "world"}, 10)
go func() {
checker.Write([]byte("this is a world"))
time.Sleep(2 * time.Second)
checker.Write([]byte("Hello, how are you?"))
}()
if !checker.Check() {
t.Fail()
}
}
func TestBaseCheckFail(t *testing.T) {
checker := NewBaseChecker([]string{"Hello", "world"}, 2)
go func() {
checker.Write([]byte("this is a world"))
}()
if checker.Check() {
t.Fail()
}
}
func TestTcpCheckOk(t *testing.T) {
go func() {
listener, err := net.Listen("tcp", ":8999")
if err == nil {
defer listener.Close()
conn, err := listener.Accept()
if err == nil {
defer conn.Close()
conn.Write([]byte("this is a world"))
time.Sleep(3 * time.Second)
conn.Write([]byte("Hello, how are you?"))
}
}
}()
checker := NewTCPChecker("127.0.0.1", 8999, []string{"Hello", "world"}, 10)
if !checker.Check() {
t.Fail()
}
}
func TestTcpCheckFail(t *testing.T) {
go func() {
listener, err := net.Listen("tcp", ":8989")
if err == nil {
conn, err := listener.Accept()
if err == nil {
conn.Write([]byte("this is a world"))
time.Sleep(3 * time.Second)
conn.Close()
}
listener.Close()
}
}()
checker := NewTCPChecker("127.0.0.1", 8989, []string{"Hello", "world"}, 2)
if checker.Check() {
t.Fail()
}
}
func TestHttpCheckOk(t *testing.T) {
go func() {
listener, err := net.Listen("tcp", ":8999")
if err == nil {
http.Serve(listener, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer listener.Close()
w.WriteHeader(http.StatusOK)
w.Write([]byte("this is an response"))
}))
}
}()
checker := NewHTTPChecker("http://127.0.0.1:8999", 2)
if !checker.Check() {
t.Fail()
}
}
func TestHttpCheckFail(t *testing.T) {
go func() {
listener, err := net.Listen("tcp", ":8999")
if err == nil {
http.Serve(listener, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer listener.Close()
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("not found"))
}))
}
}()
checker := NewHTTPChecker("http://127.0.0.1:8999", 2)
if checker.Check() {
t.Fail()
}
}