forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_test.go
152 lines (128 loc) · 4.31 KB
/
api_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package api
import (
"bytes"
"encoding/json"
"github.com/hybridgroup/gobot"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"testing"
)
func initTestAPI() *api {
log.SetOutput(gobot.NullReadWriteCloser{})
g := gobot.NewGobot()
a := NewAPI(g)
a.start = func(m *api) {}
a.Start()
g.Robots = []*gobot.Robot{
gobot.NewTestRobot("Robot 1"),
gobot.NewTestRobot("Robot 2"),
gobot.NewTestRobot("Robot 3"),
}
return a
}
func TestRobots(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET", "/robots", nil)
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)
body, _ := ioutil.ReadAll(response.Body)
var i []map[string]interface{}
json.Unmarshal(body, &i)
gobot.Expect(t, len(i), 3)
}
func TestRobot(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET", "/robots/Robot%201", nil)
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)
body, _ := ioutil.ReadAll(response.Body)
var i map[string]interface{}
json.Unmarshal(body, &i)
gobot.Expect(t, i["name"].(string), "Robot 1")
}
func TestRobotDevices(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET", "/robots/Robot%201/devices", nil)
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)
body, _ := ioutil.ReadAll(response.Body)
var i []map[string]interface{}
json.Unmarshal(body, &i)
gobot.Expect(t, len(i), 3)
}
func TestRobotCommands(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET", "/robots/Robot%201/commands", nil)
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)
body, _ := ioutil.ReadAll(response.Body)
var i []string
json.Unmarshal(body, &i)
gobot.Expect(t, i, []string{"robotTestFunction"})
}
func TestExecuteRobotCommand(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET", "/robots/Robot%201/commands/robotTestFunction", bytes.NewBufferString(`{"message":"Beep Boop"}`))
request.Header.Add("Content-Type", "application/json")
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)
body, _ := ioutil.ReadAll(response.Body)
var i interface{}
json.Unmarshal(body, &i)
gobot.Expect(t, i, "hey Robot 1, Beep Boop")
}
func TestUnknownRobotCommand(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET", "/robots/Robot%201/commands/robotTestFuntion1", bytes.NewBufferString(`{"message":"Beep Boop"}`))
request.Header.Add("Content-Type", "application/json")
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)
body, _ := ioutil.ReadAll(response.Body)
var i interface{}
json.Unmarshal(body, &i)
gobot.Expect(t, i, "Unknown Command")
}
func TestRobotDevice(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET", "/robots/Robot%201/devices/Device%201", nil)
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)
body, _ := ioutil.ReadAll(response.Body)
var i map[string]interface{}
json.Unmarshal(body, &i)
gobot.Expect(t, i["name"].(string), "Device 1")
}
func TestRobotDeviceCommands(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET", "/robots/Robot%201/devices/Device%201/commands", nil)
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)
body, _ := ioutil.ReadAll(response.Body)
var i []string
json.Unmarshal(body, &i)
gobot.Expect(t, i, []string{"TestDriverCommand", "DriverCommand"})
}
func TestExecuteRobotDeviceCommand(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET", "/robots/Robot%201/devices/Device%201/commands/TestDriverCommand", bytes.NewBufferString(`{"name":"human"}`))
request.Header.Add("Content-Type", "application/json")
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)
body, _ := ioutil.ReadAll(response.Body)
var i interface{}
json.Unmarshal(body, &i)
gobot.Expect(t, i, "hello human")
}
func TestUnknownRobotDeviceCommand(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET", "/robots/Robot%201/devices/Device%201/commands/DriverCommand1", bytes.NewBufferString(`{"name":"human"}`))
request.Header.Add("Content-Type", "application/json")
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)
body, _ := ioutil.ReadAll(response.Body)
var i interface{}
json.Unmarshal(body, &i)
gobot.Expect(t, i, "Unknown Command")
}