2021-02-23 16:01:29 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func cleanUp() {
|
2021-05-23 12:21:44 +00:00
|
|
|
handlers = make(map[string]Handler)
|
2021-02-23 16:01:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddHandler(t *testing.T) {
|
|
|
|
cleanUp()
|
|
|
|
|
2021-05-23 12:21:44 +00:00
|
|
|
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
|
2021-05-23 12:21:44 +00:00
|
|
|
AddHandler("test", ActorNode, func(info *HandlerInfo) []byte {
|
2021-02-23 16:01:29 +00:00
|
|
|
i++
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// simulate the call of the api
|
2021-05-23 12:21:44 +00:00
|
|
|
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()
|
|
|
|
|
2021-05-23 12:21:44 +00:00
|
|
|
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
|
2021-05-23 12:21:44 +00:00
|
|
|
handleAPICall("test", `{"Test":"myString","TestInt":42}`, ActorNode, nil)
|
2021-02-23 16:01:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoHandlerCovers(t *testing.T) {
|
|
|
|
cleanUp()
|
|
|
|
|
2021-05-23 12:21:44 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|