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