Skip to content

Commit 3123f4e

Browse files
committed
Add integration with clients generated by swagger-codegen
1 parent 171d704 commit 3123f4e

19 files changed

+1189
-53
lines changed

.travis.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@ cache:
1010
directories:
1111
- $HOME/local
1212
before_install:
13-
- ./.travis.build-protoc.sh 3.0.0-beta-3
13+
- ./.travis/build-protoc.sh 3.0.0-beta-3
14+
- ./.travis/install-swagger-codegen.sh 2.1.6
1415
- go get github.com/golang/lint/golint
16+
- go get github.com/dghubble/sling
1517
install:
1618
- go get github.com/gengo/grpc-gateway/protoc-gen-grpc-gateway
1719
- go get github.com/gengo/grpc-gateway/runtime
1820
- go get github.com/gengo/grpc-gateway/examples
1921
- go get github.com/gengo/grpc-gateway/examples/server
2022
script:
21-
- make realclean && make examples
23+
- make realclean && make examples SWAGGER_CODEGEN="java -jar $HOME/local/swagger-codegen-cli.jar"
2224
- if ! go version | grep devel; then test -z "$(git status --porcelain)" || (git status; git diff; exit 1); fi
2325
- env GLOG_logtostderr=1 go test -race -v github.com/gengo/grpc-gateway/...
2426
- golint github.com/gengo/grpc-gateway/protoc-gen-grpc-gateway/...
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/sh -eu
22
protoc_version=$1
33
if test -z "${protoc_version}"; then
4-
echo "Usage: .travis.build-protoc.sh protoc-version"
4+
echo "Usage: .travis/build-protoc.sh protoc-version"
55
exit 1
66
fi
77
if ! $HOME/local/bin/protoc-${protoc_version} --version 2>/dev/null; then

.travis/install-swagger-codegen.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/sh -eu
2+
codegen_version=$1
3+
if test -z "${codegen_version}"; then
4+
echo "Usage: .travis/install-swagger-codegen.sh codegen-version"
5+
exit 1
6+
fi
7+
8+
wget http://repo1.maven.org/maven2/io/swagger/swagger-codegen-cli/${codegen_version}/swagger-codegen-cli-${codegen_version}.jar \
9+
-O $HOME/local/swagger-codegen-cli.jar

Makefile

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,24 @@ EXAMPLE_GWSRCS=$(EXAMPLES:.proto=.pb.gw.go)
5656
EXAMPLE_SWAGGERSRCS=$(EXAMPLES:.proto=.swagger.json)
5757
EXAMPLE_DEPS=examples/sub/message.proto examples/sub2/message.proto
5858
EXAMPLE_DEPSRCS=$(EXAMPLE_DEPS:.proto=.pb.go)
59+
60+
EXAMPLE_CLIENT_DIR=examples/clients
61+
ECHO_EXAMPLE_SPEC=examples/examplepb/echo_service.swagger.json
62+
ECHO_EXAMPLE_SRCS=$(EXAMPLE_CLIENT_DIR)/echo/EchoServiceApi.go \
63+
$(EXAMPLE_CLIENT_DIR)/echo/ExamplepbSimpleMessage.go
64+
ABE_EXAMPLE_SPEC=examples/examplepb/a_bit_of_everything.swagger.json
65+
ABE_EXAMPLE_SRCS=$(EXAMPLE_CLIENT_DIR)/abe/ABitOfEverythingServiceApi.go \
66+
$(EXAMPLE_CLIENT_DIR)/abe/ABitOfEverythingNested.go \
67+
$(EXAMPLE_CLIENT_DIR)/abe/ExamplepbABitOfEverything.go \
68+
$(EXAMPLE_CLIENT_DIR)/abe/ExamplepbNumericEnum.go \
69+
$(EXAMPLE_CLIENT_DIR)/abe/ExamplepbIdMessage.go \
70+
$(EXAMPLE_CLIENT_DIR)/abe/NestedDeepEnum.go \
71+
$(EXAMPLE_CLIENT_DIR)/abe/ProtobufEmpty.go \
72+
$(EXAMPLE_CLIENT_DIR)/abe/Sub2IdMessage.go \
73+
$(EXAMPLE_CLIENT_DIR)/abe/SubStringMessage.go
74+
EXAMPLE_CLIENT_SRCS=$(ECHO_EXAMPLE_SRCS) $(ABE_EXAMPLE_SRCS)
75+
SWAGGER_CODEGEN=swagger-codegen
76+
5977
PROTOC_INC_PATH=$(dir $(shell which protoc))/../include
6078

6179
generate: $(OPTIONS_GO) $(RUNTIME_GO)
@@ -88,7 +106,16 @@ $(EXAMPLE_GWSRCS): $(GATEWAY_PLUGIN) $(EXAMPLES)
88106
$(EXAMPLE_SWAGGERSRCS): $(SWAGGER_PLUGIN) $(SWAGGER_EXAMPLES)
89107
protoc -I $(PROTOC_INC_PATH) -I. -I$(GOOGLEAPIS_DIR) --plugin=$(SWAGGER_PLUGIN) --swagger_out=logtostderr=true,$(PKGMAP):. $(SWAGGER_EXAMPLES)
90108

91-
examples: $(EXAMPLE_SVCSRCS) $(EXAMPLE_GWSRCS) $(EXAMPLE_DEPSRCS) $(EXAMPLE_SWAGGERSRCS)
109+
$(ECHO_EXAMPLE_SRCS): $(ECHO_EXAMPLE_SPEC)
110+
$(SWAGGER_CODEGEN) generate -i $(ECHO_EXAMPLE_SPEC) \
111+
-l go -o examples/clients --additional-properties packageName=echo
112+
@rm -f $(EXAMPLE_CLIENT_DIR)/README.md $(EXAMPLE_CLIENT_DIR)/git_push.sh $(EXAMPLE_CLIENT_DIR)/.gitignore
113+
$(ABE_EXAMPLE_SRCS): $(ABE_EXAMPLE_SPEC)
114+
$(SWAGGER_CODEGEN) generate -i $(ABE_EXAMPLE_SPEC) \
115+
-l go -o examples/clients --additional-properties packageName=abe
116+
@rm -f $(EXAMPLE_CLIENT_DIR)/README.md $(EXAMPLE_CLIENT_DIR)/git_push.sh $(EXAMPLE_CLIENT_DIR)/.gitignore
117+
118+
examples: $(EXAMPLE_SVCSRCS) $(EXAMPLE_GWSRCS) $(EXAMPLE_DEPSRCS) $(EXAMPLE_SWAGGERSRCS) $(EXAMPLE_CLIENT_SRCS)
92119
test: examples
93120
go test -race $(PKG)/...
94121

@@ -101,5 +128,6 @@ realclean: distclean
101128
rm -f $(EXAMPLE_SWAGGERSRCS)
102129
rm -f $(GO_PLUGIN)
103130
rm -f $(SWAGGER_PLUGIN)
131+
rm -f $(EXAMPLE_CLIENT_SRCS)
104132

105133
.PHONY: generate examples test clean distclean realclean

examples/client_test.go

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package abe
2+
3+
import (
4+
)
5+
6+
type ABitOfEverythingNested struct {
7+
Amount int64 `json:"amount,omitempty"`
8+
Name string `json:"name,omitempty"`
9+
Ok NestedDeepEnum `json:"ok,omitempty"`
10+
11+
}

0 commit comments

Comments
 (0)