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

473 lines
14 KiB
Go
Raw Normal View History

//nolint:forcetypeassert,usestdlibvars,bodyclose,noctx // ok here
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
"github.com/stretchr/testify/assert"
"gobot.io/x/gobot/v2"
2014-04-15 17:59:44 -07:00
)
func initTestAPI() *API {
log.SetOutput(NullReadWriteCloser{})
g := gobot.NewManager()
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.NewManager()
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)
assert.Equal(t, 200, response.Code)
}
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)
assert.Equal(t, 200, response.Code)
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)
assert.Equal(t, 200, response.Code)
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)
assert.Equal(t, 200, response.Code)
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)
assert.Equal(t, 404, response.Code)
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)
assert.Equal(t, http.StatusMovedPermanently, response.Code)
assert.Equal(t, "/index.html", response.Header()["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)
assert.NotNil(t, body["MCP"].(map[string]interface{})["robots"])
assert.NotNil(t, body["MCP"].(map[string]interface{})["commands"])
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{}
_ = json.NewDecoder(response.Body).Decode(&body)
assert.Equal(t, []interface{}{"TestFunction"}, body["commands"])
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)
assert.Equal(t, "hey Beep Boop", body.(map[string]interface{})["result"])
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)
assert.Equal(t, "Unknown Command", body.(map[string]interface{})["error"])
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{}
_ = json.NewDecoder(response.Body).Decode(&body)
assert.Len(t, 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)
assert.Equal(t, "Robot1", body["robot"].(map[string]interface{})["name"].(string))
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)
assert.Equal(t, "No Robot found with the name UnknownRobot1", body["error"])
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{}
_ = json.NewDecoder(response.Body).Decode(&body)
assert.Len(t, 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)
assert.Equal(t, "No Robot found with the name UnknownRobot1", body["error"])
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{}
_ = json.NewDecoder(response.Body).Decode(&body)
assert.Equal(t, []interface{}{"robotTestFunction"}, body["commands"])
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)
assert.Equal(t, "No Robot found with the name UnknownRobot1", body["error"])
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
_ = json.NewDecoder(response.Body).Decode(&body)
assert.Equal(t, "hey Robot1, Beep Boop", body.(map[string]interface{})["result"])
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
_ = json.NewDecoder(response.Body).Decode(&body)
assert.Equal(t, "Unknown Command", body.(map[string]interface{})["error"])
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)
assert.Equal(t, "No Robot found with the name UnknownRobot1", body.(map[string]interface{})["error"])
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)
assert.Equal(t, "Device1", body["device"].(map[string]interface{})["name"].(string))
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)
assert.Equal(t, "No Device found with the name UnknownDevice1", body["error"])
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{}
_ = json.NewDecoder(response.Body).Decode(&body)
assert.Len(t, 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)
assert.Equal(t, "No Device found with the name UnknownDevice1", body["error"])
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
_ = json.NewDecoder(response.Body).Decode(&body)
assert.Equal(t, "hello human", body.(map[string]interface{})["result"].(string))
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)
assert.Equal(t, "Unknown Command", body.(map[string]interface{})["error"])
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)
assert.Equal(t, "No Device found with the name UnknownDevice1", body.(map[string]interface{})["error"])
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{}
_ = json.NewDecoder(response.Body).Decode(&body)
assert.Len(t, 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)
assert.Equal(t, "No Robot found with the name UnknownRobot1", body["error"])
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)
assert.Equal(t, "Connection1", body["connection"].(map[string]interface{})["name"].(string))
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)
assert.Equal(t, "No Connection found with the name UnknownConnection1", body["error"])
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.manager.Robot("Robot1").
Device("Device1").(gobot.Eventer).
Event("TestEvent")
go func() {
time.Sleep(time.Millisecond * 5)
a.manager.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')
assert.Equal(t, "data: \"event-data\"\n", data)
done = true
case <-time.After(200 * 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)
assert.Equal(t, "No Event found with the name UnknownEvent", body["error"])
}
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)
assert.Equal(t, 200, response.Code)
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)
assert.Equal(t, 200, response.Code)
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)
assert.Equal(t, 200, response.Code)
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)
assert.Equal(t, 200, response.Code)
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)
assert.Equal(t, 200, response.Code)
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)
assert.Equal(t, 200, response.Code)
2014-07-23 13:37:05 -07:00
}