github.com/grpc-ecosystem/grpc-gateway/v2@v2.19.1/examples/internal/integration/client_test.go (about) 1 package integration_test 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 8 "github.com/grpc-ecosystem/grpc-gateway/v2/examples/internal/clients/abe" 9 "github.com/grpc-ecosystem/grpc-gateway/v2/examples/internal/clients/echo" 10 "github.com/grpc-ecosystem/grpc-gateway/v2/examples/internal/clients/unannotatedecho" 11 "github.com/rogpeppe/fastuuid" 12 ) 13 14 var uuidgen = fastuuid.MustNewGenerator() 15 16 func TestEchoClient(t *testing.T) { 17 if testing.Short() { 18 t.Skip() 19 return 20 } 21 22 cfg := echo.NewConfiguration() 23 cfg.BasePath = "http://localhost:8088" 24 25 cl := echo.NewAPIClient(cfg) 26 resp, _, err := cl.EchoServiceApi.EchoServiceEcho(context.Background(), "foo", nil) 27 if err != nil { 28 t.Errorf(`cl.EchoServiceApi.Echo("foo") failed with %v; want success`, err) 29 } 30 if got, want := resp.Id, "foo"; got != want { 31 t.Errorf("resp.Id = %q; want %q", got, want) 32 } 33 } 34 35 func TestEchoBodyClient(t *testing.T) { 36 if testing.Short() { 37 t.Skip() 38 return 39 } 40 41 cfg := echo.NewConfiguration() 42 cfg.BasePath = "http://localhost:8088" 43 44 cl := echo.NewAPIClient(cfg) 45 req := echo.ExamplepbSimpleMessage{Id: "foo"} 46 resp, _, err := cl.EchoServiceApi.EchoServiceEchoBody(context.Background(), req) 47 if err != nil { 48 t.Errorf("cl.EchoBody(%#v) failed with %v; want success", req, err) 49 } 50 if got, want := resp.Id, "foo"; got != want { 51 t.Errorf("resp.Id = %q; want %q", got, want) 52 } 53 } 54 55 func TestEchoBody2Client(t *testing.T) { 56 if testing.Short() { 57 t.Skip() 58 return 59 } 60 61 cfg := echo.NewConfiguration() 62 cfg.BasePath = "http://localhost:8088" 63 64 cl := echo.NewAPIClient(cfg) 65 req := echo.ExamplepbEmbedded{Note: "note"} 66 resp, _, err := cl.EchoServiceApi.EchoServiceEchoBody2(context.Background(), "foo", req, nil) 67 if err != nil { 68 t.Errorf("cl.EchoBody(%#v) failed with %v; want success", req, err) 69 } 70 if got, want := resp.Id, "foo"; got != want { 71 t.Errorf("resp.Id = %q; want %q", got, want) 72 } 73 } 74 75 func TestAbitOfEverythingClient(t *testing.T) { 76 if testing.Short() { 77 t.Skip() 78 return 79 } 80 81 cfg := abe.NewConfiguration() 82 cfg.BasePath = "http://localhost:8088" 83 84 cl := abe.NewAPIClient(cfg) 85 86 testABEClientCreate(t, cl) 87 } 88 89 func testABEClientCreate(t *testing.T, cl *abe.APIClient) { 90 enumZero := abe.ZERO_ExamplepbNumericEnum 91 enumPath := abe.ABC_PathenumPathEnum 92 messagePath := abe.JKL_MessagePathEnumNestedPathEnum 93 94 want := &abe.ExamplepbABitOfEverything{ 95 FloatValue: 1.5, 96 DoubleValue: 2.5, 97 Int64Value: "4294967296", 98 Uint64Value: "9223372036854775807", 99 Int32Value: -2147483648, 100 Fixed64Value: "9223372036854775807", 101 Fixed32Value: 4294967295, 102 BoolValue: true, 103 StringValue: "strprefix/foo", 104 Uint32Value: 4294967295, 105 Sfixed32Value: 2147483647, 106 Sfixed64Value: "-4611686018427387904", 107 Sint32Value: 2147483647, 108 Sint64Value: "4611686018427387903", 109 NonConventionalNameValue: "camelCase", 110 EnumValue: &enumZero, 111 PathEnumValue: &enumPath, 112 NestedPathEnumValue: &messagePath, 113 EnumValueAnnotation: &enumZero, 114 Uuid: fmt.Sprintf("%x", uuidgen.Next()), 115 RequiredFieldBehaviorJsonNameCustom: "test", 116 RequiredFieldSchemaJsonNameCustom: "test", 117 } 118 resp, _, err := cl.ABitOfEverythingServiceApi.ABitOfEverythingServiceCreate( 119 context.Background(), 120 want.FloatValue, 121 want.DoubleValue, 122 want.Int64Value, 123 want.Uint64Value, 124 want.Int32Value, 125 want.Fixed64Value, 126 want.Fixed32Value, 127 want.BoolValue, 128 want.StringValue, 129 want.Uint32Value, 130 want.Sfixed32Value, 131 want.Sfixed64Value, 132 want.Sint32Value, 133 want.Sint64Value, 134 want.NonConventionalNameValue, 135 want.EnumValue.String(), 136 want.PathEnumValue.String(), 137 want.NestedPathEnumValue.String(), 138 want.EnumValueAnnotation.String(), 139 want.Uuid, 140 want.RequiredStringViaFieldBehaviorAnnotation, 141 want.StringValue, 142 want.StringValue, 143 want.RequiredFieldBehaviorJsonNameCustom, 144 want.RequiredFieldSchemaJsonNameCustom, 145 nil, 146 ) 147 if err != nil { 148 t.Fatalf("cl.Create(%#v) failed with %v; want success", want, err) 149 } 150 if resp.Uuid == "" { 151 t.Errorf("resp.Uuid is empty; want not empty") 152 } 153 resp.Uuid = "" 154 155 if resp.FloatValue != want.FloatValue { 156 t.Error("float") 157 } 158 if resp.DoubleValue != want.DoubleValue { 159 t.Error("double") 160 } 161 if resp.Int64Value != want.Int64Value { 162 t.Error("double") 163 } 164 if resp.Uint64Value != want.Uint64Value { 165 t.Error("double") 166 } 167 if resp.Int32Value != want.Int32Value { 168 t.Error("double") 169 } 170 if resp.Fixed32Value != want.Fixed32Value { 171 t.Error("bool") 172 } 173 if resp.Fixed64Value != want.Fixed64Value { 174 t.Error("bool") 175 } 176 if resp.BoolValue != want.BoolValue { 177 t.Error("bool") 178 } 179 if resp.StringValue != want.StringValue { 180 t.Error("bool") 181 } 182 if resp.Uint32Value != want.Uint32Value { 183 t.Error("bool") 184 } 185 if resp.Sfixed32Value != want.Sfixed32Value { 186 t.Error("bool") 187 } 188 if resp.Sfixed64Value != want.Sfixed64Value { 189 t.Error("bool") 190 } 191 if resp.Sint32Value != want.Sint32Value { 192 t.Error("bool") 193 } 194 if resp.Sint64Value != want.Sint64Value { 195 t.Error("enum") 196 } 197 if resp.NonConventionalNameValue != want.NonConventionalNameValue { 198 t.Error("enum") 199 } 200 if resp.EnumValue.String() != want.EnumValue.String() { 201 t.Error("enum") 202 } 203 if resp.PathEnumValue.String() != want.PathEnumValue.String() { 204 t.Error("path enum") 205 } 206 if resp.NestedPathEnumValue.String() != want.NestedPathEnumValue.String() { 207 t.Error("nested path enum") 208 } 209 if resp.NestedPathEnumValue.String() != want.NestedPathEnumValue.String() { 210 t.Error("nested path enum") 211 } 212 } 213 214 func TestUnannotatedEchoClient(t *testing.T) { 215 if testing.Short() { 216 t.Skip() 217 return 218 } 219 220 cfg := unannotatedecho.NewConfiguration() 221 cfg.BasePath = "http://localhost:8088" 222 223 cl := unannotatedecho.NewAPIClient(cfg) 224 225 resp, _, err := cl.UnannotatedEchoServiceApi.UnannotatedEchoServiceEcho(context.Background(), "foo", "1", nil) 226 if err != nil { 227 t.Errorf(`cl.Echo("foo") failed with %v; want success`, err) 228 } 229 if got, want := resp.Id, "foo"; got != want { 230 t.Errorf("resp.Id = %q; want %q", got, want) 231 } 232 } 233 234 func TestUnannotatedEchoBodyClient(t *testing.T) { 235 if testing.Short() { 236 t.Skip() 237 return 238 } 239 240 cfg := unannotatedecho.NewConfiguration() 241 cfg.BasePath = "http://localhost:8088" 242 243 cl := unannotatedecho.NewAPIClient(cfg) 244 245 req := unannotatedecho.ExamplepbUnannotatedSimpleMessage{Id: "foo", Num: "1"} 246 resp, _, err := cl.UnannotatedEchoServiceApi.UnannotatedEchoServiceEchoBody(context.Background(), req) 247 if err != nil { 248 t.Errorf("cl.EchoBody(%#v) failed with %v; want success", req, err) 249 } 250 if got, want := resp.Id, "foo"; got != want { 251 t.Errorf("resp.Id = %q; want %q", got, want) 252 } 253 }