|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/fullstorydev/grpcurl"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/zeromicro/go-zero/core/hash"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
b64pb = `CpgBCgtoZWxsby5wcm90bxIFaGVsbG8iHQoHUmVxdWVzdBISCgRwaW5nGAEgASgJUgRwaW5nIh4KCFJlc3BvbnNlEhIKBHBvbmcYASABKAlSBHBvbmcyMAoFSGVsbG8SJwoEUGluZxIOLmhlbGxvLlJlcXVlc3QaDy5oZWxsby5SZXNwb25zZUIJWgcuL2hlbGxvYgZwcm90bzM=`
|
|
|
|
b64pbWithAnnotations = `Cs4EChVnb29nbGUvYXBpL2h0dHAucHJvdG8SCmdvb2dsZS5hcGkieQoESHR0cBIqCgVydWxlcxgBIAMoCzIULmdvb2dsZS5hcGkuSHR0cFJ1bGVSBXJ1bGVzEkUKH2Z1bGx5X2RlY29kZV9yZXNlcnZlZF9leHBhbnNpb24YAiABKAhSHGZ1bGx5RGVjb2RlUmVzZXJ2ZWRFeHBhbnNpb24i2gIKCEh0dHBSdWxlEhoKCHNlbGVjdG9yGAEgASgJUghzZWxlY3RvchISCgNnZXQYAiABKAlIAFIDZ2V0EhIKA3B1dBgDIAEoCUgAUgNwdXQSFAoEcG9zdBgEIAEoCUgAUgRwb3N0EhgKBmRlbGV0ZRgFIAEoCUgAUgZkZWxldGUSFgoFcGF0Y2gYBiABKAlIAFIFcGF0Y2gSNwoGY3VzdG9tGAggASgLMh0uZ29vZ2xlLmFwaS5DdXN0b21IdHRwUGF0dGVybkgAUgZjdXN0b20SEgoEYm9keRgHIAEoCVIEYm9keRIjCg1yZXNwb25zZV9ib2R5GAwgASgJUgxyZXNwb25zZUJvZHkSRQoTYWRkaXRpb25hbF9iaW5kaW5ncxg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yEAcSDQoJVFlQRV9CT09MEAgSDwoLVFlQRV9TVFJJTkcQCRIOCgpUWVBFX0dST1VQEAoSEAoMVFlQRV9NRVNTQUdFEAsSDgoKVFlQRV9CWVRFUxAMEg8KC1RZUEVfVUlOVDMyEA0SDQoJVFlQRV9FTlVNEA4SEQoNVFlQRV9TRklYRUQzMhAPEhEKDVRZUEVfU0ZJWEVENjQQEBIPCgtUWVBFX1NJTlQzMhAREg8KC1RZUE
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetMethods(t *testing.T) {
|
|
|
|
tmpfile, err := os.CreateTemp(os.TempDir(), hash.Md5Hex([]byte(b64pb)))
|
|
|
|
assert.Nil(t, err)
|
|
|
|
b, err := base64.StdEncoding.DecodeString(b64pb)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Nil(t, os.WriteFile(tmpfile.Name(), b, os.ModeTemporary))
|
|
|
|
defer os.Remove(tmpfile.Name())
|
|
|
|
|
|
|
|
source, err := grpcurl.DescriptorSourceFromProtoSets(tmpfile.Name())
|
|
|
|
assert.Nil(t, err)
|
|
|
|
methods, err := GetMethods(source)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.EqualValues(t, []Method{
|
|
|
|
{
|
|
|
|
RpcPath: "hello.Hello/Ping",
|
|
|
|
},
|
|
|
|
}, methods)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetMethodsWithAnnotations(t *testing.T) {
|
|
|
|
tmpfile, err := os.CreateTemp(os.TempDir(), hash.Md5Hex([]byte(b64pb)))
|
|
|
|
assert.Nil(t, err)
|
|
|
|
b, err := base64.StdEncoding.DecodeString(b64pbWithAnnotations)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Nil(t, os.WriteFile(tmpfile.Name(), b, os.ModeTemporary))
|
|
|
|
defer os.Remove(tmpfile.Name())
|
|
|
|
|
|
|
|
source, err := grpcurl.DescriptorSourceFromProtoSets(tmpfile.Name())
|
|
|
|
assert.Nil(t, err)
|
|
|
|
methods, err := GetMethods(source)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.EqualValues(t, []Method{
|
|
|
|
{
|
|
|
|
HttpMethod: http.MethodGet,
|
|
|
|
HttpPath: "/v1/get/:foo",
|
|
|
|
RpcPath: "hello.Hello/PingGet",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
HttpMethod: http.MethodPost,
|
|
|
|
HttpPath: "/v1/post",
|
|
|
|
RpcPath: "hello.Hello/PingPost",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
HttpMethod: http.MethodPut,
|
|
|
|
HttpPath: "/v1/put",
|
|
|
|
RpcPath: "hello.Hello/PingPut",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
HttpMethod: http.MethodDelete,
|
|
|
|
HttpPath: "/v1/delete",
|
|
|
|
RpcPath: "hello.Hello/PingDelete",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
HttpMethod: http.MethodPatch,
|
|
|
|
HttpPath: "/v1/patch",
|
|
|
|
RpcPath: "hello.Hello/PingPatch",
|
|
|
|
},
|
|
|
|
}, methods)
|
|
|
|
}
|