Skip to content

Commit 04d03d2

Browse files
authored
Merge pull request grpc-ecosystem#184 from gengo/feature/browser-example
Add browser examples
2 parents 43929f2 + 76f58d9 commit 04d03d2

File tree

11 files changed

+479
-9
lines changed

11 files changed

+479
-9
lines changed

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,26 @@ go:
99
cache:
1010
directories:
1111
- $HOME/local
12+
- ${TRAVIS_BUILD_DIR}/examples/browser/node_modules
1213
before_install:
1314
- ./.travis/build-protoc.sh 3.0.0-beta-3
1415
- ./.travis/install-swagger-codegen.sh 2.1.6
16+
- nvm install v6.1 && nvm use v6.1 && node --version
1517
- go get github.com/golang/lint/golint
1618
- go get github.com/dghubble/sling
1719
install:
1820
- go get github.com/gengo/grpc-gateway/protoc-gen-grpc-gateway
1921
- go get github.com/gengo/grpc-gateway/runtime
2022
- go get github.com/gengo/grpc-gateway/examples
2123
- go get github.com/gengo/grpc-gateway/examples/server
24+
before_script:
25+
- sh -c 'cd examples/browser && npm install'
2226
script:
2327
- make realclean && make examples SWAGGER_CODEGEN="java -jar $HOME/local/swagger-codegen-cli.jar"
2428
- if ! go version | grep devel; then test -z "$(git status --porcelain)" || (git status; git diff; exit 1); fi
2529
- env GLOG_logtostderr=1 go test -race -v github.com/gengo/grpc-gateway/...
2630
- make lint
31+
- sh -c 'cd examples/browser && gulp'
2732
env:
2833
global:
2934
- "PATH=$PATH:$HOME/local/bin"

examples/browser/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/bower_components
2+
/node_modules

examples/browser/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Browser example
2+
3+
This directory contains an example use of grpc-gateway with web browsers.
4+
The following commands automatically runs integration tests with phantomjs.
5+
6+
```shell-session
7+
$ npm install -g gulp-cli
8+
$ npm install
9+
$ gulp
10+
```
11+
12+
## Other examples
13+
14+
### Very simple example
15+
Run
16+
```shell-session
17+
$ gulp bower
18+
$ gulp backends
19+
```
20+
21+
then, open `index.html`.
22+
23+
24+
### Integration test with your browser
25+
26+
Run
27+
```shell-session
28+
$ gulp serve
29+
```
30+
31+
then, open `http://localhost:8000` with your browser.
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
'use strict';
2+
3+
var SwaggerClient = require('swagger-client');
4+
5+
describe('ABitOfEverythingService', function() {
6+
var client;
7+
8+
beforeEach(function(done) {
9+
new SwaggerClient({
10+
url: "http://localhost:8080/swagger/a_bit_of_everything.swagger.json",
11+
usePromise: true,
12+
}).then(function(c) {
13+
client = c;
14+
}).catch(function(err) {
15+
done.fail(err);
16+
}).then(done);
17+
});
18+
19+
describe('Create', function() {
20+
var created;
21+
var expected = {
22+
float_value: 1.5,
23+
double_value: 2.5,
24+
int64_value: "4294967296",
25+
uint64_value: "9223372036854775807",
26+
int32_value: -2147483648,
27+
fixed64_value: "9223372036854775807",
28+
fixed32_value: 4294967295,
29+
bool_value: true,
30+
string_value: "strprefix/foo",
31+
uint32_value: 4294967295,
32+
sfixed32_value: 2147483647,
33+
sfixed64_value: "-4611686018427387904",
34+
sint32_value: 2147483647,
35+
sint64_value: "4611686018427387903",
36+
nonConventionalNameValue: "camelCase",
37+
};
38+
39+
beforeEach(function(done) {
40+
client.ABitOfEverythingService.Create(expected).then(function(resp) {
41+
created = resp.obj;
42+
}).catch(function(err) {
43+
done.fail(err);
44+
}).then(done);
45+
});
46+
47+
it('should assign id', function() {
48+
expect(created.uuid).not.toBe("");
49+
});
50+
51+
it('should echo the request back', function() {
52+
delete created.uuid;
53+
expect(created).toEqual(expected);
54+
});
55+
});
56+
57+
describe('CreateBody', function() {
58+
var created;
59+
var expected = {
60+
float_value: 1.5,
61+
double_value: 2.5,
62+
int64_value: "4294967296",
63+
uint64_value: "9223372036854775807",
64+
int32_value: -2147483648,
65+
fixed64_value: "9223372036854775807",
66+
fixed32_value: 4294967295,
67+
bool_value: true,
68+
string_value: "strprefix/foo",
69+
uint32_value: 4294967295,
70+
sfixed32_value: 2147483647,
71+
sfixed64_value: "-4611686018427387904",
72+
sint32_value: 2147483647,
73+
sint64_value: "4611686018427387903",
74+
nonConventionalNameValue: "camelCase",
75+
76+
nested: [
77+
{ name: "bar", amount: 10 },
78+
{ name: "baz", amount: 20 },
79+
],
80+
repeated_string_value: ["a", "b", "c"],
81+
oneof_string: "x",
82+
// TODO(yugui) Support enum by name
83+
map_value: { a: 1, b: 2 },
84+
mapped_string_value: { a: "x", b: "y" },
85+
mapped_nested_value: {
86+
a: { name: "x", amount: 1 },
87+
b: { name: "y", amount: 2 },
88+
},
89+
};
90+
91+
beforeEach(function(done) {
92+
client.ABitOfEverythingService.CreateBody({
93+
body: expected,
94+
}).then(function(resp) {
95+
created = resp.obj;
96+
}).catch(function(err) {
97+
done.fail(err);
98+
}).then(done);
99+
});
100+
101+
it('should assign id', function() {
102+
expect(created.uuid).not.toBe("");
103+
});
104+
105+
it('should echo the request back', function() {
106+
delete created.uuid;
107+
expect(created).toEqual(expected);
108+
});
109+
});
110+
111+
describe('lookup', function() {
112+
var created;
113+
var expected = {
114+
bool_value: true,
115+
string_value: "strprefix/foo",
116+
};
117+
118+
beforeEach(function(done) {
119+
client.ABitOfEverythingService.CreateBody({
120+
body: expected,
121+
}).then(function(resp) {
122+
created = resp.obj;
123+
}).catch(function(err) {
124+
fail(err);
125+
}).finally(done);
126+
});
127+
128+
it('should look up an object by uuid', function(done) {
129+
client.ABitOfEverythingService.Lookup({
130+
uuid: created.uuid
131+
}).then(function(resp) {
132+
expect(resp.obj).toEqual(created);
133+
}).catch(function(err) {
134+
fail(err.errObj);
135+
}).finally(done);
136+
});
137+
138+
it('should fail if no such object', function(done) {
139+
client.ABitOfEverythingService.Lookup({
140+
uuid: 'not_exist',
141+
}).then(function(resp) {
142+
fail('expected failure but succeeded');
143+
}).catch(function(err) {
144+
expect(err.status).toBe(404);
145+
}).finally(done);
146+
});
147+
});
148+
149+
describe('Delete', function() {
150+
var created;
151+
var expected = {
152+
bool_value: true,
153+
string_value: "strprefix/foo",
154+
};
155+
156+
beforeEach(function(done) {
157+
client.ABitOfEverythingService.CreateBody({
158+
body: expected,
159+
}).then(function(resp) {
160+
created = resp.obj;
161+
}).catch(function(err) {
162+
fail(err);
163+
}).finally(done);
164+
});
165+
166+
it('should delete an object by id', function(done) {
167+
client.ABitOfEverythingService.Delete({
168+
uuid: created.uuid
169+
}).then(function(resp) {
170+
expect(resp.obj).toEqual({});
171+
}).catch(function(err) {
172+
fail(err.errObj);
173+
}).then(function() {
174+
return client.ABitOfEverythingService.Lookup({
175+
uuid: created.uuid
176+
});
177+
}).then(function(resp) {
178+
fail('expected failure but succeeded');
179+
}). catch(function(err) {
180+
expect(err.status).toBe(404);
181+
}).finally(done);
182+
});
183+
});
184+
});
185+

examples/browser/bin/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/*
2+
!/.gitignore

examples/browser/bower.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "grpc-gateway-example-browser",
3+
"description": "Example use of grpc-gateway from browser",
4+
"main": "index.js",
5+
"authors": [
6+
"Yuki Yugui Sonoda <[email protected]>"
7+
],
8+
"license": "SEE LICENSE IN LICENSE file",
9+
"homepage": "https://github.com/gengo/grpc-gateway",
10+
"private": true,
11+
"dependencies": {
12+
"swagger-js": "~> 2.1"
13+
},
14+
"ignore": [
15+
"**/.*",
16+
"node_modules",
17+
"bower_components",
18+
"test",
19+
"tests"
20+
]
21+
}

examples/browser/echo_service.spec.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
3+
var SwaggerClient = require('swagger-client');
4+
5+
describe('EchoService', function() {
6+
var client;
7+
8+
beforeEach(function(done) {
9+
new SwaggerClient({
10+
url: "http://localhost:8080/swagger/echo_service.swagger.json",
11+
usePromise: true,
12+
}).then(function(c) {
13+
client = c;
14+
done();
15+
});
16+
});
17+
18+
describe('Echo', function() {
19+
it('should echo the request back', function(done) {
20+
client.EchoService.Echo(
21+
{id: "foo"},
22+
{responseContentType: "application/json"}
23+
).then(function(resp) {
24+
expect(resp.obj).toEqual({id: "foo"});
25+
}).catch(function(err) {
26+
done.fail(err);
27+
}).then(done);
28+
});
29+
});
30+
31+
describe('EchoBody', function() {
32+
it('should echo the request back', function(done) {
33+
client.EchoService.EchoBody(
34+
{body: {id: "foo"}},
35+
{responseContentType: "application/json"}
36+
).then(function(resp) {
37+
expect(resp.obj).toEqual({id: "foo"});
38+
}).catch(function(err) {
39+
done.fail(err);
40+
}).then(done);
41+
});
42+
});
43+
});

examples/browser/gulpfile.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
"use strict";
2+
3+
var gulp = require('gulp');
4+
5+
var path = require('path');
6+
7+
var bower = require('gulp-bower');
8+
var exit = require('gulp-exit');
9+
var gprocess = require('gulp-process');
10+
var shell = require('gulp-shell');
11+
var jasmineBrowser = require('gulp-jasmine-browser');
12+
var webpack = require('webpack-stream');
13+
14+
gulp.task('bower', function(){
15+
return bower();
16+
});
17+
18+
gulp.task('server', shell.task([
19+
'go build -o bin/example-server github.com/gengo/grpc-gateway/examples/server',
20+
]));
21+
22+
gulp.task('gateway', shell.task([
23+
'go build -o bin/example-gw github.com/gengo/grpc-gateway/examples',
24+
]));
25+
26+
gulp.task('serve-server', ['server'], function(){
27+
gprocess.start('server-server', 'bin/example-server', [
28+
'--logtostderr',
29+
]);
30+
gulp.watch('bin/example-server', ['serve-server']);
31+
});
32+
33+
gulp.task('serve-gateway', ['gateway', 'serve-server'], function(){
34+
gprocess.start('gateway-server', 'bin/example-gw', [
35+
'--logtostderr', '--swagger_dir', path.join(__dirname, "../examplepb"),
36+
]);
37+
gulp.watch('bin/example-gateway', ['serve-gateway']);
38+
});
39+
40+
gulp.task('backends', ['serve-gateway', 'serve-server']);
41+
42+
var specFiles = ['*.spec.js'];
43+
gulp.task('test', ['backends'], function(done) {
44+
return gulp.src(specFiles)
45+
.pipe(webpack({output: {filename: 'spec.js'}}))
46+
.pipe(jasmineBrowser.specRunner({
47+
console: true,
48+
sourceMappedStacktrace: true,
49+
}))
50+
.pipe(jasmineBrowser.headless({
51+
findOpenPort: true,
52+
catch: true,
53+
throwFailures: true,
54+
}))
55+
.on('error', function(err) {
56+
done(err);
57+
process.exit(1);
58+
})
59+
.pipe(exit());
60+
});
61+
62+
gulp.task('serve', ['backends'], function(done) {
63+
var JasminePlugin = require('gulp-jasmine-browser/webpack/jasmine-plugin');
64+
var plugin = new JasminePlugin();
65+
66+
return gulp.src(specFiles)
67+
.pipe(webpack({
68+
output: {filename: 'spec.js'},
69+
watch: true,
70+
plugins: [plugin],
71+
}))
72+
.pipe(jasmineBrowser.specRunner({
73+
sourceMappedStacktrace: true,
74+
}))
75+
.pipe(jasmineBrowser.server({
76+
port: 8000,
77+
whenReady: plugin.whenReady,
78+
}));
79+
});
80+
81+
gulp.task('default', ['test']);

0 commit comments

Comments
 (0)