Redecode unknown protobuf fields before writing

This commit is contained in:
Fedor Indutny 2021-07-28 16:44:58 -07:00 committed by GitHub
parent efe5d86424
commit 6e4a3561f1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 7 deletions

View file

@ -88,8 +88,31 @@ describe('Proto#__unknownFields', () => {
const decodedConcat = Full.decode(concat);
assert.strictEqual(decodedConcat.a, 'hello');
assert.strictEqual(decodedConcat.b, true);
assert.isTrue(decodedConcat.b);
assert.strictEqual(decodedConcat.c, 42);
assert.strictEqual(Buffer.from(decodedConcat.d).toString(), 'ohai');
});
it('should decode unknown fields before reencoding them', () => {
const full = Full.encode({
a: 'hello',
b: true,
c: 42,
d: Buffer.from('ohai'),
}).finish();
const partial = Partial.decode(full);
assert.isUndefined(partial.b);
const encoded = Full.encode({
...partial,
b: false,
}).finish();
const decoded = Full.decode(encoded);
assert.strictEqual(decoded.a, 'hello');
assert.isFalse(decoded.b);
assert.strictEqual(decoded.c, 42);
assert.strictEqual(Buffer.from(decoded.d).toString(), 'ohai');
});
});