-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli_test.go
82 lines (64 loc) · 1.59 KB
/
cli_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
package rj
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
type response struct {
Code int `json:"code"`
Status string `json:"status"`
Headers map[string]string `json:"header"`
Timing map[string]float64 `json:"timing"`
}
func TestRequest(t *testing.T) {
buffer := &bytes.Buffer{}
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/plain")
w.Header().Add("X-Custom", "Foo")
fmt.Fprintln(w, "Hello, rj")
})
ts := httptest.NewServer(h)
defer ts.Close()
param := param{
method: "GET",
outputWriter: buffer,
}
request(ts.URL, param)
output := buffer.String()
var resp response
err := json.Unmarshal([]byte(output), &resp)
if err != nil {
t.Fatalf("Can't decode JSON")
}
if resp.Code != 200 {
t.Errorf("Expected: %d, Actual: %d", 200, resp.Code)
}
if resp.Headers["content-type"] != "text/plain" {
t.Errorf("Expected: %s, Actual: %s", "text/plain", resp.Headers["content-type"])
}
if resp.Headers["x-custom"] != "Foo" {
t.Errorf("Expected: %s, Actual: %s", "Foo", resp.Headers["x-custom"])
}
if resp.Timing["total"] == 0 {
t.Errorf("timing.total will be greater than 0")
}
}
func TestNewCmd(t *testing.T) {
buffer := new(bytes.Buffer)
cmd := newCmd()
cmd.SetOut(buffer)
cmd.SetArgs([]string{"--version"})
err := cmd.Execute()
if err != nil {
t.Fatal(err)
}
actual := strings.TrimSpace(buffer.String())
expect := "rj version " + Version
if actual != expect {
t.Errorf("Expected: %s, Actual: %s", expect, actual)
}
}