github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/sdk/javascript/spec/protobuf/round_trip_test.js (about) 1 /** 2 * Copyright 2016 Intel Corporation 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * ------------------------------------------------------------------------------ 16 */ 17 18 'use strict' 19 20 const assert = require('assert') 21 22 const { Message } = require('../../protobuf') 23 24 describe('ProtoBuf', () => { 25 describe('Message', () => { 26 it('should correctly round trip with complete fields', () => { 27 let encMessage = Message.encode({ 28 messageType: Message.MessageType.CLIENT_STATE_GET_REQUEST, 29 correlationId: 'corr_id', 30 content: Buffer.from('Hello', 'utf8') 31 }).finish() 32 33 let decMessage = Message.decode(encMessage) 34 35 assert.equal( 36 Message.MessageType.CLIENT_STATE_GET_REQUEST, 37 decMessage.messageType 38 ) 39 assert.equal('corr_id', decMessage.correlationId) 40 assert.equal('Hello', decMessage.content.toString('utf8')) 41 }) 42 43 it('should correctly round trip with partial fields', () => { 44 let encMessage = Message.encode({ 45 messageType: Message.MessageType.CLIENT_STATE_GET_RESPONSE, 46 content: Buffer.from('Hello', 'utf8') 47 }).finish() 48 49 let decMessage = Message.decode(encMessage) 50 51 assert.equal( 52 Message.MessageType.CLIENT_STATE_GET_RESPONSE, 53 decMessage.messageType 54 ) 55 assert.equal('Hello', decMessage.content.toString('utf8')) 56 assert.ok(!decMessage.correlationId) 57 }) 58 }) 59 })