aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/node/test
diff options
context:
space:
mode:
authorGravatar Tim Emiola <tbetbetbe@users.noreply.github.com>2015-08-28 11:39:27 -0700
committerGravatar Tim Emiola <tbetbetbe@users.noreply.github.com>2015-08-28 11:39:27 -0700
commit956e411e31c97836702aac5675e9f509b2231426 (patch)
tree4d1c7413b6d568ded5d20801c43605ba52764440 /src/node/test
parent9b1f91e7eac88ba477871a60912cc3b6ed3380ed (diff)
parent4ad400341e624947abbd08dd8ab8f06a6610b7e5 (diff)
Merge pull request #3012 from murgatroid99/node_metadata_class
Replace metadata objects with a Metadata class
Diffstat (limited to 'src/node/test')
-rw-r--r--src/node/test/metadata_test.js193
-rw-r--r--src/node/test/surface_test.js62
2 files changed, 226 insertions, 29 deletions
diff --git a/src/node/test/metadata_test.js b/src/node/test/metadata_test.js
new file mode 100644
index 0000000000..86383f1bad
--- /dev/null
+++ b/src/node/test/metadata_test.js
@@ -0,0 +1,193 @@
+/*
+ *
+ * Copyright 2015, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+'use strict';
+
+var Metadata = require('../src/metadata.js');
+
+var assert = require('assert');
+
+describe('Metadata', function() {
+ var metadata;
+ beforeEach(function() {
+ metadata = new Metadata();
+ });
+ describe('#set', function() {
+ it('Only accepts string values for non "-bin" keys', function() {
+ assert.throws(function() {
+ metadata.set('key', new Buffer('value'));
+ });
+ assert.doesNotThrow(function() {
+ metadata.set('key', 'value');
+ });
+ });
+ it('Only accepts Buffer values for "-bin" keys', function() {
+ assert.throws(function() {
+ metadata.set('key-bin', 'value');
+ });
+ assert.doesNotThrow(function() {
+ metadata.set('key-bin', new Buffer('value'));
+ });
+ });
+ it('Rejects invalid keys', function() {
+ assert.throws(function() {
+ metadata.set('key$', 'value');
+ });
+ assert.throws(function() {
+ metadata.set('', 'value');
+ });
+ });
+ it('Rejects values with non-ASCII characters', function() {
+ assert.throws(function() {
+ metadata.set('key', 'résumé');
+ });
+ });
+ it('Saves values that can be retrieved', function() {
+ metadata.set('key', 'value');
+ assert.deepEqual(metadata.get('key'), ['value']);
+ });
+ it('Overwrites previous values', function() {
+ metadata.set('key', 'value1');
+ metadata.set('key', 'value2');
+ assert.deepEqual(metadata.get('key'), ['value2']);
+ });
+ it('Normalizes keys', function() {
+ metadata.set('Key', 'value1');
+ assert.deepEqual(metadata.get('key'), ['value1']);
+ metadata.set('KEY', 'value2');
+ assert.deepEqual(metadata.get('key'), ['value2']);
+ });
+ });
+ describe('#add', function() {
+ it('Only accepts string values for non "-bin" keys', function() {
+ assert.throws(function() {
+ metadata.add('key', new Buffer('value'));
+ });
+ assert.doesNotThrow(function() {
+ metadata.add('key', 'value');
+ });
+ });
+ it('Only accepts Buffer values for "-bin" keys', function() {
+ assert.throws(function() {
+ metadata.add('key-bin', 'value');
+ });
+ assert.doesNotThrow(function() {
+ metadata.add('key-bin', new Buffer('value'));
+ });
+ });
+ it('Rejects invalid keys', function() {
+ assert.throws(function() {
+ metadata.add('key$', 'value');
+ });
+ assert.throws(function() {
+ metadata.add('', 'value');
+ });
+ });
+ it('Saves values that can be retrieved', function() {
+ metadata.add('key', 'value');
+ assert.deepEqual(metadata.get('key'), ['value']);
+ });
+ it('Combines with previous values', function() {
+ metadata.add('key', 'value1');
+ metadata.add('key', 'value2');
+ assert.deepEqual(metadata.get('key'), ['value1', 'value2']);
+ });
+ it('Normalizes keys', function() {
+ metadata.add('Key', 'value1');
+ assert.deepEqual(metadata.get('key'), ['value1']);
+ metadata.add('KEY', 'value2');
+ assert.deepEqual(metadata.get('key'), ['value1', 'value2']);
+ });
+ });
+ describe('#remove', function() {
+ it('clears values from a key', function() {
+ metadata.add('key', 'value');
+ metadata.remove('key');
+ assert.deepEqual(metadata.get('key'), []);
+ });
+ it('Normalizes keys', function() {
+ metadata.add('key', 'value');
+ metadata.remove('KEY');
+ assert.deepEqual(metadata.get('key'), []);
+ });
+ });
+ describe('#get', function() {
+ beforeEach(function() {
+ metadata.add('key', 'value1');
+ metadata.add('key', 'value2');
+ metadata.add('key-bin', new Buffer('value'));
+ });
+ it('gets all values associated with a key', function() {
+ assert.deepEqual(metadata.get('key'), ['value1', 'value2']);
+ });
+ it('Normalizes keys', function() {
+ assert.deepEqual(metadata.get('KEY'), ['value1', 'value2']);
+ });
+ it('returns an empty list for non-existent keys', function() {
+ assert.deepEqual(metadata.get('non-existent-key'), []);
+ });
+ it('returns Buffers for "-bin" keys', function() {
+ assert(metadata.get('key-bin')[0] instanceof Buffer);
+ });
+ });
+ describe('#getMap', function() {
+ it('gets a map of keys to values', function() {
+ metadata.add('key1', 'value1');
+ metadata.add('Key2', 'value2');
+ metadata.add('KEY3', 'value3');
+ assert.deepEqual(metadata.getMap(),
+ {key1: 'value1',
+ key2: 'value2',
+ key3: 'value3'});
+ });
+ });
+ describe('#clone', function() {
+ it('retains values from the original', function() {
+ metadata.add('key', 'value');
+ var copy = metadata.clone();
+ assert.deepEqual(copy.get('key'), ['value']);
+ });
+ it('Does not see newly added values', function() {
+ metadata.add('key', 'value1');
+ var copy = metadata.clone();
+ metadata.add('key', 'value2');
+ assert.deepEqual(copy.get('key'), ['value1']);
+ });
+ it('Does not add new values to the original', function() {
+ metadata.add('key', 'value1');
+ var copy = metadata.clone();
+ copy.add('key', 'value2');
+ assert.deepEqual(metadata.get('key'), ['value1']);
+ });
+ });
+});
diff --git a/src/node/test/surface_test.js b/src/node/test/surface_test.js
index 6e45871fc8..7c2a8d7258 100644
--- a/src/node/test/surface_test.js
+++ b/src/node/test/surface_test.js
@@ -262,6 +262,7 @@ describe('Generic client and server', function() {
describe('Echo metadata', function() {
var client;
var server;
+ var metadata;
before(function() {
var test_proto = ProtoBuf.loadProtoFile(__dirname + '/test_service.proto');
var test_service = test_proto.lookup('TestService');
@@ -294,6 +295,8 @@ describe('Echo metadata', function() {
var Client = surface_client.makeProtobufClientConstructor(test_service);
client = new Client('localhost:' + port, grpc.Credentials.createInsecure());
server.start();
+ metadata = new grpc.Metadata();
+ metadata.set('key', 'value');
});
after(function() {
server.forceShutdown();
@@ -301,35 +304,35 @@ describe('Echo metadata', function() {
it('with unary call', function(done) {
var call = client.unary({}, function(err, data) {
assert.ifError(err);
- }, {key: ['value']});
+ }, metadata);
call.on('metadata', function(metadata) {
- assert.deepEqual(metadata.key, ['value']);
+ assert.deepEqual(metadata.get('key'), ['value']);
done();
});
});
it('with client stream call', function(done) {
var call = client.clientStream(function(err, data) {
assert.ifError(err);
- }, {key: ['value']});
+ }, metadata);
call.on('metadata', function(metadata) {
- assert.deepEqual(metadata.key, ['value']);
+ assert.deepEqual(metadata.get('key'), ['value']);
done();
});
call.end();
});
it('with server stream call', function(done) {
- var call = client.serverStream({}, {key: ['value']});
+ var call = client.serverStream({}, metadata);
call.on('data', function() {});
call.on('metadata', function(metadata) {
- assert.deepEqual(metadata.key, ['value']);
+ assert.deepEqual(metadata.get('key'), ['value']);
done();
});
});
it('with bidi stream call', function(done) {
- var call = client.bidiStream({key: ['value']});
+ var call = client.bidiStream(metadata);
call.on('data', function() {});
call.on('metadata', function(metadata) {
- assert.deepEqual(metadata.key, ['value']);
+ assert.deepEqual(metadata.get('key'), ['value']);
done();
});
call.end();
@@ -337,9 +340,10 @@ describe('Echo metadata', function() {
it('shows the correct user-agent string', function(done) {
var version = require('../package.json').version;
var call = client.unary({}, function(err, data) { assert.ifError(err); },
- {key: ['value']});
+ metadata);
call.on('metadata', function(metadata) {
- assert(_.startsWith(metadata['user-agent'], 'grpc-node/' + version));
+ assert(_.startsWith(metadata.get('user-agent')[0],
+ 'grpc-node/' + version));
done();
});
});
@@ -354,13 +358,15 @@ describe('Other conditions', function() {
var test_proto = ProtoBuf.loadProtoFile(__dirname + '/test_service.proto');
test_service = test_proto.lookup('TestService');
server = new grpc.Server();
+ var trailer_metadata = new grpc.Metadata();
+ trailer_metadata.add('trailer-present', 'yes');
server.addProtoService(test_service, {
unary: function(call, cb) {
var req = call.request;
if (req.error) {
- cb(new Error('Requested error'), null, {trailer_present: ['yes']});
+ cb(new Error('Requested error'), null, trailer_metadata);
} else {
- cb(null, {count: 1}, {trailer_present: ['yes']});
+ cb(null, {count: 1}, trailer_metadata);
}
},
clientStream: function(stream, cb){
@@ -369,14 +375,14 @@ describe('Other conditions', function() {
stream.on('data', function(data) {
if (data.error) {
errored = true;
- cb(new Error('Requested error'), null, {trailer_present: ['yes']});
+ cb(new Error('Requested error'), null, trailer_metadata);
} else {
count += 1;
}
});
stream.on('end', function() {
if (!errored) {
- cb(null, {count: count}, {trailer_present: ['yes']});
+ cb(null, {count: count}, trailer_metadata);
}
});
},
@@ -384,13 +390,13 @@ describe('Other conditions', function() {
var req = stream.request;
if (req.error) {
var err = new Error('Requested error');
- err.metadata = {trailer_present: ['yes']};
+ err.metadata = trailer_metadata;
stream.emit('error', err);
} else {
for (var i = 0; i < 5; i++) {
stream.write({count: i});
}
- stream.end({trailer_present: ['yes']});
+ stream.end(trailer_metadata);
}
},
bidiStream: function(stream) {
@@ -398,10 +404,8 @@ describe('Other conditions', function() {
stream.on('data', function(data) {
if (data.error) {
var err = new Error('Requested error');
- err.metadata = {
- trailer_present: ['yes'],
- count: ['' + count]
- };
+ err.metadata = trailer_metadata.clone();
+ err.metadata.add('count', '' + count);
stream.emit('error', err);
} else {
stream.write({count: count});
@@ -409,7 +413,7 @@ describe('Other conditions', function() {
}
});
stream.on('end', function() {
- stream.end({trailer_present: ['yes']});
+ stream.end(trailer_metadata);
});
}
});
@@ -510,7 +514,7 @@ describe('Other conditions', function() {
assert.ifError(err);
});
call.on('status', function(status) {
- assert.deepEqual(status.metadata.trailer_present, ['yes']);
+ assert.deepEqual(status.metadata.get('trailer-present'), ['yes']);
done();
});
});
@@ -519,7 +523,7 @@ describe('Other conditions', function() {
assert(err);
});
call.on('status', function(status) {
- assert.deepEqual(status.metadata.trailer_present, ['yes']);
+ assert.deepEqual(status.metadata.get('trailer-present'), ['yes']);
done();
});
});
@@ -531,7 +535,7 @@ describe('Other conditions', function() {
call.write({error: false});
call.end();
call.on('status', function(status) {
- assert.deepEqual(status.metadata.trailer_present, ['yes']);
+ assert.deepEqual(status.metadata.get('trailer-present'), ['yes']);
done();
});
});
@@ -543,7 +547,7 @@ describe('Other conditions', function() {
call.write({error: true});
call.end();
call.on('status', function(status) {
- assert.deepEqual(status.metadata.trailer_present, ['yes']);
+ assert.deepEqual(status.metadata.get('trailer-present'), ['yes']);
done();
});
});
@@ -552,7 +556,7 @@ describe('Other conditions', function() {
call.on('data', function(){});
call.on('status', function(status) {
assert.strictEqual(status.code, grpc.status.OK);
- assert.deepEqual(status.metadata.trailer_present, ['yes']);
+ assert.deepEqual(status.metadata.get('trailer-present'), ['yes']);
done();
});
});
@@ -560,7 +564,7 @@ describe('Other conditions', function() {
var call = client.serverStream({error: true});
call.on('data', function(){});
call.on('error', function(error) {
- assert.deepEqual(error.metadata.trailer_present, ['yes']);
+ assert.deepEqual(error.metadata.get('trailer-present'), ['yes']);
done();
});
});
@@ -572,7 +576,7 @@ describe('Other conditions', function() {
call.on('data', function(){});
call.on('status', function(status) {
assert.strictEqual(status.code, grpc.status.OK);
- assert.deepEqual(status.metadata.trailer_present, ['yes']);
+ assert.deepEqual(status.metadata.get('trailer-present'), ['yes']);
done();
});
});
@@ -583,7 +587,7 @@ describe('Other conditions', function() {
call.end();
call.on('data', function(){});
call.on('error', function(error) {
- assert.deepEqual(error.metadata.trailer_present, ['yes']);
+ assert.deepEqual(error.metadata.get('trailer-present'), ['yes']);
done();
});
});