Пример #1
0
        public void PostEntry(HttpEntityManager manager, int expectedVersion, bool requireMaster, string stream)
        {
            manager.ReadTextRequestAsync(
                (man, body) =>
            {
                var events = new Event[0];
                try
                {
                    events = AutoEventConverter.SmartParse(body, manager.RequestCodec);
                }
                catch (Exception ex)
                {
                    SendBadRequest(manager, ex.Message);
                }
                if (events.IsEmpty())
                {
                    SendBadRequest(manager, "Write request body invalid.");
                    return;
                }

                var envelope = new SendToHttpEnvelope(_networkSendQueue,
                                                      manager,
                                                      Format.WriteEventsCompleted,
                                                      (a, m) => Configure.WriteEventsCompleted(a, m, stream));
                var corrId = Guid.NewGuid();
                var msg    = new ClientMessage.WriteEvents(corrId, corrId, envelope, requireMaster,
                                                           stream, expectedVersion, events, manager.User);
                Publish(msg);
            },
                e => Log.Debug("Error while reading request (POST entry): {0}.", e.Message));
        }
Пример #2
0
        public void should_store_both_data_and_metadata_as_string_if_both_are_not_valid_xobjects_objects()
        {
            var codec   = Codec.Xml;
            var request = FakeRequest.GetXmlWrite("data", "metadata");

            var tuple     = AutoEventConverter.SmartParse(request, codec);
            var converted = tuple.Item2.Single();

            Assert.That(!converted.IsJson);
            Assert.That(ToString(converted.Data), Is.EqualTo("data"));
            Assert.That(ToString(converted.Metadata), Is.EqualTo("metadata"));
        }
Пример #3
0
        public void should_convert_data_and_metadata_to_json_if_both_are_valid_xobjects()
        {
            var codec   = Codec.Xml;
            var request = FakeRequest.GetXmlWrite(FakeRequest.XmlData, FakeRequest.XmlMetadata);

            var tuple     = AutoEventConverter.SmartParse(request, codec);
            var converted = tuple.Item2.Single();

            Assert.That(converted.IsJson);
            Assert.That(ToString(converted.Data), Is.EqualTo(FakeRequest.JsonData));
            Assert.That(ToString(converted.Metadata), Is.EqualTo(FakeRequest.JsonMetadata));
        }
Пример #4
0
        public void should_store_both_data_and_metadata_as_string_if_both_are_not_valid_json_objects()
        {
            var codec   = Codec.Json;
            var request = FakeRequest.GetJsonWrite("\"data\"", "\"metadata\"");

            var tuple = AutoEventConverter.SmartParse(request, codec);
            var evnt  = tuple.Item2.Single();

            Assert.That(!evnt.IsJson);
            Assert.That(ToString(evnt.Data), Is.EqualTo("data"));
            Assert.That(ToString(evnt.Metadata), Is.EqualTo("metadata"));
        }
Пример #5
0
        public void should_convert_data_to_json_if_its_valid_xobject_and_metadata_as_string_if_its_not()
        {
            var codec   = Codec.EventsXml;
            var request = FakeRequest.GetXmlWrite(FakeRequest.XmlData, "metadata");

            var events    = AutoEventConverter.SmartParse(request, codec);
            var converted = events.Single();

            Assert.That(converted.IsJson);
            Assert.That(ToString(converted.Data), Is.EqualTo(FakeRequest.JsonData));
            Assert.That(ToString(converted.Metadata), Is.EqualTo("metadata"));
        }
Пример #6
0
        public void should_convert_metadata_to_json_if_its_valid_xobject_and_data_as_string_if_its_not()
        {
            var codec   = Codec.EventsXml;
            var request = FakeRequest.GetXmlWrite("data", FakeRequest.XmlMetadata);

            var events    = AutoEventConverter.SmartParse(Helper.UTF8NoBom.GetBytes(request), codec, Guid.Empty);
            var converted = events.Single();

            Assert.That(converted.IsJson);
            Assert.That(converted.Data.AsString(), Is.EqualTo("data"));
            Assert.That(converted.Metadata.AsString(), Is.EqualTo(FakeRequest.JsonMetadata));
        }
Пример #7
0
        public void should_return_string_data_and_string_metadata_if_both_were_written_as_string_using_xml_events_write()
        {
            var request = FakeRequest.GetXmlWrite("data", "metadata");

            var events        = AutoEventConverter.SmartParse(request, Codec.EventsXml, Guid.Empty);
            var evnt          = events.Single();
            var resolvedEvent = GenerateResolvedEvent(evnt.Data, evnt.Metadata);
            var expected      = FakeRequest.GetXmlEventReadResult(resolvedEvent, dataJson: false, metadataJson: false);
            var converted     = AutoEventConverter.SmartFormat(resolvedEvent, Codec.EventXml);

            Assert.That(converted, Is.EqualTo(expected));
        }
Пример #8
0
        public void should_return_json_data_and_json_metadata_if_both_were_written_as_xobjects()
        {
            var request = FakeRequest.GetXmlWrite(FakeRequest.XmlData, FakeRequest.XmlMetadata);

            var events        = AutoEventConverter.SmartParse(Helper.UTF8NoBom.GetBytes(request), Codec.EventsXml, Guid.Empty);
            var evnt          = events.Single();
            var resolvedEvent = GenerateResolvedEvent(evnt.Data, evnt.Metadata);
            var expected      = FakeRequest.GetJsonEventReadResult(resolvedEvent, dataJson: true, metadataJson: true);
            var converted     = AutoEventConverter.SmartFormat(resolvedEvent, Codec.EventJson);

            Assert.That(converted, Is.EqualTo(expected));
        }
Пример #9
0
        public void should_return_json_metadata_if_metadata_was_originally_written_as_jobject_and_data_as_string()
        {
            var request = FakeRequest.GetJsonWrite("\"data\"", FakeRequest.JsonMetadata);

            var events        = AutoEventConverter.SmartParse(request, Codec.EventsJson, Guid.Empty);
            var evnt          = events.Single();
            var resolvedEvent = GenerateResolvedEvent(evnt.Data, evnt.Metadata);
            var expected      = FakeRequest.GetJsonEventReadResult(resolvedEvent, dataJson: false, metadataJson: true);
            var converted     = AutoEventConverter.SmartFormat(resolvedEvent, Codec.EventJson);

            Assert.That(converted, Is.EqualTo(expected));
        }
Пример #10
0
        public void should_store_both_data_and_metadata_as_string_if_both_are_not_valid_xobjects_objects()
        {
            var codec   = Codec.EventsXml;
            var request = FakeRequest.GetXmlWrite("data", "metadata");

            var events    = AutoEventConverter.SmartParse(request, codec, Guid.Empty);
            var converted = events.Single();

            Assert.That(!converted.IsJson);
            Assert.That(converted.Data.AsString(), Is.EqualTo("data"));
            Assert.That(converted.Metadata.AsString(), Is.EqualTo("metadata"));
        }
Пример #11
0
        public void should_return_xml_data_if_data_was_originally_written_as_xobject_and_metadata_as_string()
        {
            var request = FakeRequest.GetXmlWrite(FakeRequest.XmlData, "metadata");

            var events        = AutoEventConverter.SmartParse(request, Codec.EventsXml, Guid.Empty);
            var evnt          = events.Single();
            var resolvedEvent = GenerateResolvedEvent(evnt.Data, evnt.Metadata);
            var expected      = FakeRequest.GetXmlEventReadResult(resolvedEvent, dataJson: true, metadataJson: false);
            var converted     = AutoEventConverter.SmartFormat(resolvedEvent, Codec.EventXml);

            Assert.That(converted, Is.EqualTo(expected));
        }
Пример #12
0
        public void should_return_xml_data_and_xml_metadata_if_both_were_written_as_jobjects()
        {
            var request = FakeRequest.GetJsonWrite(FakeRequest.JsonData, FakeRequest.JsonMetadata);

            var events        = AutoEventConverter.SmartParse(request, Codec.EventsJson, Guid.Empty);
            var evnt          = events.Single();
            var resolvedEvent = GenerateResolvedEvent(evnt.Data, evnt.Metadata);
            var expected      = FakeRequest.GetXmlEventReadResult(resolvedEvent, dataJson: true, metadataJson: true);
            var converted     = AutoEventConverter.SmartFormat(resolvedEvent, Codec.EventXml);

            Assert.That(converted, Is.EqualTo(expected));
        }
Пример #13
0
        public void should_return_string_data_and_string_metadata_if_both_were_written_as_string_using_json_write()
        {
            var request = FakeRequest.GetJsonWrite("\"data\"", "\"metadata\"");

            var events        = AutoEventConverter.SmartParse(Helper.UTF8NoBom.GetBytes(request), Codec.EventsJson, Guid.Empty);
            var evnt          = events.Single();
            var resolvedEvent = GenerateResolvedEvent(evnt.Data, evnt.Metadata);
            var expected      = FakeRequest.GetJsonEventReadResult(resolvedEvent, dataJson: false, metadataJson: false);
            var converted     = AutoEventConverter.SmartFormat(resolvedEvent, Codec.EventJson);

            Assert.That(converted, Is.EqualTo(expected));
        }
Пример #14
0
        public void should_convert_data_and_metadata_to_json_if_both_are_valid_xobjects()
        {
            var codec   = Codec.EventsXml;
            var request = FakeRequest.GetXmlWrite(FakeRequest.XmlData, FakeRequest.XmlMetadata);

            var events    = AutoEventConverter.SmartParse(request, codec, Guid.Empty);
            var converted = events.Single();

            Assert.That(converted.IsJson);
            Assert.That(converted.Data.AsString(), Is.EqualTo(FakeRequest.JsonData));
            Assert.That(converted.Metadata.AsString(), Is.EqualTo(FakeRequest.JsonMetadata));
        }
Пример #15
0
        public void should_return_string_data_and_string_metadata_if_both_were_written_as_string_using_xml_write()
        {
            var request = FakeRequest.GetXmlWrite("data", "metadata");

            var written = AutoEventConverter.SmartParse(request, Codec.Xml);
            var evnt    = written.Item2.Single();

            var readCompleted = GenereteReadCompleted(evnt.Data, evnt.Metadata);

            var expected  = FakeRequest.GetXmlReadResult(readCompleted, dataJson: false, metadataJson: false);
            var converted = AutoEventConverter.SmartFormat(readCompleted, Codec.Xml);

            Assert.That(converted, Is.EqualTo(expected));
        }
Пример #16
0
        public void should_return_json_metadata_if_metadata_was_originally_written_as_jobject_and_data_as_string()
        {
            var request = FakeRequest.GetJsonWrite("\"data\"", FakeRequest.JsonMetadata);

            var written = AutoEventConverter.SmartParse(request, Codec.Json);
            var evnt    = written.Item2.Single();

            var readCompleted = GenereteReadCompleted(evnt.Data, evnt.Metadata);

            var expected  = FakeRequest.GetJsonReadResult(readCompleted, dataJson: false, metadataJson: true);
            var converted = AutoEventConverter.SmartFormat(readCompleted, Codec.Json);

            Assert.That(converted, Is.EqualTo(expected));
        }
Пример #17
0
        public void should_return_xml_data_and_xml_metadata_if_both_were_written_as_jobjects()
        {
            var request = FakeRequest.GetJsonWrite(FakeRequest.JsonData, FakeRequest.JsonMetadata);

            var written = AutoEventConverter.SmartParse(request, Codec.Json);
            var evnt    = written.Item2.Single();

            var readCompleted = GenereteReadCompleted(evnt.Data, evnt.Metadata);

            var expected  = FakeRequest.GetXmlReadResult(readCompleted, dataJson: true, metadataJson: true);
            var converted = AutoEventConverter.SmartFormat(readCompleted, Codec.Xml);

            Assert.That(converted, Is.EqualTo(expected));
        }
Пример #18
0
        public void should_just_count_as_body_if_just_json()
        {
            var codec   = Codec.Json;
            var request = FakeRequest.JsonData;
            var id      = Guid.NewGuid();
            var type    = "EventType";
            var events  = AutoEventConverter.SmartParse(Helper.UTF8NoBom.GetBytes(request), codec, id, type);

            Assert.NotNull(events);
            Assert.That(events.Length, Is.EqualTo(1));

            Assert.IsTrue(events[0].IsJson);
            Assert.AreEqual(events[0].EventId, id);
            Assert.AreEqual(events[0].EventType, type);
            Assert.That(events[0].Data.AsString(), Is.EqualTo(FakeRequest.JsonData));
            Assert.That(events[0].Metadata.AsString(), Is.EqualTo(string.Empty));
        }
Пример #19
0
        public void should_just_count_as_body_if_just_xml()
        {
            var codec   = Codec.Xml;
            var request = FakeRequest.XmlData;
            var id      = Guid.NewGuid();
            var type    = "EventType";
            var events  = AutoEventConverter.SmartParse(request, codec, id, type);

            Assert.NotNull(events);
            Assert.That(events.Length, Is.EqualTo(1));

            Assert.IsFalse(events[0].IsJson);
            Assert.AreEqual(events[0].EventId, id);
            Assert.AreEqual(events[0].EventType, type);
            Assert.That(events[0].Data.AsString(), Is.EqualTo(FakeRequest.XmlData));
            Assert.That(events[0].Metadata.AsString(), Is.EqualTo(string.Empty));
        }
Пример #20
0
        public void should_do_its_best_at_preserving_data_format_with_multiple_events()
        {
            var codec   = Codec.EventsXml;
            var request = FakeRequest.GetXmlWrite(new[] {
                Tuple.Create(FakeRequest.XmlData, FakeRequest.XmlMetadata),
                Tuple.Create("data2", "metadata2")
            });

            var events = AutoEventConverter.SmartParse(Helper.UTF8NoBom.GetBytes(request), codec, Guid.Empty);

            Assert.That(events.Length, Is.EqualTo(2));

            Assert.IsTrue(events[0].IsJson);
            Assert.That(events[0].Data.AsString(), Is.EqualTo(FakeRequest.JsonData));
            Assert.That(events[0].Metadata.AsString(), Is.EqualTo(FakeRequest.JsonMetadata));

            Assert.IsFalse(events[1].IsJson);
            Assert.That(events[1].Data.AsString(), Is.EqualTo("data2"));
            Assert.That(events[1].Metadata.AsString(), Is.EqualTo("metadata2"));
        }
Пример #21
0
        public void should_store_both_data_and_metadata_as_string_if_both_are_not_valid_json_objects()
        {
            var codec   = Codec.EventsJson;
            var request = FakeRequest.GetJsonWrite(new[] {
                Tuple.Create("\"data\"", "\"metadata\""),
                Tuple.Create("\"data2\"", "\"metadata2\"")
            });

            var events = AutoEventConverter.SmartParse(Helper.UTF8NoBom.GetBytes(request), codec, Guid.Empty);

            Assert.That(events.Length, Is.EqualTo(2));

            Assert.IsFalse(events[0].IsJson);
            Assert.That(events[0].Data.AsString(), Is.EqualTo("data"));
            Assert.That(events[0].Metadata.AsString(), Is.EqualTo("metadata"));

            Assert.IsFalse(events[1].IsJson);
            Assert.That(events[1].Data.AsString(), Is.EqualTo("data2"));
            Assert.That(events[1].Metadata.AsString(), Is.EqualTo("metadata2"));
        }
Пример #22
0
        private void OnPostEntryRequestRead(HttpEntityManager manager, string body)
        {
            var entity = manager.HttpEntity;
            var stream = (string)manager.AsyncState;

            var parsed          = AutoEventConverter.SmartParse(body, entity.RequestCodec);
            var expectedVersion = parsed.Item1;
            var events          = parsed.Item2;

            if (events == null || events.Length == 0)
            {
                SendBadRequest(entity, "Write request body invalid");
                return;
            }

            var envelope = new SendToHttpEnvelope(entity, Format.WriteEventsCompleted, Configure.WriteEventsCompleted);
            var msg      = new ClientMessage.WriteEvents(Guid.NewGuid(), envelope, true, stream, expectedVersion, events);

            Publish(msg);
        }
Пример #23
0
        public void should_do_its_best_at_preserving_data_format_with_multiple_events()
        {
            var codec   = Codec.EventsJson;
            var request = FakeRequest.GetJsonWrite(new[]
            {
                Tuple.Create(FakeRequest.JsonData, FakeRequest.JsonMetadata),
                Tuple.Create("\"data2\"", "\"metadata2\"")
            });

            var events = AutoEventConverter.SmartParse(request, codec);

            Assert.That(events.Length, Is.EqualTo(2));

            Assert.IsTrue(events[0].IsJson);
            Assert.That(ToString(events[0].Data), Is.EqualTo(FakeRequest.JsonData));
            Assert.That(ToString(events[0].Metadata), Is.EqualTo(FakeRequest.JsonMetadata));

            Assert.IsFalse(events[1].IsJson);
            Assert.That(ToString(events[1].Data), Is.EqualTo("data2"));
            Assert.That(ToString(events[1].Metadata), Is.EqualTo("metadata2"));
        }
Пример #24
0
        public void should_store_both_data_and_metadata_as_json_if_both_are_valid_json_objects()
        {
            var codec   = Codec.EventsJson;
            var request = FakeRequest.GetJsonWrite(new[]
            {
                Tuple.Create(FakeRequest.JsonData, FakeRequest.JsonMetadata),
                Tuple.Create(FakeRequest.JsonData2, FakeRequest.JsonMetadata2)
            });

            var events = AutoEventConverter.SmartParse(request, codec, Guid.Empty);

            Assert.That(events.Length, Is.EqualTo(2));

            Assert.IsTrue(events[0].IsJson);
            Assert.That(events[0].Data.AsString(), Is.EqualTo(FakeRequest.JsonData));
            Assert.That(events[0].Metadata.AsString(), Is.EqualTo(FakeRequest.JsonMetadata));

            Assert.IsTrue(events[1].IsJson);
            Assert.That(events[1].Data.AsString(), Is.EqualTo(FakeRequest.JsonData2));
            Assert.That(events[1].Metadata.AsString(), Is.EqualTo(FakeRequest.JsonMetadata2));
        }
Пример #25
0
        public void should_store_metadata_as_json_if_its_valid_and_data_as_string_if_its_not()
        {
            var codec   = Codec.EventsJson;
            var request = FakeRequest.GetJsonWrite(new[]
            {
                Tuple.Create("\"data\"", FakeRequest.JsonMetadata),
                Tuple.Create("\"data2\"", FakeRequest.JsonMetadata2)
            });

            var events = AutoEventConverter.SmartParse(request, codec);

            Assert.That(events.Length, Is.EqualTo(2));

            Assert.IsTrue(events[0].IsJson);
            Assert.That(ToString(events[0].Data), Is.EqualTo("data"));
            Assert.That(ToString(events[0].Metadata), Is.EqualTo(FakeRequest.JsonMetadata));

            Assert.IsTrue(events[1].IsJson);
            Assert.That(ToString(events[1].Data), Is.EqualTo("data2"));
            Assert.That(ToString(events[1].Metadata), Is.EqualTo(FakeRequest.JsonMetadata2));
        }
Пример #26
0
 public void PostEntry(HttpEntityManager manager, int expectedVersion, bool requireMaster, string stream, Guid idIncluded, string typeIncluded)
 {
     //TODO GFY SHOULD WE MAKE THIS READ BYTE[] FOR RAW THEN CONVERT? AS OF NOW ITS ALL NO BOM UTF8
     manager.ReadTextRequestAsync(
         (man, body) =>
     {
         var events = new Event[0];
         try
         {
             events = AutoEventConverter.SmartParse(body, manager.RequestCodec, idIncluded, typeIncluded);
         }
         catch (Exception ex)
         {
             SendBadRequest(manager, ex.Message);
             return;
         }
         if (events.IsEmpty())
         {
             SendBadRequest(manager, "Write request body invalid.");
             return;
         }
         foreach (var e in events)
         {
             if (e.Data.Length + e.Metadata.Length > 4 * 1024 * 1024)
             {
                 SendTooBig(manager);
             }
         }
         var envelope = new SendToHttpEnvelope(_networkSendQueue,
                                               manager,
                                               Format.WriteEventsCompleted,
                                               (a, m) => Configure.WriteEventsCompleted(a, m, stream));
         var corrId = Guid.NewGuid();
         var msg    = new ClientMessage.WriteEvents(corrId, corrId, envelope, requireMaster,
                                                    stream, expectedVersion, events, manager.User);
         Publish(msg);
     },
         e => Log.Debug("Error while reading request (POST entry): {0}.", e.Message));
 }