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

279 lines
7.5 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
request, _ := http.NewRequest("GET", "/", nil)
request.SetBasicAuth("admin", "password")
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)
gobot.Assert(t, response.Code, 200)
request, _ = http.NewRequest("GET", "/", nil)
request.SetBasicAuth("admin", "wrongPassword")
response = httptest.NewRecorder()
a.server.ServeHTTP(response, request)
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()
a.server.ServeHTTP(response, request)
gobot.Assert(t, response.Code, 200)
// js assets
request, _ = http.NewRequest("GET", "/js/app.js", nil)
response = httptest.NewRecorder()
a.server.ServeHTTP(response, request)
gobot.Assert(t, response.Code, 200)
// css assets
request, _ = http.NewRequest("GET", "/css/style.css", nil)
response = httptest.NewRecorder()
a.server.ServeHTTP(response, request)
gobot.Assert(t, response.Code, 200)
// unknown asset
request, _ = http.NewRequest("GET", "/some/fake/file.js", nil)
response = httptest.NewRecorder()
a.server.ServeHTTP(response, request)
gobot.Assert(t, response.Code, 404)
}
func TestMcp(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET", "/", nil)
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)
var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, len(body), 2)
}
func TestMcpCommands(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET", "/commands", nil)
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)
var body []string
json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body, []string{"TestFunction"})
}
func TestExecuteMcpCommand(t *testing.T) {
var body interface{}
a := initTestAPI()
// known command
request, _ := http.NewRequest("GET",
"/commands/TestFunction",
bytes.NewBufferString(`{"message":"Beep Boop"}`),
)
request.Header.Add("Content-Type", "application/json")
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)
json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body, "hey Beep Boop")
// unknown command
request, _ = http.NewRequest("GET",
"/commands/TestFuntion1",
bytes.NewBufferString(`{"message":"Beep Boop"}`),
)
request.Header.Add("Content-Type", "application/json")
response = httptest.NewRecorder()
a.server.ServeHTTP(response, request)
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-06-12 14:38:03 -07:00
request, _ := http.NewRequest("GET", "/robots", nil)
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)
2014-07-21 21:14:00 -07:00
var body []map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, len(body), 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-21 21:14:00 -07:00
request, _ := http.NewRequest("GET", "/robots/Robot1", nil)
2014-06-12 14:38:03 -07:00
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)
2014-07-21 21:14:00 -07:00
var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body["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-21 21:14:00 -07:00
request, _ := http.NewRequest("GET", "/robots/Robot1/devices", nil)
2014-06-12 14:38:03 -07:00
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)
2014-07-21 21:14:00 -07:00
var body []map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, len(body), 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-21 21:14:00 -07:00
request, _ := http.NewRequest("GET", "/robots/Robot1/commands", nil)
2014-06-12 14:38:03 -07:00
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)
2014-07-21 21:14:00 -07:00
var body []string
json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body, []string{"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",
"/robots/Robot1/commands/robotTestFunction",
bytes.NewBufferString(`{"message":"Beep Boop"}`),
)
2014-06-12 14:38:03 -07:00
request.Header.Add("Content-Type", "application/json")
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)
2014-07-21 21:14:00 -07:00
json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body, "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",
"/robots/Robot1/commands/robotTestFuntion1",
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-06-12 14:38:03 -07:00
a.server.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 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",
"/robots/Robot1/devices/Device1",
nil,
)
2014-06-12 14:38:03 -07:00
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)
2014-07-21 21:14:00 -07:00
var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body["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",
"/robots/Robot1/devices/Device1/commands",
nil,
)
2014-06-12 14:38:03 -07:00
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)
2014-07-21 21:14:00 -07:00
var body []string
json.NewDecoder(response.Body).Decode(&body)
2014-07-22 09:16:54 -07:00
gobot.Assert(t, len(body), 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",
"/robots/Robot1/devices/Device1/commands/TestDriverCommand",
bytes.NewBufferString(`{"name":"human"}`),
)
2014-06-12 14:38:03 -07:00
request.Header.Add("Content-Type", "application/json")
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)
2014-07-21 21:14:00 -07:00
json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body, "hello human")
// unknown command
request, _ = http.NewRequest("GET",
"/robots/Robot1/devices/Device1/commands/DriverCommand1",
bytes.NewBufferString(`{"name":"human"}`),
)
request.Header.Add("Content-Type", "application/json")
response = httptest.NewRecorder()
a.server.ServeHTTP(response, request)
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-21 21:14:00 -07:00
request, _ := http.NewRequest("GET", "/robots/Robot1/connections", nil)
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)
var body []map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, len(body), 3)
}
func TestRobotConnection(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET",
"/robots/Robot1/connections/Connection1",
nil,
)
2014-06-12 14:38:03 -07:00
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)
2014-07-21 21:14:00 -07:00
var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body["name"].(string), "Connection1")
2014-06-12 14:38:03 -07:00
}