public void TestCreateSessionConnectFailure() { using (var server = new ImapPseudoServer()) { server.Start(); var credential = new NetworkCredential("user", "pass"); var request = WebRequest.Create(string.Format("imap://{0}@{1}/", credential.UserName, server.HostPort)) as ImapWebRequest; request.Timeout = 500; server.Stop(); try { using (var response = request.GetResponse()) { } } catch (WebException ex) { WebExceptionAssertion(WebExceptionStatus.ConnectFailure, ex); } } }
public void TestGetResponseKeepAliveFalse() { using (var server = new ImapPseudoServer()) { for (var req = 0; req < 3; req++) { server.Start(); /* * request/response */ var request = WebRequest.Create(string.Format("imap://{0}/", server.HostPort)) as ImapWebRequest; request.Timeout = 1000; request.Method = "NOOP"; request.KeepAlive = false; // 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"); // NOOP server.EnqueueResponse("0002 OK done\r\n"); // LOGOUT server.EnqueueResponse("* BYE logging out\r\n" + "0003 OK done\r\n"); using (var response = request.GetResponse()) { } server.DequeueRequest(); // CAPABILITY server.DequeueRequest(); // LOGIN StringAssert.Contains("NOOP", server.DequeueRequest(), "request #{0}", req); StringAssert.Contains("LOGOUT", server.DequeueRequest(), "request #{0}", req); server.Stop(); } } }
private void TestExceptionWhileCloseSession(string test) { using (var server = new ImapPseudoServer()) { server.Start(); var request = WebRequest.Create(string.Format("imap://{0}/", server.HostPort)) as ImapWebRequest; request.Timeout = 3000; request.ReadWriteTimeout = 3000; request.Method = ImapWebRequestMethods.NoOp; request.KeepAlive = false; // 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"); // NOOP server.EnqueueResponse("0002 OK done\r\n"); var asyncResult = request.BeginGetResponse(null, null); Thread.Sleep(500); server.DequeueRequest(); // CAPABILITY server.DequeueRequest(); // LOGIN StringAssert.Contains("NOOP", server.DequeueRequest()); if (test == "not responding") // not respond to LOGOUT ; else if (test == "disconnected") server.Stop(); using (var response = request.EndGetResponse(asyncResult) as ImapWebResponse) { Assert.IsTrue(response.Result.Succeeded); } } }
private void KeepAliveAndDisconnected(bool disconnectFromServer) { using (var server = new ImapPseudoServer()) { for (var req = 0; req < 2; req++) { server.Start(); /* * request/response */ var request = WebRequest.Create(string.Format("imap://{0}/", server.HostPort)) as ImapWebRequest; request.Timeout = 1000; request.Method = "NOOP"; request.KeepAlive = true; // 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"); // NOOP server.EnqueueResponse("0002 OK done\r\n"); using (var response = request.GetResponse()) { } server.DequeueRequest(); // CAPABILITY server.DequeueRequest(); // LOGIN StringAssert.Contains("NOOP", server.DequeueRequest(), "request #{0}", req); if (disconnectFromServer) { server.Stop(); } else { // LOGOUT server.EnqueueResponse("* BYE logging out\r\n" + "0003 OK done\r\n"); ImapSessionManager.DisconnectFrom(request.RequestUri); server.DequeueRequest(); // LOGOUT server.Stop(); } } } }
public void TestCreateSessionCreateSslStreamCallbackNull() { using (var server = new ImapPseudoServer()) { server.Start(); var prof = new Profile(new NetworkCredential("user", "pass"), "IMAPS", "user", server.HostPort); prof.Timeout = 500; server.Stop(); ImapSessionCreator.CreateSession(prof, null, null); } }
public void TestCreateSessionConnectRefusedByTimeout() { using (var server = new ImapPseudoServer()) { server.Start(); var prof = new Profile(new NetworkCredential("user", "pass"), "user", server.HostPort); prof.Timeout = 500; server.Stop(); try { ImapSessionCreator.CreateSession(prof, null, null); Assert.Fail("ImapConnectionException not thrown"); } catch (ImapConnectionException ex) { var timeoutException = ex.InnerException as TimeoutException; Assert.IsNotNull(timeoutException); } } }
public void TestGetRequestStreamGetSessionFailed() { string hostPort; using (var server = new ImapPseudoServer()) { server.Start(); hostPort = server.HostPort; server.Stop(); } var request = WebRequest.Create(string.Format("imap://{0}/INBOX", hostPort)); request.Method = "APPEND"; request.Timeout = 1000; try { request.GetRequestStream(); } catch (WebException ex) { if (ex.Status != WebExceptionStatus.ConnectFailure) throw ex; } }