mirror of https://github.com/grpc/grpc-web.git
Add test to show how to access metadata in interceptor
This commit is contained in:
parent
5276b1e36d
commit
675deb6e34
|
@ -30,8 +30,19 @@ class MyUnaryInterceptor implements grpcWeb.UnaryInterceptor<
|
||||||
reqMsg.setMessage('[-out-]' + reqMsg.getMessage());
|
reqMsg.setMessage('[-out-]' + reqMsg.getMessage());
|
||||||
return invoker(request).then((response: grpcWeb.UnaryResponse<
|
return invoker(request).then((response: grpcWeb.UnaryResponse<
|
||||||
EchoRequest, EchoResponse>) => {
|
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();
|
const responseMsg = response.getResponseMessage();
|
||||||
responseMsg.setMessage('[-in-]' + responseMsg.getMessage());
|
result += '[-in-]' + responseMsg.getMessage();
|
||||||
|
responseMsg.setMessage(result);
|
||||||
return response;
|
return response;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,6 +38,14 @@ function createGeneratedCodeDir() {
|
||||||
function assertFileExists(relPath) {
|
function assertFileExists(relPath) {
|
||||||
assert.equal(true, fs.existsSync(relativePath(relPath)));
|
assert.equal(true, fs.existsSync(relativePath(relPath)));
|
||||||
}
|
}
|
||||||
|
function multiDone(done, count) {
|
||||||
|
return function() {
|
||||||
|
count -= 1;
|
||||||
|
if (count <= 0) {
|
||||||
|
done();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
function runTscCmd(tscCmd) {
|
function runTscCmd(tscCmd) {
|
||||||
try {
|
try {
|
||||||
execSync(tscCmd, {cwd: relativePath('./tsc-tests')});
|
execSync(tscCmd, {cwd: relativePath('./tsc-tests')});
|
||||||
|
@ -149,6 +157,7 @@ describe('tsc test03: streamInterceptor', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('tsc should run and export', function(done) {
|
it('tsc should run and export', function(done) {
|
||||||
|
done = multiDone(done, 3);
|
||||||
const tscCmd = `tsc client03.ts \
|
const tscCmd = `tsc client03.ts \
|
||||||
generated/echo_pb.d.ts generated/echo_pb.js \
|
generated/echo_pb.d.ts generated/echo_pb.js \
|
||||||
generated/echo_grpc_web_pb.d.ts generated/echo_grpc_web_pb.js \
|
generated/echo_grpc_web_pb.d.ts generated/echo_grpc_web_pb.js \
|
||||||
|
@ -173,12 +182,13 @@ describe('tsc test03: streamInterceptor', function() {
|
||||||
// should contain the string "[-out-]aaa".
|
// should contain the string "[-out-]aaa".
|
||||||
assert.equal('AAAAAAwKClstb3V0LV1hYWE=', xhr.body);
|
assert.equal('AAAAAAwKClstb3V0LV1hYWE=', xhr.body);
|
||||||
|
|
||||||
xhr.respond(200, {'Content-Type': 'application/grpc-web-text'},
|
xhr.respond(200, {'Content-Type': 'application/grpc-web-text',
|
||||||
// echo it back
|
'p': 'q'}, // add a piece of initial metadata
|
||||||
xhr.body);
|
// echo it back, plus a trailing metadata "x: y"
|
||||||
|
'AAAAAAwKClstb3V0LV1hYWGAAAAABng6IHkNCg==');
|
||||||
};
|
};
|
||||||
// this is the callback-based client
|
// this is the callback-based client
|
||||||
echoService.echo(req, {}, (err, response) => {
|
var call = echoService.echo(req, {}, (err, response) => {
|
||||||
assert.ifError(err);
|
assert.ifError(err);
|
||||||
// Now, the interceptor will be invoked again on receiving the response
|
// Now, the interceptor will be invoked again on receiving the response
|
||||||
// from the server. It attaches an additional "[-in-]" string in front of
|
// from the server. It attaches an additional "[-in-]" string in front of
|
||||||
|
@ -186,6 +196,20 @@ describe('tsc test03: streamInterceptor', function() {
|
||||||
assert.equal('[-in-][-out-]aaa', response.getMessage());
|
assert.equal('[-in-][-out-]aaa', response.getMessage());
|
||||||
done();
|
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();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
@ -237,16 +261,21 @@ describe('tsc test04: unaryInterceptor', function() {
|
||||||
// So by the time the proto is being sent by the underlying transport, it
|
// So by the time the proto is being sent by the underlying transport, it
|
||||||
// should contain the string "[-out-]aaa".
|
// should contain the string "[-out-]aaa".
|
||||||
assert.equal('AAAAAAwKClstb3V0LV1hYWE=', xhr.body);
|
assert.equal('AAAAAAwKClstb3V0LV1hYWE=', xhr.body);
|
||||||
xhr.respond(200, {'Content-Type': 'application/grpc-web-text'},
|
|
||||||
// echo it back
|
xhr.respond(200, {'Content-Type': 'application/grpc-web-text',
|
||||||
xhr.body);
|
'p': 'q'}, // add a piece of initial metadata
|
||||||
|
// echo it back, plus a trailing metadata "x: y"
|
||||||
|
'AAAAAAwKClstb3V0LV1hYWGAAAAABng6IHkNCg==');
|
||||||
};
|
};
|
||||||
// this is the promise-based client
|
// this is the promise-based client
|
||||||
echoService.echo(req, {}).then((response) => {
|
echoService.echo(req, {}).then((response) => {
|
||||||
// Now, the interceptor will be invoked again on receiving the response
|
// Now, the interceptor will be invoked again on receiving the response
|
||||||
// from the server. It attaches an additional "[-in-]" string in front of
|
// from the server. See the initerceptor logic in client04.ts. It
|
||||||
// the server response.
|
// flattens both the initialMetadata and the trailingMetadata, and then
|
||||||
assert.equal('[-in-][-out-]aaa', response.getMessage());
|
// 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();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue