Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test to show how to access metadata in interceptor #910

Merged
merged 1 commit into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/grpc-web/test/tsc-tests/client04.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,19 @@ class MyUnaryInterceptor implements grpcWeb.UnaryInterceptor<
reqMsg.setMessage('[-out-]' + reqMsg.getMessage());
return invoker(request).then((response: grpcWeb.UnaryResponse<
EchoRequest, EchoResponse>) => {
let result = '<-InitialMetadata->';
let initialMetadata = response.getMetadata();
for (let i in initialMetadata) {
result += i + ': ' + initialMetadata[i];
}
result += '<-TrailingMetadata->';
let trailingMetadata = response.getStatus().metadata;
for (let i in trailingMetadata) {
result += i + ': ' + trailingMetadata[i];
}
const responseMsg = response.getResponseMessage();
responseMsg.setMessage('[-in-]' + responseMsg.getMessage());
result += '[-in-]' + responseMsg.getMessage();
responseMsg.setMessage(result);
return response;
});
}
Expand Down
49 changes: 39 additions & 10 deletions packages/grpc-web/test/tsc_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ function createGeneratedCodeDir() {
function assertFileExists(relPath) {
assert.equal(true, fs.existsSync(relativePath(relPath)));
}
function multiDone(done, count) {
return function() {
count -= 1;
if (count <= 0) {
done();
}
};
}
function runTscCmd(tscCmd) {
try {
execSync(tscCmd, {cwd: relativePath('./tsc-tests')});
Expand Down Expand Up @@ -149,6 +157,7 @@ describe('tsc test03: streamInterceptor', function() {
});

it('tsc should run and export', function(done) {
done = multiDone(done, 3);
const tscCmd = `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 \
Expand All @@ -173,19 +182,34 @@ describe('tsc test03: streamInterceptor', function() {
// 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);
xhr.respond(200, {'Content-Type': 'application/grpc-web-text',
'p': 'q'}, // add a piece of initial metadata
// echo it back, plus a trailing metadata "x: y"
'AAAAAAwKClstb3V0LV1hYWGAAAAABng6IHkNCg==');
};
// this is the callback-based client
echoService.echo(req, {}, (err, response) => {
var call = 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();
});
call.on('metadata', (initialMetadata) => {
assert('p' in initialMetadata);
assert(!('x' in initialMetadata));
assert.equal('q', initialMetadata['p']);
done();
});
call.on('status', (status) => {
assert('metadata' in status);
var trailingMetadata = status.metadata;
assert('x' in trailingMetadata);
assert(!('p' in trailingMetadata));
assert.equal('y', trailingMetadata['x']);
done();
});
});

});
Expand Down Expand Up @@ -237,16 +261,21 @@ describe('tsc test04: unaryInterceptor', function() {
// 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);

xhr.respond(200, {'Content-Type': 'application/grpc-web-text',
'p': 'q'}, // add a piece of initial metadata
// echo it back, plus a trailing metadata "x: y"
'AAAAAAwKClstb3V0LV1hYWGAAAAABng6IHkNCg==');
};
// 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());
// from the server. See the initerceptor logic in client04.ts. It
// flattens both the initialMetadata and the trailingMetadata, and then
// attaches an additional "[-in-]" string in front of the server
// response.
assert.equal('<-InitialMetadata->p: q<-TrailingMetadata->x: y'+
'[-in-][-out-]aaa', response.getMessage());
done();
});
});
Expand Down