Add test for promise-based unaryInterceptor too

This commit is contained in:
Stanley Cheung 2020-07-10 00:24:24 -07:00 committed by Stanley Cheung
parent 023cad0644
commit 03691eeae4
6 changed files with 148 additions and 24 deletions

View File

@ -25,3 +25,9 @@ module.Request.prototype.getRequestMessage = function() {};
module.Request.prototype.getMethodDescriptor = function() {};
module.Request.prototype.getMetadata = function() {};
module.Request.prototype.getCallOptions = function() {};
module.UnaryResponse = function() {};
module.UnaryResponse.prototype.getResponseMessage = function() {};
module.UnaryResponse.prototype.getMetadata = function() {};
module.UnaryResponse.prototype.getMethodDescriptor = function() {};
module.UnaryResponse.prototype.getStatus = function() {};

View File

@ -66,7 +66,13 @@ declare module "grpc-web" {
invoker: (request: Request<Req, Resp>) =>
ClientReadableStream<Resp>): ClientReadableStream<Resp>;
}
export interface UnaryInterceptor<Req, Resp> {
intercept(request: Request<Req, Resp>,
invoker: (request: Request<Req, Resp>) =>
Promise<UnaryResponse<Req, Resp>>): Promise<UnaryResponse<Req, Resp>>;
}
export class CallOptions {
constructor(options: { [index: string]: any; });
}

View File

@ -52,7 +52,15 @@ 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"));
function createSymlink(target, path) {
fs.symlink(target, path, (err) => {
if (err && err.code != 'EEXIST') {
throw err;
}
});
}
createSymlink(path.resolve(__dirname, "../index.js"),
path.resolve(__dirname, "../node_modules/grpc-web.js"));
createSymlink(path.resolve(__dirname, "../index.d.ts"),
path.resolve(__dirname, "../node_modules/grpc-web.d.ts"));

View File

@ -21,7 +21,7 @@ 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<
class MyStreamInterceptor implements grpcWeb.StreamInterceptor<
EchoRequest, EchoResponse> {
intercept(
request: grpcWeb.Request<EchoRequest, EchoResponse>,
@ -55,7 +55,7 @@ class StreamResponseInterceptor implements grpcWeb.StreamInterceptor<
};
}
var opts = {'streamInterceptors' : [new StreamResponseInterceptor()]};
var opts = {'streamInterceptors' : [new MyStreamInterceptor()]};
const echoService = new EchoServiceClient('http://localhost:8080', null, opts);

View File

@ -0,0 +1,44 @@
/**
*
* 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 {EchoServicePromiseClient} from './generated/echo_grpc_web_pb';
// The UnaryInterceptor interface is for the promise-based client.
class MyUnaryInterceptor implements grpcWeb.UnaryInterceptor<
EchoRequest, EchoResponse> {
intercept(request: grpcWeb.Request<EchoRequest, EchoResponse>,
invoker: (request: grpcWeb.Request<EchoRequest, EchoResponse>) =>
Promise<grpcWeb.UnaryResponse<EchoRequest, EchoResponse>>) {
const reqMsg = request.getRequestMessage();
reqMsg.setMessage('[-out-]' + reqMsg.getMessage());
return invoker(request).then((response: grpcWeb.UnaryResponse<
EchoRequest, EchoResponse>) => {
const responseMsg = response.getResponseMessage();
responseMsg.setMessage('[-in-]' + responseMsg.getMessage());
return response;
});
}
}
var opts = {'unaryInterceptors' : [new MyUnaryInterceptor()]};
const echoService = new EchoServicePromiseClient('http://localhost:8080', null, opts);
export {echoService, EchoRequest}

View File

@ -38,6 +38,13 @@ function createGeneratedCodeDir() {
function assertFileExists(relPath) {
assert.equal(true, fs.existsSync(relativePath(relPath)));
}
function runTscCmd(tscCmd) {
try {
execSync(tscCmd, {cwd: relativePath('./tsc-tests')});
} catch (e) {
assert.fail(e.stdout);
}
}
const outputDir = './test/tsc-tests/generated';
describe('tsc test01: nested messages', function() {
@ -59,10 +66,8 @@ describe('tsc test01: nested messages', function() {
});
it('tsc should run and export', function() {
execSync(`tsc client01.ts generated/test01_pb.d.ts generated/test01_pb.js \
--allowJs --outDir ./dist`, {
cwd: relativePath('./tsc-tests')
});
runTscCmd(`tsc client01.ts generated/test01_pb.d.ts generated/test01_pb.js \
--allowJs --strict --outDir ./dist`);
// check for the tsc output
assertFileExists('./tsc-tests/dist/client01.js');
@ -98,12 +103,10 @@ describe('tsc test02: simple rpc, messages in separate proto', function() {
});
it('tsc should run and export', function(done) {
execSync(`tsc client02.ts generated/Test02ServiceClientPb.ts \
runTscCmd(`tsc client02.ts generated/Test02ServiceClientPb.ts \
generated/test02_pb.d.ts generated/test02_pb.js \
generated/test03_pb.d.ts generated/test03_pb.js \
--allowJs --outDir ./dist`, {
cwd: relativePath('./tsc-tests')
});
--allowJs --strict --outDir ./dist`);
// check for the tsc output
assertFileExists('./tsc-tests/dist/client02.js');
@ -145,15 +148,9 @@ describe('tsc test03: streamInterceptor', function() {
});
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);
}
runTscCmd(`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`);
// check for the tsc output
assertFileExists('./tsc-tests/dist/client03.js');
@ -177,6 +174,7 @@ describe('tsc test03: streamInterceptor', function() {
// echo it back
xhr.body);
};
// this is the callback-based client
echoService.echo(req, {}, (err, response) => {
assert.ifError(err);
// Now, the interceptor will be invoked again on receiving the response
@ -188,3 +186,65 @@ describe('tsc test03: streamInterceptor', function() {
});
});
describe('tsc test04: unaryInterceptor', 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) {
const tscCmd = `tsc client04.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`;
runTscCmd(tscCmd);
// check for the tsc output
assertFileExists('./tsc-tests/dist/client04.js');
assertFileExists('./tsc-tests/dist/generated/echo_pb.js');
const {echoService, EchoRequest} =
require(relativePath('./tsc-tests/dist/client04.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 client04.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);
};
// this is the promise-based client
echoService.echo(req, {}).then((response) => {
// 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();
});
});
});