2021-01-29 13:17:32 +03:00
|
|
|
package register
|
2020-10-09 16:15:36 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2020-12-13 13:51:09 +03:00
|
|
|
type TestHandler struct{}
|
2020-10-09 16:15:36 +03:00
|
|
|
|
2020-12-13 13:51:09 +03:00
|
|
|
type TestRequest struct{}
|
2020-10-09 16:15:36 +03:00
|
|
|
|
2020-12-13 13:51:09 +03:00
|
|
|
type TestResponse struct{}
|
2020-10-09 16:15:36 +03:00
|
|
|
|
2020-12-13 13:51:09 +03:00
|
|
|
func (t *TestHandler) Test(ctx context.Context, req *TestRequest, rsp *TestResponse) error {
|
2020-10-09 16:15:36 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExtractEndpoint(t *testing.T) {
|
2020-12-13 13:51:09 +03:00
|
|
|
handler := &TestHandler{}
|
2020-10-09 16:15:36 +03:00
|
|
|
typ := reflect.TypeOf(handler)
|
|
|
|
|
2020-10-09 16:20:10 +03:00
|
|
|
var endpoints []*Endpoint
|
2020-10-09 16:15:36 +03:00
|
|
|
|
|
|
|
for m := 0; m < typ.NumMethod(); m++ {
|
2020-10-09 16:20:10 +03:00
|
|
|
if e := ExtractEndpoint(typ.Method(m)); e != nil {
|
2020-10-09 16:15:36 +03:00
|
|
|
endpoints = append(endpoints, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i := len(endpoints); i != 1 {
|
2020-12-13 13:51:09 +03:00
|
|
|
t.Fatalf("Expected 1 endpoint, have %d", i)
|
2020-10-09 16:15:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if endpoints[0].Name != "Test" {
|
2020-12-13 13:51:09 +03:00
|
|
|
t.Fatalf("Expected handler Test, got %s", endpoints[0].Name)
|
2020-10-09 16:15:36 +03:00
|
|
|
}
|
|
|
|
|
2021-04-20 12:39:21 +03:00
|
|
|
if endpoints[0].Request == "" {
|
2020-12-13 13:51:09 +03:00
|
|
|
t.Fatal("Expected non nil Request")
|
2020-10-09 16:15:36 +03:00
|
|
|
}
|
|
|
|
|
2021-04-20 12:39:21 +03:00
|
|
|
if endpoints[0].Response == "" {
|
2020-12-13 13:51:09 +03:00
|
|
|
t.Fatal("Expected non nil Request")
|
2020-10-09 16:15:36 +03:00
|
|
|
}
|
|
|
|
|
2021-04-20 12:39:21 +03:00
|
|
|
if endpoints[0].Request != "TestRequest" {
|
|
|
|
t.Fatalf("Expected TestRequest got %s", endpoints[0].Request)
|
2020-10-09 16:15:36 +03:00
|
|
|
}
|
|
|
|
|
2021-04-20 12:39:21 +03:00
|
|
|
if endpoints[0].Response != "TestResponse" {
|
|
|
|
t.Fatalf("Expected TestResponse got %s", endpoints[0].Response)
|
2020-10-09 16:15:36 +03:00
|
|
|
}
|
|
|
|
|
2021-04-20 12:39:21 +03:00
|
|
|
t.Logf("XXX %#+v\n", endpoints[0])
|
2020-10-09 16:15:36 +03:00
|
|
|
}
|