chore: add more tests (#3009)

This commit is contained in:
Kevin Wan
2023-03-10 20:48:10 +08:00
committed by GitHub
parent 3a493cd6a6
commit c8a17a97be
2 changed files with 93 additions and 45 deletions

View File

@@ -37,50 +37,44 @@ func GetMethods(source grpcurl.DescriptorSource) ([]Method, error) {
for _, method := range svcMethods { for _, method := range svcMethods {
rpcPath := fmt.Sprintf("%s/%s", svc, method.GetName()) rpcPath := fmt.Sprintf("%s/%s", svc, method.GetName())
ext := proto.GetExtension(method.GetMethodOptions(), annotations.E_Http) ext := proto.GetExtension(method.GetMethodOptions(), annotations.E_Http)
if ext == nil { switch rule := ext.(type) {
case *annotations.HttpRule:
if rule == nil {
methods = append(methods, Method{ methods = append(methods, Method{
RpcPath: rpcPath, RpcPath: rpcPath,
}) })
continue continue
} }
httpExt, ok := ext.(*annotations.HttpRule) switch httpRule := rule.GetPattern().(type) {
if !ok {
methods = append(methods, Method{
RpcPath: rpcPath,
})
continue
}
switch rule := httpExt.GetPattern().(type) {
case *annotations.HttpRule_Get: case *annotations.HttpRule_Get:
methods = append(methods, Method{ methods = append(methods, Method{
HttpMethod: http.MethodGet, HttpMethod: http.MethodGet,
HttpPath: adjustHttpPath(rule.Get), HttpPath: adjustHttpPath(httpRule.Get),
RpcPath: rpcPath, RpcPath: rpcPath,
}) })
case *annotations.HttpRule_Post: case *annotations.HttpRule_Post:
methods = append(methods, Method{ methods = append(methods, Method{
HttpMethod: http.MethodPost, HttpMethod: http.MethodPost,
HttpPath: adjustHttpPath(rule.Post), HttpPath: adjustHttpPath(httpRule.Post),
RpcPath: rpcPath, RpcPath: rpcPath,
}) })
case *annotations.HttpRule_Put: case *annotations.HttpRule_Put:
methods = append(methods, Method{ methods = append(methods, Method{
HttpMethod: http.MethodPut, HttpMethod: http.MethodPut,
HttpPath: adjustHttpPath(rule.Put), HttpPath: adjustHttpPath(httpRule.Put),
RpcPath: rpcPath, RpcPath: rpcPath,
}) })
case *annotations.HttpRule_Delete: case *annotations.HttpRule_Delete:
methods = append(methods, Method{ methods = append(methods, Method{
HttpMethod: http.MethodDelete, HttpMethod: http.MethodDelete,
HttpPath: adjustHttpPath(rule.Delete), HttpPath: adjustHttpPath(httpRule.Delete),
RpcPath: rpcPath, RpcPath: rpcPath,
}) })
case *annotations.HttpRule_Patch: case *annotations.HttpRule_Patch:
methods = append(methods, Method{ methods = append(methods, Method{
HttpMethod: http.MethodPatch, HttpMethod: http.MethodPatch,
HttpPath: adjustHttpPath(rule.Patch), HttpPath: adjustHttpPath(httpRule.Patch),
RpcPath: rpcPath, RpcPath: rpcPath,
}) })
default: default:
@@ -88,6 +82,11 @@ func GetMethods(source grpcurl.DescriptorSource) ([]Method, error) {
RpcPath: rpcPath, RpcPath: rpcPath,
}) })
} }
default:
methods = append(methods, Method{
RpcPath: rpcPath,
})
}
} }
} }
} }

View File

@@ -2,11 +2,13 @@ package internal
import ( import (
"encoding/base64" "encoding/base64"
"errors"
"net/http" "net/http"
"os" "os"
"testing" "testing"
"github.com/fullstorydev/grpcurl" "github.com/fullstorydev/grpcurl"
"github.com/jhump/protoreflect/desc"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/hash" "github.com/zeromicro/go-zero/core/hash"
) )
@@ -75,3 +77,50 @@ func TestGetMethodsWithAnnotations(t *testing.T) {
}, },
}, methods) }, methods)
} }
func TestGetMethodsBadCases(t *testing.T) {
t.Run("no services", func(t *testing.T) {
source := &mockDescriptorSource{
servicesErr: errors.New("no services"),
}
_, err := GetMethods(source)
assert.NotNil(t, err)
})
t.Run("no symbol in services", func(t *testing.T) {
source := &mockDescriptorSource{
services: []string{"hello.Hello"},
symbolErr: errors.New("no symbol"),
}
_, err := GetMethods(source)
assert.NotNil(t, err)
})
t.Run("no symbol in services", func(t *testing.T) {
source := &mockDescriptorSource{
services: []string{"hello.Hello"},
symbolErr: errors.New("no symbol"),
}
_, err := GetMethods(source)
assert.NotNil(t, err)
})
}
type mockDescriptorSource struct {
symbolDesc desc.Descriptor
symbolErr error
services []string
servicesErr error
}
func (m *mockDescriptorSource) AllExtensionsForType(_ string) ([]*desc.FieldDescriptor, error) {
return nil, nil
}
func (m *mockDescriptorSource) FindSymbol(_ string) (desc.Descriptor, error) {
return m.symbolDesc, m.symbolErr
}
func (m *mockDescriptorSource) ListServices() ([]string, error) {
return m.services, m.servicesErr
}