-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathoperator_test.go
103 lines (84 loc) · 2.12 KB
/
operator_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
package goflow
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func TestCommand(t *testing.T) {
result, _ := Command{Cmd: "sh", Args: []string{"-c", "echo $((2 + 4))"}}.Run()
resultStr := fmt.Sprintf("%v", result)
expected := "6\n"
if resultStr != expected {
t.Errorf("Expected %s, got %s", expected, resultStr)
}
}
func TestGetSuccess(t *testing.T) {
expected := "OK"
srv := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte(expected))
}))
defer srv.Close()
client := &http.Client{}
result, _ := Get{client, srv.URL}.Run()
if result != expected {
t.Errorf("Expected %s, got %s", expected, result)
}
}
func TestGetNotFound(t *testing.T) {
srv := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
w.Write([]byte("Page not found"))
}))
defer srv.Close()
client := &http.Client{}
_, err := Get{client, srv.URL}.Run()
if err == nil {
t.Errorf("Expected an error")
}
}
func TestGetInvalid(t *testing.T) {
client := &http.Client{}
_, err := Get{client, ""}.Run()
if err == nil {
t.Errorf("Expected an error")
}
}
func TestPostSuccess(t *testing.T) {
expected := "OK"
srv := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte(expected))
}))
defer srv.Close()
client := &http.Client{}
result, _ := Post{client, srv.URL, bytes.NewBuffer([]byte(""))}.Run()
if result != expected {
t.Errorf("Expected %s, got %s", expected, result)
}
}
func TestPostNotFound(t *testing.T) {
srv := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
w.Write([]byte("Page not found"))
}))
defer srv.Close()
client := &http.Client{}
_, err := Post{client, srv.URL, bytes.NewBuffer([]byte(""))}.Run()
if err == nil {
t.Errorf("Expected an error")
}
}
func TestPostInvalid(t *testing.T) {
client := &http.Client{}
_, err := Post{client, "", bytes.NewBuffer([]byte(""))}.Run()
if err == nil {
t.Errorf("Expected an error")
}
}