private void WriteType0Section(BsonBinaryWriter writer, Type0CommandMessageSection section)
        {
            var serializer = section.DocumentSerializer;
            var context    = BsonSerializationContext.CreateRoot(writer);

            serializer.Serialize(context, section.Document);
        }
        public void WriteMessage_should_invoke_encoding_post_processor()
        {
            var stream   = new MemoryStream();
            var subject  = CreateSubject(stream);
            var command  = BsonDocument.Parse("{ command : \"x\", writeConcern : { w : 0 } }");
            var section  = new Type0CommandMessageSection <BsonDocument>(command, BsonDocumentSerializer.Instance);
            var sections = new CommandMessageSection[] { section };
            var message  = CreateMessage(sections: sections, moreToCome: true);

            message.PostWriteAction = encoder =>
            {
                encoder.ChangeWriteConcernFromW0ToW1();
            };

            subject.WriteMessage(message);

            stream.Position = 0;
            var rehydratedMessage = subject.ReadMessage();

            rehydratedMessage.MoreToCome.Should().BeFalse();
            var rehydratedType0Section = rehydratedMessage.Sections.OfType <Type0CommandMessageSection <RawBsonDocument> >().Single();
            var rehyrdatedCommand      = rehydratedType0Section.Document;

            rehyrdatedCommand["writeConcern"]["w"].Should().Be(1);

            // assert that the original message was altered only as expected
            message.MoreToCome.Should().BeFalse(); // was true before PostWriteAction
            var originalCommand = message.Sections.OfType <Type0CommandMessageSection <BsonDocument> >().Single().Document;

            originalCommand["writeConcern"]["w"].Should().Be(0); // unchanged
        }
 private BsonDocument CreateType0SectionDocument(Type0CommandMessageSection <BsonDocument> section)
 {
     return(new BsonDocument
     {
         { "payloadType", 0 },
         { "document", section.Document }
     });
 }
        private void WriteType0Section(IBsonWriter writer, Type0CommandMessageSection section)
        {
            writer.WriteName("document");
            var serializer = section.DocumentSerializer;
            var context    = BsonSerializationContext.CreateRoot(writer);

            serializer.Serialize(context, section.Document);
        }
 private byte[] CreateType0SectionBytes(Type0CommandMessageSection <BsonDocument> section)
 {
     using (var memoryStream = new MemoryStream())
         using (var writer = new BsonBinaryWriter(memoryStream))
         {
             memoryStream.WriteByte(0);
             var context = BsonSerializationContext.CreateRoot(writer);
             BsonDocumentSerializer.Instance.Serialize(context, section.Document);
             return(memoryStream.ToArray());
         }
 }