public void TestBeginGetRequestStreamCallback()
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

            // greeting
            server.EnqueueResponse("* OK ready\r\n");
            // CAPABILITY
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0000 OK done\r\n");
            // LOGIN
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0001 OK done\r\n");
            // APPEND
            server.EnqueueResponse("+ continue\r\n");
            server.EnqueueResponse(string.Empty);

            var request = WebRequest.Create(string.Format("imap://{0}/INBOX", server.HostPort)) as ImapWebRequest;

            request.Method = "APPEND";
            request.Timeout = 3000;
            request.ReadWriteTimeout = 3000;
            request.AllowInsecureLogin = true;

            var asyncResult = request.BeginGetRequestStream((AsyncCallback)BeginGetRequestStreamCallbackProc, request);

            Thread.Sleep(100); // XXX

            if (!asyncResult.AsyncWaitHandle.WaitOne(5000))
              Assert.Fail("wait time out");

            server.EnqueueResponse("0002 OK done\r\n");

            using (var response = request.GetResponse()) {
            }

            server.DequeueRequest(); // CAPABILITY
            server.DequeueRequest(); // LOGIN

            var appendCommand = server.DequeueAll();

            StringAssert.StartsWith("0002 APPEND \"INBOX\" ", appendCommand);
            StringAssert.EndsWith(" {12}\r\ntest message\r\n", appendCommand);
              }
        }
        public void TestGetRequestStreamWriteOverContentLength()
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

            // greeting
            server.EnqueueResponse("* OK ready\r\n");
            // CAPABILITY
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0000 OK done\r\n");
            // LOGIN
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0001 OK done\r\n");
            // APPEND
            server.EnqueueResponse("+ continue\r\n");
            server.EnqueueResponse(string.Empty);

            var message = "test message";
            var data = Encoding.ASCII.GetBytes(message);
            var request = WebRequest.Create(string.Format("imap://{0}/INBOX", server.HostPort)) as ImapWebRequest;

            request.Method = "APPEND";
            request.Timeout = 3000;
            request.ReadWriteTimeout = 3000;
            request.ContentLength = data.Length;
            request.AllowInsecureLogin = true;

            using (var stream = request.GetRequestStream()) {
              stream.Write(data, 0, data.Length);

              // extra data
              stream.WriteByte(0x40);
              stream.WriteByte(0x40);
              stream.WriteByte(0x40);
              stream.WriteByte(0x40);
            }

            server.EnqueueResponse("0002 OK done\r\n");

            using (var response = request.GetResponse() as ImapWebResponse) {
              Assert.IsNotNull(response.GetResponseStream());

              Assert.IsNotNull(response.Result);
              Assert.IsTrue(response.Result.Succeeded);
            }

            server.DequeueRequest(); // CAPABILITY
            server.DequeueRequest(); // LOGIN

            var appendCommand = server.DequeueAll();

            StringAssert.StartsWith("0002 APPEND \"INBOX\" ", appendCommand);
            StringAssert.EndsWith(string.Format(" {{{0}}}\r\n{1}\r\n", Encoding.ASCII.GetByteCount(message), message), appendCommand);
              }
        }
        private void GetAppendResponse(bool appendUid)
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

            // greeting
            server.EnqueueResponse("* OK ready\r\n");
            // CAPABILITY
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0000 OK done\r\n");
            // LOGIN
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0001 OK done\r\n");
            // APPEND
            server.EnqueueResponse("+ continue\r\n");
            server.EnqueueResponse(string.Empty);

            var message = "test message";
            var data = Encoding.ASCII.GetBytes(message);
            var request = WebRequest.Create(string.Format("imap://{0}/INBOX", server.HostPort)) as ImapWebRequest;

            request.Method = "APPEND";
            request.Timeout = 3000;
            request.ReadWriteTimeout = 3000;
            request.ContentLength = data.Length;
            request.AllowInsecureLogin = true;

            using (var stream = request.GetRequestStream()) {
              stream.Write(data, 0, data.Length);
            }

            if (appendUid)
              server.EnqueueResponse("0002 OK [APPENDUID 38505 3955] done\r\n");
            else
              server.EnqueueResponse("0002 OK done\r\n");

            using (var response = request.GetResponse() as ImapWebResponse) {
              Assert.IsNotNull(response.GetResponseStream());

              if (appendUid)
            Assert.AreEqual(new Uri(string.Format("imap://{0}/INBOX;UIDVALIDITY=38505/;UID=3955", server.HostPort)), response.ResponseUri);
              else
            Assert.AreEqual(request.RequestUri, response.ResponseUri);

              Assert.IsNotNull(response.Result);
              Assert.IsTrue(response.Result.Succeeded);
            }

            server.DequeueRequest(); // CAPABILITY
            server.DequeueRequest(); // LOGIN

            var appendCommand = server.DequeueAll();

            StringAssert.StartsWith("0002 APPEND \"INBOX\" ", appendCommand);
            StringAssert.EndsWith(string.Format(" {{{0}}}\r\n{1}\r\n", Encoding.ASCII.GetByteCount(message), message), appendCommand);
              }
        }
        public void TestGetAppendResponseFailure()
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

            // greeting
            server.EnqueueResponse("* OK ready\r\n");
            // CAPABILITY
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0000 OK done\r\n");
            // LOGIN
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0001 OK done\r\n");
            // APPEND
            server.EnqueueResponse("0002 NO failed\r\n");

            var message = "test message";
            var data = Encoding.ASCII.GetBytes(message);
            var request = WebRequest.Create(string.Format("imap://{0}/INBOX", server.HostPort)) as ImapWebRequest;

            request.Method = "APPEND";
            request.Timeout = 3000;
            request.ReadWriteTimeout = 3000;
            request.AllowInsecureLogin = true;

            using (var stream = request.GetRequestStream()) {
              stream.Write(data, 0, data.Length);
            }

            try {
              using (var response = request.GetResponse()) {
              }
            }
            catch (WebException ex) {
              Assert.AreEqual(WebExceptionStatus.ProtocolError, ex.Status);

              var resp = ex.Response as ImapWebResponse;

              Assert.IsNotNull(resp);

              Assert.AreEqual(request.RequestUri, resp.ResponseUri);
              Assert.IsNotNull(resp.Result);
              Assert.IsTrue(resp.Result.Failed);
            }

            server.DequeueRequest(); // CAPABILITY
            server.DequeueRequest(); // LOGIN

            var appendCommand = server.DequeueAll();

            StringAssert.StartsWith("0002 APPEND \"INBOX\" ", appendCommand);
              }
        }
Esempio n. 5
0
        private void AssertAppendRequest(ImapPseudoServer server)
        {
            server.DequeueRequest(); // LOGIN

              var appendRequest = server.DequeueRequest();

              StringAssert.StartsWith("0001 APPEND \"INBOX\"", appendRequest);
              StringAssert.EndsWith(string.Format(" {{{0}}}\r\n", Encoding.ASCII.GetByteCount(message)), appendRequest);

              StringAssert.StartsWith(message, server.DequeueAll());
        }