github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/server/grpc/extractor_test.go (about) 1 // Copyright 2020 Asim Aslam 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // Original source: github.com/micro/go-micro/v3/server/grpc/extractor_test.go 16 17 package grpc 18 19 import ( 20 "context" 21 "reflect" 22 "testing" 23 24 "github.com/stretchr/testify/assert" 25 ) 26 27 type TestHandler struct{} 28 29 type TestRequest struct{} 30 31 type TestResponse struct{} 32 33 func (t *TestHandler) Test(ctx context.Context, req *TestRequest, rsp *TestResponse) error { 34 return nil 35 } 36 37 type TestRequest2 struct { 38 int64Arg int64 `json:"int64Arg,omitempty"` 39 stringArg string `json:"stringArg,omitempty"` 40 stringSliceArg []string `json:"stringSliceArg,omitempty"` 41 pointerSliceArg []*TestRequest `json:"pointerSliceArg,omitempty"` 42 mapArg map[string]bool `json:"mapArg,omitempty"` 43 mapPointerArg map[*TestRequest]TestResponse `json:"mapPointerArg,omitempty"` 44 } 45 type TestResponse2 struct { 46 int64Arg int64 `json:"int64Arg,omitempty"` 47 stringArg string `json:"stringArg,omitempty"` 48 stringSliceArg []string `json:"stringSliceArg,omitempty"` 49 pointerSliceArg []*TestRequest `json:"pointerSliceArg,omitempty"` 50 mapArg map[string]bool `json:"mapArg,omitempty"` 51 mapPointerArg map[*TestRequest]TestResponse `json:"mapPointerArg,omitempty"` 52 } 53 54 func (t *TestHandler) Test2(ctx context.Context, req *TestRequest2, rsp *TestResponse2) error { 55 return nil 56 } 57 58 func TestExtractEndpoint(t *testing.T) { 59 handler := &TestHandler{} 60 typ := reflect.TypeOf(handler) 61 62 type param struct { 63 name string 64 value string 65 } 66 tcs := []struct { 67 name string 68 reqName string 69 reqType string 70 reqArgs []param 71 rspName string 72 rspType string 73 rspArgs []param 74 }{ 75 { 76 name: "Test", 77 reqName: "TestRequest", 78 reqType: "TestRequest", 79 rspName: "TestResponse", 80 rspType: "TestResponse", 81 }, 82 { 83 name: "Test2", 84 reqName: "TestRequest2", 85 reqType: "TestRequest2", 86 rspName: "TestResponse2", 87 rspType: "TestResponse2", 88 reqArgs: []param{ 89 { 90 name: "int64Arg", 91 value: "int64", 92 }, 93 { 94 name: "stringArg", 95 value: "string", 96 }, 97 { 98 name: "stringSliceArg", 99 value: "[]string", 100 }, 101 { 102 name: "pointerSliceArg", 103 value: "[]TestRequest", 104 }, 105 { 106 name: "mapArg", 107 value: "map[string]bool", 108 }, 109 { 110 name: "mapPointerArg", 111 value: "map[TestRequest]TestResponse", 112 }, 113 }, 114 rspArgs: []param{ 115 { 116 name: "int64Arg", 117 value: "int64", 118 }, 119 { 120 name: "stringArg", 121 value: "string", 122 }, 123 { 124 name: "stringSliceArg", 125 value: "[]string", 126 }, 127 { 128 name: "pointerSliceArg", 129 value: "[]TestRequest", 130 }, 131 { 132 name: "mapArg", 133 value: "map[string]bool", 134 }, 135 { 136 name: "mapPointerArg", 137 value: "map[TestRequest]TestResponse", 138 }, 139 }, 140 }, 141 } 142 143 for _, tc := range tcs { 144 m, ok := typ.MethodByName(tc.name) 145 assert.True(t, ok) 146 e := extractEndpoint(m) 147 assert.Equal(t, tc.name, e.Name) 148 assert.NotNil(t, e.Request) 149 assert.NotNil(t, e.Response) 150 assert.Equal(t, tc.reqName, e.Request.Name) 151 assert.Equal(t, tc.reqType, e.Request.Type) 152 assert.Equal(t, tc.rspName, e.Response.Name) 153 assert.Equal(t, tc.rspType, e.Response.Type) 154 for i, v := range tc.reqArgs { 155 assert.Equal(t, v.name, e.Request.Values[i].Name) 156 assert.Equal(t, v.value, e.Request.Values[i].Type) 157 } 158 for i, v := range tc.rspArgs { 159 assert.Equal(t, v.name, e.Response.Values[i].Name) 160 assert.Equal(t, v.value, e.Response.Values[i].Type) 161 } 162 } 163 164 }