go.uber.org/yarpc@v1.72.1/encoding/json/register_test.go (about) 1 // Copyright (c) 2022 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package json 22 23 import ( 24 "context" 25 "testing" 26 27 "github.com/stretchr/testify/assert" 28 ) 29 30 func TestWrapUnaryHandlerInvalid(t *testing.T) { 31 tests := []struct { 32 Name string 33 Func interface{} 34 }{ 35 {"empty", func() {}}, 36 {"not-a-function", 0}, 37 { 38 "wrong-args-in", 39 func(context.Context) (*struct{}, error) { 40 return nil, nil 41 }, 42 }, 43 { 44 "wrong-ctx", 45 func(string, *struct{}) (*struct{}, error) { 46 return nil, nil 47 }, 48 }, 49 { 50 "wrong-req-body", 51 func(context.Context, string, int) (*struct{}, error) { 52 return nil, nil 53 }, 54 }, 55 { 56 "wrong-response", 57 func(context.Context, map[string]interface{}) error { 58 return nil 59 }, 60 }, 61 { 62 "non-pointer-req", 63 func(context.Context, struct{}) (*struct{}, error) { 64 return nil, nil 65 }, 66 }, 67 { 68 "non-pointer-res", 69 func(context.Context, *struct{}) (struct{}, error) { 70 return struct{}{}, nil 71 }, 72 }, 73 { 74 "non-string-key", 75 func(context.Context, map[int32]interface{}) (*struct{}, error) { 76 return nil, nil 77 }, 78 }, 79 { 80 "second-return-value-not-error", 81 func(context.Context, *struct{}) (*struct{}, *struct{}) { 82 return nil, nil 83 }, 84 }, 85 } 86 87 for _, tt := range tests { 88 assert.Panics(t, assert.PanicTestFunc(func() { 89 wrapUnaryHandler(tt.Name, tt.Func) 90 }), tt.Name) 91 } 92 } 93 94 func TestWrapUnaryHandlerValid(t *testing.T) { 95 tests := []struct { 96 Name string 97 Func interface{} 98 }{ 99 { 100 "foo", 101 func(context.Context, *struct{}) (*struct{}, error) { 102 return nil, nil 103 }, 104 }, 105 { 106 "bar", 107 func(context.Context, map[string]interface{}) (*struct{}, error) { 108 return nil, nil 109 }, 110 }, 111 { 112 "baz", 113 func(context.Context, map[string]interface{}) (map[string]interface{}, error) { 114 return nil, nil 115 }, 116 }, 117 { 118 "qux", 119 func(context.Context, interface{}) (map[string]interface{}, error) { 120 return nil, nil 121 }, 122 }, 123 } 124 125 for _, tt := range tests { 126 wrapUnaryHandler(tt.Name, tt.Func) 127 } 128 } 129 130 func TestWrapOnewayHandlerInvalid(t *testing.T) { 131 tests := []struct { 132 Name string 133 Func interface{} 134 }{ 135 {"empty", func() {}}, 136 {"not-a-function", 0}, 137 { 138 "wrong-args-in", 139 func(context.Context) error { 140 return nil 141 }, 142 }, 143 { 144 "wrong-ctx", 145 func(string, *struct{}) error { 146 return nil 147 }, 148 }, 149 { 150 "wrong-req-body", 151 func(context.Context, string, int) error { 152 return nil 153 }, 154 }, 155 { 156 "wrong-response", 157 func(context.Context, map[string]interface{}) (*struct{}, error) { 158 return nil, nil 159 }, 160 }, 161 { 162 "wrong-response-val", 163 func(context.Context, map[string]interface{}) int { 164 return 0 165 }, 166 }, 167 { 168 "non-pointer-req", 169 func(context.Context, struct{}) error { 170 return nil 171 }, 172 }, 173 { 174 "non-string-key", 175 func(context.Context, map[int32]interface{}) error { 176 return nil 177 }, 178 }, 179 } 180 181 for _, tt := range tests { 182 assert.Panics(t, assert.PanicTestFunc(func() { 183 wrapOnewayHandler(tt.Name, tt.Func) 184 })) 185 } 186 } 187 func TestWrapOnewayHandlerValid(t *testing.T) { 188 tests := []struct { 189 Name string 190 Func interface{} 191 }{ 192 { 193 "foo", 194 func(context.Context, *struct{}) error { 195 return nil 196 }, 197 }, 198 { 199 "bar", 200 func(context.Context, map[string]interface{}) error { 201 return nil 202 }, 203 }, 204 { 205 "baz", 206 func(context.Context, map[string]interface{}) error { 207 return nil 208 }, 209 }, 210 { 211 "qux", 212 func(context.Context, interface{}) error { 213 return nil 214 }, 215 }, 216 } 217 218 for _, tt := range tests { 219 wrapOnewayHandler(tt.Name, tt.Func) 220 } 221 }