|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/gengo/grpc-gateway/examples/clients/abe" |
| 8 | + "github.com/gengo/grpc-gateway/examples/clients/echo" |
| 9 | +) |
| 10 | + |
| 11 | +func TestClientIntegration(t *testing.T) { |
| 12 | +} |
| 13 | + |
| 14 | +func TestEchoClient(t *testing.T) { |
| 15 | + if testing.Short() { |
| 16 | + t.Skip() |
| 17 | + return |
| 18 | + } |
| 19 | + |
| 20 | + cl := echo.NewEchoServiceApiWithBasePath("http://localhost:8080") |
| 21 | + resp, err := cl.Echo("foo") |
| 22 | + if err != nil { |
| 23 | + t.Errorf(`cl.Echo("foo") failed with %v; want success`, err) |
| 24 | + } |
| 25 | + if got, want := resp.Id, "foo"; got != want { |
| 26 | + t.Errorf("resp.Id = %q; want %q", got, want) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +func TestEchoBodyClient(t *testing.T) { |
| 31 | + if testing.Short() { |
| 32 | + t.Skip() |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + cl := echo.NewEchoServiceApiWithBasePath("http://localhost:8080") |
| 37 | + req := echo.ExamplepbSimpleMessage{Id: "foo"} |
| 38 | + resp, err := cl.EchoBody(req) |
| 39 | + if err != nil { |
| 40 | + t.Errorf("cl.EchoBody(%#v) failed with %v; want success", req, err) |
| 41 | + } |
| 42 | + if got, want := resp.Id, "foo"; got != want { |
| 43 | + t.Errorf("resp.Id = %q; want %q", got, want) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func TestAbitOfEverythingClient(t *testing.T) { |
| 48 | + if testing.Short() { |
| 49 | + t.Skip() |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + cl := abe.NewABitOfEverythingServiceApiWithBasePath("http://localhost:8080") |
| 54 | + testABEClientCreate(t, cl) |
| 55 | + testABEClientCreateBody(t, cl) |
| 56 | +} |
| 57 | + |
| 58 | +func testABEClientCreate(t *testing.T, cl *abe.ABitOfEverythingServiceApi) { |
| 59 | + want := abe.ExamplepbABitOfEverything{ |
| 60 | + FloatValue: 1.5, |
| 61 | + DoubleValue: 2.5, |
| 62 | + Int64Value: "4294967296", |
| 63 | + Uint64Value: "9223372036854775807", |
| 64 | + Int32Value: -2147483648, |
| 65 | + Fixed64Value: "9223372036854775807", |
| 66 | + Fixed32Value: 4294967295, |
| 67 | + BoolValue: true, |
| 68 | + StringValue: "strprefix/foo", |
| 69 | + Uint32Value: 4294967295, |
| 70 | + Sfixed32Value: 2147483647, |
| 71 | + Sfixed64Value: "-4611686018427387904", |
| 72 | + Sint32Value: 2147483647, |
| 73 | + Sint64Value: "4611686018427387903", |
| 74 | + NonConventionalNameValue: "camelCase", |
| 75 | + } |
| 76 | + resp, err := cl.Create( |
| 77 | + want.FloatValue, |
| 78 | + want.DoubleValue, |
| 79 | + want.Int64Value, |
| 80 | + want.Uint64Value, |
| 81 | + want.Int32Value, |
| 82 | + want.Fixed64Value, |
| 83 | + want.Fixed32Value, |
| 84 | + want.BoolValue, |
| 85 | + want.StringValue, |
| 86 | + want.Uint32Value, |
| 87 | + want.Sfixed32Value, |
| 88 | + want.Sfixed64Value, |
| 89 | + want.Sint32Value, |
| 90 | + want.Sint64Value, |
| 91 | + want.NonConventionalNameValue, |
| 92 | + ) |
| 93 | + if err != nil { |
| 94 | + t.Errorf("cl.Create(%#v) failed with %v; want success", want, err) |
| 95 | + } |
| 96 | + if resp.Uuid == "" { |
| 97 | + t.Errorf("resp.Uuid is empty; want not empty") |
| 98 | + } |
| 99 | + resp.Uuid = "" |
| 100 | + if got := resp; !reflect.DeepEqual(got, want) { |
| 101 | + t.Errorf("resp = %#v; want %#v", got, want) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func testABEClientCreateBody(t *testing.T, cl *abe.ABitOfEverythingServiceApi) { |
| 106 | + t.Log("TODO: support enum") |
| 107 | + return |
| 108 | + |
| 109 | + want := abe.ExamplepbABitOfEverything{ |
| 110 | + FloatValue: 1.5, |
| 111 | + DoubleValue: 2.5, |
| 112 | + Int64Value: "4294967296", |
| 113 | + Uint64Value: "9223372036854775807", |
| 114 | + Int32Value: -2147483648, |
| 115 | + Fixed64Value: "9223372036854775807", |
| 116 | + Fixed32Value: 4294967295, |
| 117 | + BoolValue: true, |
| 118 | + StringValue: "strprefix/foo", |
| 119 | + Uint32Value: 4294967295, |
| 120 | + Sfixed32Value: 2147483647, |
| 121 | + Sfixed64Value: "-4611686018427387904", |
| 122 | + Sint32Value: 2147483647, |
| 123 | + Sint64Value: "4611686018427387903", |
| 124 | + NonConventionalNameValue: "camelCase", |
| 125 | + |
| 126 | + Nested: []abe.ABitOfEverythingNested{ |
| 127 | + { |
| 128 | + Name: "bar", |
| 129 | + Amount: 10, |
| 130 | + }, |
| 131 | + { |
| 132 | + Name: "baz", |
| 133 | + Amount: 20, |
| 134 | + }, |
| 135 | + }, |
| 136 | + RepeatedStringValue: []string{"a", "b", "c"}, |
| 137 | + OneofString: "x", |
| 138 | + MapValue: map[string]abe.ExamplepbNumericEnum{ |
| 139 | + // "a": abe.ExamplepbNumericEnum_ONE, |
| 140 | + // "b": abe.ExamplepbNumericEnum_ZERO, |
| 141 | + }, |
| 142 | + MappedStringValue: map[string]string{ |
| 143 | + "a": "x", |
| 144 | + "b": "y", |
| 145 | + }, |
| 146 | + MappedNestedValue: map[string]abe.ABitOfEverythingNested{ |
| 147 | + "a": {Name: "x", Amount: 1}, |
| 148 | + "b": {Name: "y", Amount: 2}, |
| 149 | + }, |
| 150 | + } |
| 151 | + resp, err := cl.CreateBody(want) |
| 152 | + if err != nil { |
| 153 | + t.Errorf("cl.CreateBody(%#v) failed with %v; want success", want, err) |
| 154 | + } |
| 155 | + if resp.Uuid == "" { |
| 156 | + t.Errorf("resp.Uuid is empty; want not empty") |
| 157 | + } |
| 158 | + resp.Uuid = "" |
| 159 | + if got := resp; !reflect.DeepEqual(got, want) { |
| 160 | + t.Errorf("resp = %#v; want %#v", got, want) |
| 161 | + } |
| 162 | +} |
0 commit comments