Files
OpenMediaCenter/apiGo/api/ApiBase_test.go
T

73 lines
1.4 KiB
Go
Raw Normal View History

2021-02-23 16:01:29 +00:00
package api
import (
"testing"
)
func cleanUp() {
handlers = make(map[string]Handler)
2021-02-23 16:01:29 +00:00
}
func TestAddHandler(t *testing.T) {
cleanUp()
AddHandler("test", ActorNode, func(info *HandlerInfo) []byte {
2021-02-23 16:01:29 +00:00
return nil
})
if len(handlers) != 1 {
t.Errorf("Handler insertion failed, got: %d handlers, want: %d.", len(handlers), 1)
}
}
func TestCallOfHandler(t *testing.T) {
cleanUp()
i := 0
AddHandler("test", ActorNode, func(info *HandlerInfo) []byte {
2021-02-23 16:01:29 +00:00
i++
return nil
})
// simulate the call of the api
handleAPICall("test", "", ActorNode, nil)
2021-02-23 16:01:29 +00:00
if i != 1 {
t.Errorf("Unexpected number of Lambda calls : %d/1", i)
}
}
func TestDecodingOfArguments(t *testing.T) {
cleanUp()
AddHandler("test", ActorNode, func(info *HandlerInfo) []byte {
var args struct {
Test string
TestInt int
}
err := FillStruct(&args, info.Data)
if err != nil {
t.Errorf("Error parsing args: %s", err.Error())
return nil
}
if args.TestInt != 42 || args.Test != "myString" {
t.Errorf("Wrong parsing of argument parameters : %d/42 - %s/myString", args.TestInt, args.Test)
}
2021-02-23 16:01:29 +00:00
return nil
})
// simulate the call of the api
handleAPICall("test", `{"Test":"myString","TestInt":42}`, ActorNode, nil)
2021-02-23 16:01:29 +00:00
}
func TestNoHandlerCovers(t *testing.T) {
cleanUp()
ret := handleAPICall("test", "", ActorNode, nil)
2021-02-23 16:01:29 +00:00
if ret != nil {
t.Error("Expect nil return within unhandled api action")
}
}