public List <ImapResponse> Render(MailDb maildb, FetchMessage message, DataItem dataItem) { var flags = maildb.Messages.GetFlags(message.Message.Id); var result = new StringBuilder() .Append(dataItem.FullItemName) .Append(" ("); for (int i = 0; i < flags.Length; i++) { if (i > 0) { result.Append(" "); } result.Append("\\").Append(flags[i]); } result.Append(")"); return(new List <ImapResponse> { new ImapResponse(result.ToString()) }); }
public bool Fetch(FetchMessage message) { throw new NotImplementedException(); }
public void TestFetchWithDecodeError() { // Prepare var serializer = new Mock <Node.ISerialization>(); var connection = new Mock <IConnection>(); var node = new Node("Node", () => connection.Object, serializer.Object, new Configuration { TaskScheduler = new CurrentThreadTaskScheduler(), ConsumeBatchSize = 1 }, 1); var ev = new ManualResetEvent(false); var corrs = new Queue <int>(); connection.Setup(c => c.SendAsync(It.IsAny <int>(), It.IsAny <ReusableMemoryStream>(), It.IsAny <bool>())) .Returns((int c, ReusableMemoryStream d, bool a) => { corrs.Enqueue(c); return(Success); }) .Callback(() => ev.Set()); connection.Setup(c => c.ConnectAsync()).Returns(Success); var message = new FetchMessage { Topic = "balbuzzard", Offset = 42, Partition = 1, MaxBytes = 1242 }; node.Fetch(message); ev.WaitOne(); Assert.AreEqual(1, corrs.Count); int corr = corrs.Dequeue(); node.ResponseReceived += (n, l) => { Assert.AreSame(node, n); }; var acknowledgement = new CommonAcknowledgement <FetchResponse>(); node.FetchAcknowledgement += (n, ack) => { acknowledgement = ack; Assert.AreSame(node, n); }; int decodeError = 0; node.DecodeError += (n, e) => ++ decodeError; serializer.Setup(s => s.DeserializeResponse <FetchResponse>(corr, It.IsAny <ReusableMemoryStream>(), It.IsAny <Basics.ApiVersion>())) .Throws(new Exception()); // Now send a response connection.Raise(c => c.Response += null, connection.Object, corr, new ReusableMemoryStream(null)); // Checks Assert.AreEqual(1, decodeError); Assert.AreNotEqual(default(DateTime), acknowledgement.ReceivedDate); Assert.AreEqual(1, acknowledgement.Response.FetchPartitionResponse.TopicsResponse.Length); Assert.AreEqual("balbuzzard", acknowledgement.Response.FetchPartitionResponse.TopicsResponse[0].TopicName); Assert.AreEqual(1, acknowledgement.Response.FetchPartitionResponse.TopicsResponse[0].PartitionsData.Count()); var fetch = acknowledgement.Response.FetchPartitionResponse.TopicsResponse[0].PartitionsData.First(); Assert.AreEqual(ErrorCode.LocalError, fetch.ErrorCode); Assert.AreEqual(-1, fetch.HighWatermarkOffset); Assert.AreEqual(1, fetch.Partition); Assert.AreEqual(0, fetch.Messages.Count()); }
public void TestFetchWithNoErrors(Basics.ApiVersion version) { // Prepare var serializer = new Mock <Node.ISerialization>(); var connection = new Mock <IConnection>(); var node = new Node("Node", () => connection.Object, serializer.Object, new Configuration { TaskScheduler = new CurrentThreadTaskScheduler(), ConsumeBatchSize = 1, Compatibility = version == Basics.ApiVersion.V0 ? Compatibility.V0_8_2 : Compatibility.V0_10_1 }, 1); var ev = new ManualResetEvent(false); var corrs = new Queue <int>(); connection.Setup(c => c.SendAsync(It.IsAny <int>(), It.IsAny <ReusableMemoryStream>(), It.IsAny <bool>())) .Returns((int c, ReusableMemoryStream d, bool a) => { corrs.Enqueue(c); return(Success); }) .Callback(() => ev.Set()); connection.Setup(c => c.ConnectAsync()).Returns(Success); var message = new FetchMessage { Topic = "balbuzzard", Offset = 42, Partition = 1, MaxBytes = 1242 }; node.Fetch(message); ev.WaitOne(); Assert.AreEqual(1, corrs.Count); int corr = corrs.Dequeue(); int response = 0; node.ResponseReceived += (n, l) => { Assert.AreSame(node, n); ++response; }; var acknowledgement = new CommonAcknowledgement <FetchResponse>(); node.FetchAcknowledgement += (n, ack) => { acknowledgement = ack; Assert.AreSame(node, n); }; serializer.Setup(s => s.DeserializeResponse <FetchResponse>(corr, It.IsAny <ReusableMemoryStream>(), version)) .Returns(new FetchResponse { FetchPartitionResponse = new CommonResponse <FetchPartitionResponse> { TopicsResponse = new[] { new TopicData <FetchPartitionResponse> { TopicName = "balbuzzard", PartitionsData = new[] { new FetchPartitionResponse { ErrorCode = ErrorCode.NoError, HighWatermarkOffset = 42, Partition = 1, Messages = new List <ResponseMessage> { new ResponseMessage { Offset = 28, Message = new Message() } } } } } } } }); bool resp = false; node.FetchResponseReceived += (n, c, s) => { Assert.That(n, Is.EqualTo(node)); Assert.That(c, Is.EqualTo(1)); Assert.That(s, Is.EqualTo(28)); resp = true; }; // Now send a response var r = new ReusableMemoryStream(null); r.SetLength(28); connection.Raise(c => c.Response += null, connection.Object, corr, r); serializer.Verify(s => s.DeserializeResponse <FetchResponse>(corr, It.IsAny <ReusableMemoryStream>(), It.IsAny <Basics.ApiVersion>()), Times.Once); serializer.Verify(s => s.DeserializeResponse <FetchResponse>(corr, It.IsAny <ReusableMemoryStream>(), version)); // Checks Assert.IsTrue(resp); Assert.AreNotEqual(default(DateTime), acknowledgement.ReceivedDate); Assert.AreEqual(1, acknowledgement.Response.FetchPartitionResponse.TopicsResponse.Length); Assert.AreEqual("balbuzzard", acknowledgement.Response.FetchPartitionResponse.TopicsResponse[0].TopicName); Assert.AreEqual(1, acknowledgement.Response.FetchPartitionResponse.TopicsResponse[0].PartitionsData.Count()); var fetch = acknowledgement.Response.FetchPartitionResponse.TopicsResponse[0].PartitionsData.First(); Assert.AreEqual(ErrorCode.NoError, fetch.ErrorCode); Assert.AreEqual(42, fetch.HighWatermarkOffset); Assert.AreEqual(1, fetch.Partition); Assert.AreEqual(1, fetch.Messages.Count()); Assert.AreEqual(28, fetch.Messages.First().Offset); Assert.AreEqual(new Message(), fetch.Messages.First().Message); // This is not full proof but come on... }
protected override void BeforeRender(MailDb maildb, FetchMessage Message, DataItem dataItem) { }
public List <ImapResponse> Render(MailDb maildb, FetchMessage message, DataItem dataItem) { // Todo: what happen when BODY.PEEK if (dataItem.Section == null) { return(RenderBodyStructure(maildb, message, dataItem)); } BeforeRender(maildb, message, dataItem); var bytes = message.Bytes; if (dataItem.Section.PartSpecifier != null) { var parsed = LumiSoft.Net.Mail.Mail_Message.ParseFromByte(bytes); var specifier = SpecifierParse.Parse(dataItem.Section.PartSpecifier); var entity = GetEntity(parsed, specifier.Numbers); if (entity == null) { bytes = new byte[0]; } else { switch (specifier.Sole) { case "MIME": bytes = entity.Header.ToByte(new MIME_Encoding_EncodedWord(MIME_EncodedWordEncoding.B, Encoding.UTF8), Encoding.UTF8); break; case "HEADER": bytes = entity.Header.ToByte(new MIME_Encoding_EncodedWord(MIME_EncodedWordEncoding.B, Encoding.UTF8), Encoding.UTF8); bytes = bytes.Concat(new byte[] { 0x0D, 0x0A }).ToArray(); break; case "HEADER.FIELDS": bytes = PickHeaderToBytes(entity, new HashSet <string>(dataItem.Section.Paras, StringComparer.OrdinalIgnoreCase), true); bytes = bytes.Concat(new byte[] { 0x0D, 0x0A }).ToArray(); break; case "HEADER.FIELDS.NOT": bytes = PickHeaderToBytes(entity, new HashSet <string>(dataItem.Section.Paras, StringComparer.OrdinalIgnoreCase), false); bytes = bytes.Concat(new byte[] { 0x0D, 0x0A }).ToArray(); break; //case "TEXT": default: bytes = entity.Body.ToByte(); break; } } } var result = new List <ImapResponse>(); var builder = new StringBuilder() .Append("BODY").Append("["); if (dataItem.Section != null) { builder.Append(dataItem.Section.PartSpecifier); if (dataItem.Section.Paras != null && dataItem.Section.Paras.Count > 0) { builder.Append(" (").Append(String.Join(" ", dataItem.Section.Paras.Select(o => o.ToUpperInvariant()))).Append(")"); } builder.Append("]"); } if (dataItem.Partial != null) { if (dataItem.Partial.OffSet >= bytes.Length) { builder.Append("<").Append(dataItem.Partial.OffSet).Append(">").Append(" \"\""); result.Add(new ImapResponse(builder.ToString())); } else { var count = Math.Min(dataItem.Partial.Count, bytes.Length - dataItem.Partial.OffSet); builder.Append("<").Append(dataItem.Partial.OffSet).Append(">") .Append(" {").Append(count).Append("}"); result.Add(new ImapResponse(builder.ToString())); result.Add(new ImapResponse(bytes.Skip(dataItem.Partial.OffSet).Take(count).ToArray())); } } else { builder.Append(" {").Append(bytes.Length).Append("}"); result.Add(new ImapResponse(builder.ToString())); result.Add(new ImapResponse(bytes)); } return(result); }