-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_test.go
63 lines (55 loc) · 1.79 KB
/
http_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
// goforever - processes management
// Copyright (c) 2013 Garrett Woodworth (https://github.com/gwoo).
// sphere-director - Ninja processes management
// Copyright (c) 2014 Ninja Blocks Inc. (https://github.com/ninjablocks).
package main
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/gwoo/greq"
)
func TestListHandler(t *testing.T) {
daemon.children = children{
"test": &Process{Name: "test"},
}
body, _ := newTestResponse("GET", "/", nil)
ex := fmt.Sprintf("%s", string([]byte(`["test"]`)))
r := fmt.Sprintf("%s", string(body))
if ex != r {
t.Errorf("\nExpected = %v\nResult = %v\n", ex, r)
}
}
func TestShowHandler(t *testing.T) {
daemon.children = children{
"test": &Process{Name: "test"},
}
body, _ := newTestResponse("GET", "/test", nil)
e := []byte(`{"Name":"test","Command":"","Args":null,"Pidfile":"","Logfile":"","Errfile":"","Path":"","Respawn":0,"Delay":"","Ping":"","Pid":0,"Status":""}`)
ex := fmt.Sprintf("%s", e)
r := fmt.Sprintf("%s", body)
if ex != r {
t.Errorf("\nExpected = %v\nResult = %v\n", ex, r)
}
}
func TestPostHandler(t *testing.T) {
daemon.children = children{
"test": &Process{Name: "test", Command: "/bin/echo", Args: []string{"woohoo"}},
}
body, _ := newTestResponse("POST", "/test", nil)
e := []byte(`{"Name":"test","Command":"/bin/echo","Args":["woohoo"],"Pidfile":"","Logfile":"","Errfile":"","Path":"","Respawn":0,"Delay":"","Ping":"","Pid":0,"Status":"stopped"}`)
ex := fmt.Sprintf("%s", e)
r := fmt.Sprintf("%s", body)
if ex != r {
t.Errorf("\nExpected = %v\nResult = %v\n", ex, r)
}
}
func newTestResponse(method string, path string, body io.Reader) ([]byte, *http.Response) {
ts := httptest.NewServer(http.HandlerFunc(Handler))
defer ts.Close()
url := ts.URL + path
b, r, _ := greq.Do(method, url, body)
return b, r
}