mirror of https://github.com/grpc/grpc-web.git
Add test for StreamInterceptor
This commit is contained in:
parent
9b73daa521
commit
023cad0644
|
@ -166,41 +166,7 @@ class EchoApp {
|
|||
}
|
||||
}
|
||||
|
||||
class StreamResponseInterceptor implements grpcWeb.StreamInterceptor<any, any> {
|
||||
intercept(
|
||||
request: grpcWeb.Request<any, any>,
|
||||
invoker: (request: grpcWeb.Request<any, any>) =>
|
||||
grpcWeb.ClientReadableStream<any>) {
|
||||
class InterceptedStream implements grpcWeb.ClientReadableStream<any> {
|
||||
stream: grpcWeb.ClientReadableStream<any>;
|
||||
constructor(stream: grpcWeb.ClientReadableStream<any>) {
|
||||
this.stream = stream;
|
||||
};
|
||||
on(eventType: string, callback: any) {
|
||||
if (eventType == 'data') {
|
||||
const newCallback = (response: any) => {
|
||||
response.setMessage('[Intcpt Resp1]'+response.getMessage());
|
||||
callback(response);
|
||||
};
|
||||
this.stream.on(eventType, newCallback);
|
||||
} else {
|
||||
this.stream.on(eventType, callback);
|
||||
}
|
||||
return this;
|
||||
};
|
||||
removeListener(eventType: string, callback: any) {
|
||||
}
|
||||
cancel() {}
|
||||
}
|
||||
var reqMsg = request.getRequestMessage();
|
||||
reqMsg.setMessage('[Intcpt Req1]'+reqMsg.getMessage());
|
||||
return new InterceptedStream(invoker(request));
|
||||
};
|
||||
}
|
||||
|
||||
var opts = {'streamInterceptors' : [new StreamResponseInterceptor()]};
|
||||
|
||||
const echoService = new EchoServiceClient('http://localhost:8080', null, opts);
|
||||
const echoService = new EchoServiceClient('http://localhost:8080', null, null);
|
||||
|
||||
const echoApp = new EchoApp(echoService);
|
||||
echoApp.load();
|
||||
|
|
|
@ -17,12 +17,13 @@
|
|||
"typings": "index.d.ts",
|
||||
"scripts": {
|
||||
"build": "node scripts/build.js",
|
||||
"prepare": "npm run build && require-self",
|
||||
"prepare": "npm run build",
|
||||
"prepublishOnly": "npm run build",
|
||||
"test": "mocha --timeout 60000 \"./test/**/*_test.js\""
|
||||
},
|
||||
"license": "Apache-2.0",
|
||||
"devDependencies": {
|
||||
"@types/google-protobuf": "~3.7.0",
|
||||
"command-exists": "~1.2.8",
|
||||
"google-closure-compiler": "~20200224.0.0",
|
||||
"google-protobuf": "~3.12.0",
|
||||
|
@ -30,7 +31,6 @@
|
|||
"gulp-eval": "~1.0.0",
|
||||
"mocha": "~5.2.0",
|
||||
"mock-xmlhttprequest": "~2.0.0",
|
||||
"require-self": "0.2.1",
|
||||
"typescript": "~3.8.0"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
*
|
||||
*/
|
||||
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const {exec} = require("child_process");
|
||||
|
||||
|
@ -50,3 +51,8 @@ let child = exec(closureCommand);
|
|||
|
||||
child.stdout.pipe(process.stdout);
|
||||
child.stderr.pipe(process.stderr);
|
||||
|
||||
fs.symlinkSync(path.resolve(__dirname, "../index.js"),
|
||||
path.resolve(__dirname, "../node_modules/grpc-web.js"));
|
||||
fs.symlinkSync(path.resolve(__dirname, "../index.d.ts"),
|
||||
path.resolve(__dirname, "../node_modules/grpc-web.d.ts"));
|
||||
|
|
|
@ -1,3 +1,20 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2018 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
import * as grpcWeb from 'grpc-web';
|
||||
|
||||
import {MessageOuter} from './generated/test01_pb';
|
||||
|
|
|
@ -1,3 +1,20 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2018 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
import * as grpcWeb from 'grpc-web';
|
||||
|
||||
import {Integer} from './generated/test03_pb';
|
||||
|
|
|
@ -0,0 +1,62 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2018 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
import * as grpcWeb from 'grpc-web';
|
||||
|
||||
import {EchoRequest, EchoResponse} from './generated/echo_pb';
|
||||
import {EchoServiceClient} from './generated/echo_grpc_web_pb';
|
||||
|
||||
// The StreamInterceptor interface is for the callback-based client.
|
||||
class StreamResponseInterceptor implements grpcWeb.StreamInterceptor<
|
||||
EchoRequest, EchoResponse> {
|
||||
intercept(
|
||||
request: grpcWeb.Request<EchoRequest, EchoResponse>,
|
||||
invoker: (request: grpcWeb.Request<EchoRequest, EchoResponse>) =>
|
||||
grpcWeb.ClientReadableStream<EchoResponse>) {
|
||||
class InterceptedStream implements grpcWeb.ClientReadableStream<
|
||||
EchoResponse> {
|
||||
stream: grpcWeb.ClientReadableStream<EchoResponse>;
|
||||
constructor(stream: grpcWeb.ClientReadableStream<EchoResponse>) {
|
||||
this.stream = stream;
|
||||
};
|
||||
on(eventType: string, callback: any) {
|
||||
if (eventType == 'data') {
|
||||
const newCallback = (response: EchoResponse) => {
|
||||
response.setMessage('[-in-]'+response.getMessage());
|
||||
callback(response);
|
||||
};
|
||||
this.stream.on(eventType, newCallback);
|
||||
} else {
|
||||
this.stream.on(eventType, callback);
|
||||
}
|
||||
return this;
|
||||
};
|
||||
removeListener(eventType: string, callback: any) {
|
||||
}
|
||||
cancel() {}
|
||||
}
|
||||
var reqMsg = request.getRequestMessage();
|
||||
reqMsg.setMessage('[-out-]'+reqMsg.getMessage());
|
||||
return new InterceptedStream(invoker(request));
|
||||
};
|
||||
}
|
||||
|
||||
var opts = {'streamInterceptors' : [new StreamResponseInterceptor()]};
|
||||
|
||||
const echoService = new EchoServiceClient('http://localhost:8080', null, opts);
|
||||
|
||||
export {echoService, EchoRequest}
|
|
@ -120,3 +120,71 @@ describe('tsc test02: simple rpc, messages in separate proto', function() {
|
|||
require(relativePath('./tsc-tests/dist/client02.js'));
|
||||
});
|
||||
});
|
||||
|
||||
describe('tsc test03: streamInterceptor', function() {
|
||||
before(function() {
|
||||
cleanup();
|
||||
createGeneratedCodeDir();
|
||||
MockXMLHttpRequest = mockXmlHttpRequest.newMockXhr();
|
||||
global.XMLHttpRequest = MockXMLHttpRequest;
|
||||
const genCmd = `protoc -I=./test/protos echo.proto \
|
||||
--js_out=import_style=commonjs:${outputDir} \
|
||||
--grpc-web_out=import_style=commonjs+dts,mode=grpcwebtext:${outputDir}`;
|
||||
execSync(genCmd);
|
||||
});
|
||||
|
||||
after(function() {
|
||||
cleanup();
|
||||
});
|
||||
|
||||
it('generated code should exist', function() {
|
||||
assertFileExists('./tsc-tests/generated/echo_pb.js');
|
||||
assertFileExists('./tsc-tests/generated/echo_pb.d.ts');
|
||||
assertFileExists('./tsc-tests/generated/echo_grpc_web_pb.js');
|
||||
assertFileExists('./tsc-tests/generated/echo_grpc_web_pb.d.ts');
|
||||
});
|
||||
|
||||
it('tsc should run and export', function(done) {
|
||||
try {
|
||||
execSync(`tsc client03.ts generated/echo_pb.d.ts generated/echo_pb.js \
|
||||
generated/echo_grpc_web_pb.d.ts generated/echo_grpc_web_pb.js \
|
||||
--allowJs --strict --outDir ./dist`, {
|
||||
cwd: relativePath('./tsc-tests')
|
||||
});
|
||||
} catch (e) {
|
||||
assert.fail(e.stdout);
|
||||
}
|
||||
|
||||
// check for the tsc output
|
||||
assertFileExists('./tsc-tests/dist/client03.js');
|
||||
assertFileExists('./tsc-tests/dist/generated/echo_pb.js');
|
||||
|
||||
const {echoService, EchoRequest} =
|
||||
require(relativePath('./tsc-tests/dist/client03.js'));
|
||||
assert.equal('function', typeof echoService.echo);
|
||||
assert.equal('function', typeof echoService.serverStreamingEcho);
|
||||
|
||||
const req = new EchoRequest();
|
||||
req.setMessage('aaa');
|
||||
MockXMLHttpRequest.onSend = function(xhr) {
|
||||
// The interceptor will attach "[-out-]" in front of our proto message.
|
||||
// See the interceptor code in client03.ts.
|
||||
// So by the time the proto is being sent by the underlying transport, it
|
||||
// should contain the string "[-out-]aaa".
|
||||
assert.equal('AAAAAAwKClstb3V0LV1hYWE=', xhr.body);
|
||||
|
||||
xhr.respond(200, {'Content-Type': 'application/grpc-web-text'},
|
||||
// echo it back
|
||||
xhr.body);
|
||||
};
|
||||
echoService.echo(req, {}, (err, response) => {
|
||||
assert.ifError(err);
|
||||
// Now, the interceptor will be invoked again on receiving the response
|
||||
// from the server. It attaches an additional "[-in-]" string in front of
|
||||
// the server response.
|
||||
assert.equal('[-in-][-out-]aaa', response.getMessage());
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue