1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-27 13:48:56 +08:00
hybridgroup.gobot/api/api_test.go

472 lines
14 KiB
Go
Raw Normal View History

2014-04-29 13:20:32 -07:00
package api
2014-04-15 17:59:44 -07:00
import (
"bufio"
2014-04-15 17:59:44 -07:00
"bytes"
"encoding/json"
2014-07-21 21:14:00 -07:00
"fmt"
2014-06-12 14:38:03 -07:00
"log"
2014-04-15 17:59:44 -07:00
"net/http"
"net/http/httptest"
2014-06-12 14:38:03 -07:00
"testing"
"time"
2014-07-09 09:38:43 -07:00
"gobot.io/x/gobot"
"gobot.io/x/gobot/gobottest"
2014-04-15 17:59:44 -07:00
)
func initTestAPI() *API {
log.SetOutput(NullReadWriteCloser{})
g := gobot.NewMaster()
2014-06-13 16:01:39 -07:00
a := NewAPI(g)
a.start = func(m *API) {}
2014-06-12 14:38:03 -07:00
a.Start()
2014-09-25 18:28:28 -07:00
a.Debug()
2014-06-12 14:38:03 -07:00
2014-11-30 00:19:53 -08:00
g.AddRobot(newTestRobot("Robot1"))
g.AddRobot(newTestRobot("Robot2"))
g.AddRobot(newTestRobot("Robot3"))
2014-07-21 21:14:00 -07:00
g.AddCommand("TestFunction", func(params map[string]interface{}) interface{} {
message := params["message"].(string)
return fmt.Sprintf("hey %v", message)
})
2014-06-13 16:01:39 -07:00
return a
2014-06-12 14:38:03 -07:00
}
2014-09-17 18:20:18 -05:00
func TestStartWithoutDefaults(t *testing.T) {
log.SetOutput(NullReadWriteCloser{})
g := gobot.NewMaster()
a := NewAPI(g)
a.start = func(m *API) {}
a.Get("/", func(res http.ResponseWriter, req *http.Request) {})
a.StartWithoutDefaults()
request, _ := http.NewRequest("GET", "/", nil)
response := httptest.NewRecorder()
a.ServeHTTP(response, request)
gobottest.Assert(t, response.Code, 200)
}
2014-07-21 21:14:00 -07:00
func TestRobeaux(t *testing.T) {
a := initTestAPI()
// html assets
request, _ := http.NewRequest("GET", "/index.html", nil)
response := httptest.NewRecorder()
2014-07-23 13:37:05 -07:00
a.ServeHTTP(response, request)
gobottest.Assert(t, response.Code, 200)
2014-07-21 21:14:00 -07:00
// js assets
2015-10-26 09:13:42 -07:00
request, _ = http.NewRequest("GET", "/js/script.js", nil)
2014-07-21 21:14:00 -07:00
response = httptest.NewRecorder()
2014-07-23 13:37:05 -07:00
a.ServeHTTP(response, request)
gobottest.Assert(t, response.Code, 200)
2014-07-21 21:14:00 -07:00
// css assets
2015-10-26 09:13:42 -07:00
request, _ = http.NewRequest("GET", "/css/application.css", nil)
2014-07-21 21:14:00 -07:00
response = httptest.NewRecorder()
2014-07-23 13:37:05 -07:00
a.ServeHTTP(response, request)
gobottest.Assert(t, response.Code, 200)
2014-07-21 21:14:00 -07:00
// unknown asset
2014-07-23 13:37:05 -07:00
request, _ = http.NewRequest("GET", "/js/fake/file.js", nil)
2014-07-21 21:14:00 -07:00
response = httptest.NewRecorder()
2014-07-23 13:37:05 -07:00
a.ServeHTTP(response, request)
gobottest.Assert(t, response.Code, 404)
2014-07-21 21:14:00 -07:00
}
2015-03-16 09:53:05 -06:00
func TestIndex(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET", "/", nil)
response := httptest.NewRecorder()
a.ServeHTTP(response, request)
gobottest.Assert(t, http.StatusMovedPermanently, response.Code)
gobottest.Assert(t, "/index.html", response.HeaderMap["Location"][0])
2015-03-16 09:53:05 -06:00
}
2014-07-21 21:14:00 -07:00
func TestMcp(t *testing.T) {
a := initTestAPI()
2014-07-24 16:39:27 -07:00
request, _ := http.NewRequest("GET", "/api/", nil)
2014-07-21 21:14:00 -07:00
response := httptest.NewRecorder()
2014-07-23 13:37:05 -07:00
a.ServeHTTP(response, request)
2014-07-21 21:14:00 -07:00
var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
gobottest.Refute(t, body["MCP"].(map[string]interface{})["robots"], nil)
gobottest.Refute(t, body["MCP"].(map[string]interface{})["commands"], nil)
2014-07-21 21:14:00 -07:00
}
func TestMcpCommands(t *testing.T) {
a := initTestAPI()
2014-07-24 16:39:27 -07:00
request, _ := http.NewRequest("GET", "/api/commands", nil)
2014-07-21 21:14:00 -07:00
response := httptest.NewRecorder()
2014-07-23 13:37:05 -07:00
a.ServeHTTP(response, request)
2014-07-21 21:14:00 -07:00
2014-07-24 16:39:27 -07:00
var body map[string]interface{}
2014-07-21 21:14:00 -07:00
json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body["commands"], []interface{}{"TestFunction"})
2014-07-21 21:14:00 -07:00
}
func TestExecuteMcpCommand(t *testing.T) {
var body interface{}
a := initTestAPI()
// known command
request, _ := http.NewRequest("GET",
2014-07-24 16:39:27 -07:00
"/api/commands/TestFunction",
2014-07-21 21:14:00 -07:00
bytes.NewBufferString(`{"message":"Beep Boop"}`),
)
request.Header.Add("Content-Type", "application/json")
response := httptest.NewRecorder()
2014-07-23 13:37:05 -07:00
a.ServeHTTP(response, request)
2014-07-21 21:14:00 -07:00
json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body.(map[string]interface{})["result"], "hey Beep Boop")
2014-07-21 21:14:00 -07:00
// unknown command
request, _ = http.NewRequest("GET",
2014-07-24 16:39:27 -07:00
"/api/commands/TestFuntion1",
2014-07-21 21:14:00 -07:00
bytes.NewBufferString(`{"message":"Beep Boop"}`),
)
request.Header.Add("Content-Type", "application/json")
response = httptest.NewRecorder()
2014-07-23 13:37:05 -07:00
a.ServeHTTP(response, request)
2014-07-21 21:14:00 -07:00
json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body.(map[string]interface{})["error"], "Unknown Command")
2014-07-21 21:14:00 -07:00
}
2014-06-12 14:38:03 -07:00
func TestRobots(t *testing.T) {
2014-06-13 16:01:39 -07:00
a := initTestAPI()
2014-07-24 16:39:27 -07:00
request, _ := http.NewRequest("GET", "/api/robots", nil)
2014-06-12 14:38:03 -07:00
response := httptest.NewRecorder()
2014-07-23 13:37:05 -07:00
a.ServeHTTP(response, request)
2014-06-12 14:38:03 -07:00
2014-07-24 16:39:27 -07:00
var body map[string]interface{}
2014-07-21 21:14:00 -07:00
json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, len(body["robots"].([]interface{})), 3)
2014-06-12 14:38:03 -07:00
}
func TestRobot(t *testing.T) {
2014-06-13 16:01:39 -07:00
a := initTestAPI()
2015-03-16 09:53:05 -06:00
// known robot
2014-07-24 16:39:27 -07:00
request, _ := http.NewRequest("GET", "/api/robots/Robot1", nil)
2014-06-12 14:38:03 -07:00
response := httptest.NewRecorder()
2014-07-23 13:37:05 -07:00
a.ServeHTTP(response, request)
2014-06-12 14:38:03 -07:00
2014-07-21 21:14:00 -07:00
var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body["robot"].(map[string]interface{})["name"].(string), "Robot1")
2015-03-16 09:53:05 -06:00
// unknown robot
request, _ = http.NewRequest("GET", "/api/robots/UnknownRobot1", nil)
a.ServeHTTP(response, request)
json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body["error"], "No Robot found with the name UnknownRobot1")
2014-06-12 14:38:03 -07:00
}
func TestRobotDevices(t *testing.T) {
2014-06-13 16:01:39 -07:00
a := initTestAPI()
2015-03-16 09:53:05 -06:00
// known robot
2014-07-24 16:39:27 -07:00
request, _ := http.NewRequest("GET", "/api/robots/Robot1/devices", nil)
2014-06-12 14:38:03 -07:00
response := httptest.NewRecorder()
2014-07-23 13:37:05 -07:00
a.ServeHTTP(response, request)
2014-06-12 14:38:03 -07:00
2014-07-24 16:39:27 -07:00
var body map[string]interface{}
2014-07-21 21:14:00 -07:00
json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, len(body["devices"].([]interface{})), 3)
2015-03-16 09:53:05 -06:00
// unknown robot
request, _ = http.NewRequest("GET", "/api/robots/UnknownRobot1/devices", nil)
a.ServeHTTP(response, request)
json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body["error"], "No Robot found with the name UnknownRobot1")
2014-06-12 14:38:03 -07:00
}
func TestRobotCommands(t *testing.T) {
2014-06-13 16:01:39 -07:00
a := initTestAPI()
2015-03-16 09:53:05 -06:00
// known robot
2014-07-24 16:39:27 -07:00
request, _ := http.NewRequest("GET", "/api/robots/Robot1/commands", nil)
2014-06-12 14:38:03 -07:00
response := httptest.NewRecorder()
2014-07-23 13:37:05 -07:00
a.ServeHTTP(response, request)
2014-06-12 14:38:03 -07:00
2014-07-24 16:39:27 -07:00
var body map[string]interface{}
2014-07-21 21:14:00 -07:00
json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body["commands"], []interface{}{"robotTestFunction"})
2015-03-16 09:53:05 -06:00
// unknown robot
request, _ = http.NewRequest("GET", "/api/robots/UnknownRobot1/commands", nil)
a.ServeHTTP(response, request)
json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body["error"], "No Robot found with the name UnknownRobot1")
2014-06-12 14:38:03 -07:00
}
2014-06-13 16:01:39 -07:00
2014-06-12 14:38:03 -07:00
func TestExecuteRobotCommand(t *testing.T) {
2014-07-21 21:14:00 -07:00
var body interface{}
2014-06-13 16:01:39 -07:00
a := initTestAPI()
2014-07-21 21:14:00 -07:00
// known command
request, _ := http.NewRequest("GET",
2014-07-24 16:39:27 -07:00
"/api/robots/Robot1/commands/robotTestFunction",
2014-07-23 11:24:41 -07:00
bytes.NewBufferString(`{"message":"Beep Boop", "robot":"Robot1"}`),
2014-07-21 21:14:00 -07:00
)
2014-06-12 14:38:03 -07:00
request.Header.Add("Content-Type", "application/json")
response := httptest.NewRecorder()
2014-07-23 13:37:05 -07:00
a.ServeHTTP(response, request)
2014-06-12 14:38:03 -07:00
2014-07-21 21:14:00 -07:00
json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body.(map[string]interface{})["result"], "hey Robot1, Beep Boop")
2014-06-12 14:38:03 -07:00
2014-07-21 21:14:00 -07:00
// unknown command
request, _ = http.NewRequest("GET",
2014-07-24 16:39:27 -07:00
"/api/robots/Robot1/commands/robotTestFuntion1",
2014-07-21 21:14:00 -07:00
bytes.NewBufferString(`{"message":"Beep Boop"}`),
)
2014-06-12 14:38:03 -07:00
request.Header.Add("Content-Type", "application/json")
2014-07-21 21:14:00 -07:00
response = httptest.NewRecorder()
2014-07-23 13:37:05 -07:00
a.ServeHTTP(response, request)
2014-06-12 14:38:03 -07:00
2014-07-21 21:14:00 -07:00
json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body.(map[string]interface{})["error"], "Unknown Command")
2015-03-16 09:53:05 -06:00
// uknown robot
request, _ = http.NewRequest("GET",
"/api/robots/UnknownRobot1/commands/robotTestFuntion1",
bytes.NewBufferString(`{"message":"Beep Boop"}`),
)
request.Header.Add("Content-Type", "application/json")
a.ServeHTTP(response, request)
json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body.(map[string]interface{})["error"], "No Robot found with the name UnknownRobot1")
2014-06-12 14:38:03 -07:00
}
func TestRobotDevice(t *testing.T) {
2014-06-13 16:01:39 -07:00
a := initTestAPI()
2015-03-16 09:53:05 -06:00
// known device
2014-07-21 21:14:00 -07:00
request, _ := http.NewRequest("GET",
2014-07-24 16:39:27 -07:00
"/api/robots/Robot1/devices/Device1",
2014-07-21 21:14:00 -07:00
nil,
)
2014-06-12 14:38:03 -07:00
response := httptest.NewRecorder()
2014-07-23 13:37:05 -07:00
a.ServeHTTP(response, request)
2014-06-12 14:38:03 -07:00
2014-07-21 21:14:00 -07:00
var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body["device"].(map[string]interface{})["name"].(string), "Device1")
2015-03-16 09:53:05 -06:00
// unknown device
request, _ = http.NewRequest("GET",
"/api/robots/Robot1/devices/UnknownDevice1", nil)
a.ServeHTTP(response, request)
json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body["error"], "No Device found with the name UnknownDevice1")
2014-06-12 14:38:03 -07:00
}
func TestRobotDeviceCommands(t *testing.T) {
2014-06-13 16:01:39 -07:00
a := initTestAPI()
2015-03-16 09:53:05 -06:00
// known device
2014-07-21 21:14:00 -07:00
request, _ := http.NewRequest("GET",
2014-07-24 16:39:27 -07:00
"/api/robots/Robot1/devices/Device1/commands",
2014-07-21 21:14:00 -07:00
nil,
)
2014-06-12 14:38:03 -07:00
response := httptest.NewRecorder()
2014-07-23 13:37:05 -07:00
a.ServeHTTP(response, request)
2014-06-12 14:38:03 -07:00
2014-07-24 16:39:27 -07:00
var body map[string]interface{}
2014-07-21 21:14:00 -07:00
json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, len(body["commands"].([]interface{})), 2)
2015-03-16 09:53:05 -06:00
// unknown device
request, _ = http.NewRequest("GET",
"/api/robots/Robot1/devices/UnknownDevice1/commands",
nil,
)
a.ServeHTTP(response, request)
json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body["error"], "No Device found with the name UnknownDevice1")
2014-06-12 14:38:03 -07:00
}
func TestExecuteRobotDeviceCommand(t *testing.T) {
2014-07-21 21:14:00 -07:00
var body interface{}
2014-06-13 16:01:39 -07:00
a := initTestAPI()
2014-07-21 21:14:00 -07:00
// known command
request, _ := http.NewRequest("GET",
2014-07-24 16:39:27 -07:00
"/api/robots/Robot1/devices/Device1/commands/TestDriverCommand",
2014-07-21 21:14:00 -07:00
bytes.NewBufferString(`{"name":"human"}`),
)
2014-06-12 14:38:03 -07:00
request.Header.Add("Content-Type", "application/json")
response := httptest.NewRecorder()
2014-07-23 13:37:05 -07:00
a.ServeHTTP(response, request)
2014-06-12 14:38:03 -07:00
2014-07-21 21:14:00 -07:00
json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body.(map[string]interface{})["result"].(string), "hello human")
2014-07-21 21:14:00 -07:00
// unknown command
request, _ = http.NewRequest("GET",
2014-07-24 16:39:27 -07:00
"/api/robots/Robot1/devices/Device1/commands/DriverCommand1",
2014-07-21 21:14:00 -07:00
bytes.NewBufferString(`{"name":"human"}`),
)
request.Header.Add("Content-Type", "application/json")
response = httptest.NewRecorder()
2014-07-23 13:37:05 -07:00
a.ServeHTTP(response, request)
2014-07-21 21:14:00 -07:00
json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body.(map[string]interface{})["error"], "Unknown Command")
2015-03-16 09:53:05 -06:00
// unknown device
request, _ = http.NewRequest("GET",
"/api/robots/Robot1/devices/UnknownDevice1/commands/DriverCommand1",
bytes.NewBufferString(`{"name":"human"}`),
)
request.Header.Add("Content-Type", "application/json")
a.ServeHTTP(response, request)
json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body.(map[string]interface{})["error"], "No Device found with the name UnknownDevice1")
2015-03-16 09:53:05 -06:00
2014-06-12 14:38:03 -07:00
}
2014-07-21 21:14:00 -07:00
func TestRobotConnections(t *testing.T) {
2014-06-13 16:01:39 -07:00
a := initTestAPI()
2015-03-16 09:53:05 -06:00
// known robot
2014-07-24 16:39:27 -07:00
request, _ := http.NewRequest("GET", "/api/robots/Robot1/connections", nil)
2014-07-21 21:14:00 -07:00
response := httptest.NewRecorder()
2014-07-23 13:37:05 -07:00
a.ServeHTTP(response, request)
2014-07-21 21:14:00 -07:00
2014-07-24 16:39:27 -07:00
var body map[string]interface{}
2014-07-21 21:14:00 -07:00
json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, len(body["connections"].([]interface{})), 3)
2015-03-16 09:53:05 -06:00
// unknown robot
request, _ = http.NewRequest("GET", "/api/robots/UnknownRobot1/connections", nil)
a.ServeHTTP(response, request)
json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body["error"], "No Robot found with the name UnknownRobot1")
2014-07-21 21:14:00 -07:00
}
func TestRobotConnection(t *testing.T) {
a := initTestAPI()
2015-03-16 09:53:05 -06:00
// known connection
2014-07-21 21:14:00 -07:00
request, _ := http.NewRequest("GET",
2014-07-24 16:39:27 -07:00
"/api/robots/Robot1/connections/Connection1",
2014-07-21 21:14:00 -07:00
nil,
)
2014-06-12 14:38:03 -07:00
response := httptest.NewRecorder()
2014-07-23 13:37:05 -07:00
a.ServeHTTP(response, request)
2014-06-12 14:38:03 -07:00
2014-07-21 21:14:00 -07:00
var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body["connection"].(map[string]interface{})["name"].(string), "Connection1")
2015-03-16 09:53:05 -06:00
// unknown connection
request, _ = http.NewRequest("GET",
"/api/robots/Robot1/connections/UnknownConnection1",
nil,
)
a.ServeHTTP(response, request)
json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body["error"], "No Connection found with the name UnknownConnection1")
2014-06-12 14:38:03 -07:00
}
2014-07-23 13:37:05 -07:00
func TestRobotDeviceEvent(t *testing.T) {
a := initTestAPI()
server := httptest.NewServer(a)
defer server.Close()
eventsURL := "/api/robots/Robot1/devices/Device1/events/"
// known event
respc := make(chan *http.Response, 1)
go func() {
resp, _ := http.Get(server.URL + eventsURL + "TestEvent")
respc <- resp
}()
event := a.master.Robot("Robot1").
Device("Device1").(gobot.Eventer).
Event("TestEvent")
go func() {
time.Sleep(time.Millisecond * 5)
a.master.Robot("Robot1").
Device("Device1").(gobot.Eventer).Publish(event, "event-data")
}()
done := false
for !done {
select {
case resp := <-respc:
reader := bufio.NewReader(resp.Body)
data, _ := reader.ReadString('\n')
gobottest.Assert(t, data, "data: \"event-data\"\n")
done = true
case <-time.After(100 * time.Millisecond):
t.Error("Not receiving data")
done = true
}
}
server.CloseClientConnections()
// unknown event
response, _ := http.Get(server.URL + eventsURL + "UnknownEvent")
var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
gobottest.Assert(t, body["error"], "No Event found with the name UnknownEvent")
}
2014-07-23 13:37:05 -07:00
func TestAPIRouter(t *testing.T) {
a := initTestAPI()
a.Head("/test", func(res http.ResponseWriter, req *http.Request) {})
request, _ := http.NewRequest("HEAD", "/test", nil)
response := httptest.NewRecorder()
a.ServeHTTP(response, request)
gobottest.Assert(t, response.Code, 200)
2014-07-23 13:37:05 -07:00
a.Get("/test", func(res http.ResponseWriter, req *http.Request) {})
request, _ = http.NewRequest("GET", "/test", nil)
response = httptest.NewRecorder()
a.ServeHTTP(response, request)
gobottest.Assert(t, response.Code, 200)
2014-07-23 13:37:05 -07:00
a.Post("/test", func(res http.ResponseWriter, req *http.Request) {})
request, _ = http.NewRequest("POST", "/test", nil)
response = httptest.NewRecorder()
a.ServeHTTP(response, request)
gobottest.Assert(t, response.Code, 200)
2014-07-23 13:37:05 -07:00
a.Put("/test", func(res http.ResponseWriter, req *http.Request) {})
request, _ = http.NewRequest("PUT", "/test", nil)
response = httptest.NewRecorder()
a.ServeHTTP(response, request)
gobottest.Assert(t, response.Code, 200)
2014-07-23 13:37:05 -07:00
a.Delete("/test", func(res http.ResponseWriter, req *http.Request) {})
request, _ = http.NewRequest("DELETE", "/test", nil)
response = httptest.NewRecorder()
a.ServeHTTP(response, request)
gobottest.Assert(t, response.Code, 200)
2014-07-23 13:37:05 -07:00
a.Options("/test", func(res http.ResponseWriter, req *http.Request) {})
request, _ = http.NewRequest("OPTIONS", "/test", nil)
response = httptest.NewRecorder()
a.ServeHTTP(response, request)
gobottest.Assert(t, response.Code, 200)
2014-07-23 13:37:05 -07:00
}