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

320 lines
9.3 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 (
"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"
2014-07-09 09:38:43 -07:00
"github.com/hybridgroup/gobot"
2014-04-15 17:59:44 -07:00
)
2014-06-13 16:01:39 -07:00
func initTestAPI() *api {
log.SetOutput(gobot.NullReadWriteCloser{})
g := gobot.NewGobot()
a := NewAPI(g)
2014-06-12 14:38:03 -07:00
a.start = func(m *api) {}
a.Start()
2014-07-21 22:19:04 -07:00
a.SetDebug()
2014-06-12 14:38:03 -07:00
2014-07-21 21:14:00 -07:00
g.AddRobot(gobot.NewTestRobot("Robot1"))
g.AddRobot(gobot.NewTestRobot("Robot2"))
g.AddRobot(gobot.NewTestRobot("Robot3"))
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-07-21 21:14:00 -07:00
func TestBasicAuth(t *testing.T) {
a := initTestAPI()
2014-07-21 22:19:04 -07:00
a.SetBasicAuth("admin", "password")
2014-07-21 21:14:00 -07:00
2014-07-24 16:39:27 -07:00
request, _ := http.NewRequest("GET", "/api/", nil)
2014-07-21 21:14:00 -07:00
request.SetBasicAuth("admin", "password")
response := httptest.NewRecorder()
2014-07-23 13:37:05 -07:00
a.ServeHTTP(response, request)
2014-07-21 21:14:00 -07:00
gobot.Assert(t, response.Code, 200)
2014-07-24 16:39:27 -07:00
request, _ = http.NewRequest("GET", "/api/", nil)
2014-07-21 21:14:00 -07:00
request.SetBasicAuth("admin", "wrongPassword")
response = httptest.NewRecorder()
2014-07-23 13:37:05 -07:00
a.ServeHTTP(response, request)
2014-07-21 21:14:00 -07:00
gobot.Assert(t, response.Code, 401)
}
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)
2014-07-21 21:14:00 -07:00
gobot.Assert(t, response.Code, 200)
// js assets
request, _ = http.NewRequest("GET", "/js/app.js", nil)
response = httptest.NewRecorder()
2014-07-23 13:37:05 -07:00
a.ServeHTTP(response, request)
2014-07-21 21:14:00 -07:00
gobot.Assert(t, response.Code, 200)
// css assets
request, _ = http.NewRequest("GET", "/css/style.css", nil)
response = httptest.NewRecorder()
2014-07-23 13:37:05 -07:00
a.ServeHTTP(response, request)
2014-07-21 21:14:00 -07:00
gobot.Assert(t, response.Code, 200)
// 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)
2014-07-21 21:14:00 -07:00
gobot.Assert(t, response.Code, 404)
}
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)
2014-07-24 16:39:27 -07:00
gobot.Refute(t, body["MCP"].(map[string]interface{})["robots"], nil)
gobot.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)
2014-07-24 16:39:27 -07:00
gobot.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)
2014-07-24 16:39:27 -07:00
gobot.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)
gobot.Assert(t, body, "Unknown Command")
}
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)
2014-07-24 16:39:27 -07:00
gobot.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()
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)
2014-07-24 16:39:27 -07:00
gobot.Assert(t, body["robot"].(map[string]interface{})["name"].(string), "Robot1")
2014-06-12 14:38:03 -07:00
}
func TestRobotDevices(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/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)
2014-07-24 16:39:27 -07:00
gobot.Assert(t, len(body["devices"].([]interface{})), 3)
2014-06-12 14:38:03 -07:00
}
func TestRobotCommands(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/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)
2014-07-24 16:39:27 -07:00
gobot.Assert(t, body["commands"], []interface{}{"robotTestFunction"})
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)
2014-07-24 16:39:27 -07:00
gobot.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)
gobot.Assert(t, body, "Unknown Command")
2014-06-12 14:38:03 -07:00
}
func TestRobotDevice(t *testing.T) {
2014-06-13 16:01:39 -07:00
a := initTestAPI()
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)
2014-07-24 16:39:27 -07:00
gobot.Assert(t, body["device"].(map[string]interface{})["name"].(string), "Device1")
2014-06-12 14:38:03 -07:00
}
func TestRobotDeviceCommands(t *testing.T) {
2014-06-13 16:01:39 -07:00
a := initTestAPI()
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)
2014-07-24 16:39:27 -07:00
gobot.Assert(t, len(body["commands"].([]interface{})), 2)
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)
2014-07-24 16:39:27 -07:00
gobot.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)
gobot.Assert(t, body, "Unknown Command")
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()
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)
2014-07-24 16:39:27 -07:00
gobot.Assert(t, len(body["connections"].([]interface{})), 3)
2014-07-21 21:14:00 -07:00
}
func TestRobotConnection(t *testing.T) {
a := initTestAPI()
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)
2014-07-24 16:39:27 -07:00
gobot.Assert(t, body["connection"].(map[string]interface{})["name"].(string), "Connection1")
2014-06-12 14:38:03 -07:00
}
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)
gobot.Assert(t, response.Code, 200)
a.Get("/test", func(res http.ResponseWriter, req *http.Request) {})
request, _ = http.NewRequest("GET", "/test", nil)
response = httptest.NewRecorder()
a.ServeHTTP(response, request)
gobot.Assert(t, response.Code, 200)
a.Post("/test", func(res http.ResponseWriter, req *http.Request) {})
request, _ = http.NewRequest("POST", "/test", nil)
response = httptest.NewRecorder()
a.ServeHTTP(response, request)
gobot.Assert(t, response.Code, 200)
a.Put("/test", func(res http.ResponseWriter, req *http.Request) {})
request, _ = http.NewRequest("PUT", "/test", nil)
response = httptest.NewRecorder()
a.ServeHTTP(response, request)
gobot.Assert(t, response.Code, 200)
a.Delete("/test", func(res http.ResponseWriter, req *http.Request) {})
request, _ = http.NewRequest("DELETE", "/test", nil)
response = httptest.NewRecorder()
a.ServeHTTP(response, request)
gobot.Assert(t, response.Code, 200)
a.Options("/test", func(res http.ResponseWriter, req *http.Request) {})
request, _ = http.NewRequest("OPTIONS", "/test", nil)
response = httptest.NewRecorder()
a.ServeHTTP(response, request)
gobot.Assert(t, response.Code, 200)
}