Start() public method

public Start ( ) : void
return void
Example #1
0
File: test.cs Project: mono/gert
	static void Main ()
	{
		IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 5000);
		using (SocketResponder sr = new SocketResponder (localEP, new SocketRequestHandler (Response_Bug80944))) {
			sr.Start ();

			HttpWebRequest req = (HttpWebRequest) WebRequest.Create (
				"http://" + sr.LocalEndPoint.Address.ToString () + ":5000/");
			req.KeepAlive = false;
			req.Method = "GET";
			req.ReadWriteTimeout = 2000;
#if NET_2_0
			req.Proxy = null;
#endif
			req.Timeout = 2000;
#if NET_2_0
			Assert.IsNull (req.Proxy, "#1");
#else
			Assert.IsNotNull (req.Proxy, "#1");
#endif

			HttpWebResponse resp = (HttpWebResponse) req.GetResponse ();
			using (StreamReader r = new StreamReader (resp.GetResponseStream (), Encoding.UTF8)) {
				Assert.AreEqual ("FINE", r.ReadToEnd (), "#2");
			}
			resp.Close ();
		}
	}
Example #2
0
		public void ContentEncoding_Disposed ()
		{
			IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
			string url = "http://" + ep.ToString () + "/test/";

			using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (FullResponseHandler))) {
				responder.Start ();

				HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
				req.Method = "GET";
				req.Timeout = 2000;
				req.ReadWriteTimeout = 2000;
				req.KeepAlive = false;

				HttpWebResponse resp = (HttpWebResponse) req.GetResponse ();
				((IDisposable) resp).Dispose ();

				try {
					string enc = resp.ContentEncoding;
					Assert.Fail ("#1:" + enc);
				} catch (ObjectDisposedException ex) {
					Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
					Assert.IsNull (ex.InnerException, "#3");
					Assert.IsNotNull (ex.Message, "#4");
					Assert.AreEqual (typeof (HttpWebResponse).FullName, ex.ObjectName, "#5");
				}
			}
		}
Example #3
0
		[Test] // bug #81105
		public void CloseTest ()
		{
			IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8765);
			using (SocketResponder sr = new SocketResponder (localEP, new SocketRequestHandler (CloseRequestHandler))) {
				sr.Start ();

				TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), 8765);
				NetworkStream ns = tcpClient.GetStream ();
				Assert.IsNotNull (ns, "#A1");
				Assert.AreEqual (0, tcpClient.Available, "#A2");
				Assert.IsTrue (tcpClient.Connected, "#A3");
				// Assert.IsFalse (tcpClient.ExclusiveAddressUse, "#A4");
				tcpClient.Close ();
				Assert.IsNotNull (tcpClient.Client, "#A5");
				try {
					int available = tcpClient.Available;
					Assert.Fail ("#A6: " + available);
				} catch (ObjectDisposedException) {
				}
				Assert.IsFalse (tcpClient.Connected, "#A7");
				// not supported on linux
				/*
				try {
					bool exclusive = tcpClient.ExclusiveAddressUse;
					Assert.Fail ("#A8: " + exclusive);
				} catch (ObjectDisposedException) {
				}
				*/
			}

			using (SocketResponder sr = new SocketResponder (localEP, new SocketRequestHandler (CloseRequestHandler))) {
				sr.Start ();

				TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), 8765);
				Assert.AreEqual (0, tcpClient.Available, "#B1");
				Assert.IsTrue (tcpClient.Connected, "#B2");
				// Assert.IsFalse (tcpClient.ExclusiveAddressUse, "#B3");
				tcpClient.Close ();
				Assert.IsNull (tcpClient.Client, "#B4");
				try {
					int available = tcpClient.Available;
					Assert.Fail ("#B5: " + available);
				} catch (NullReferenceException) {
				}
				try {
					bool connected = tcpClient.Connected;
					Assert.Fail ("#B6: " + connected);
				} catch (NullReferenceException) {
				}
				// not supported on linux
				/*
				try {
					bool exclusive = tcpClient.ExclusiveAddressUse;
					Assert.Fail ("#B7: " + exclusive);
				} catch (NullReferenceException) {
				}
				*/
			}
		}
Example #4
0
File: test.cs Project: mono/gert
	static void Main ()
	{
		IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 5000);
		using (SocketResponder sr = new SocketResponder (localEP, new SocketRequestHandler (Response_Bug79988))) {
			sr.Start ();

			FooService service = new FooService ();
			service.Url = "http://" + IPAddress.Loopback.ToString () + ":5000/";

			int a;
			bool b;
			Elem [] e = service.Req ("x", out a, out b);
			Assert.IsNull (e, "#A1");
			Assert.AreEqual (0, a, "#A2");
			Assert.IsFalse (b, "#A3");
		}
	}
Example #5
0
File: test.cs Project: mono/gert
	static void Main ()
	{
		IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 5000);
		using (SocketResponder sr = new SocketResponder (localEP, new SocketRequestHandler (Response_Bug80131))) {
			sr.Start ();

			FooService service = new FooService ();
			service.Url = "http://" + sr.LocalEndPoint.Address.ToString () + ":5000/";

			int a;
			bool b;
			Elem [] e = service.Req ("x", out a, out b);
			Assert.IsNotNull (e, "#B1");
			Assert.AreEqual (1, e.Length, "#B2");
			Assert.AreEqual ("whatever", e [0].attr, "#B2");
			Assert.AreEqual (5, a, "#B3");
			Assert.IsTrue (b, "#B4");
		}
	}
Example #6
0
		public void Close_Disposed ()
		{
			IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
			string url = "http://" + ep.ToString () + "/test/";

			using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (FullResponseHandler))) {
				responder.Start ();

				HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
				req.Method = "GET";
				req.Timeout = 2000;
				req.ReadWriteTimeout = 2000;
				req.KeepAlive = false;

				HttpWebResponse resp = (HttpWebResponse) req.GetResponse ();
				((IDisposable) resp).Dispose ();
				resp.Close ();
			}
		}
Example #7
0
        public void CloseTest()
        {
            IPEndPoint localEP = new IPEndPoint(IPAddress.Loopback, 8765);

            using (SocketResponder sr = new SocketResponder(localEP, new SocketRequestHandler(CloseRequestHandler))) {
                sr.Start();

                TcpClient     tcpClient = new TcpClient(IPAddress.Loopback.ToString(), 8765);
                NetworkStream ns        = tcpClient.GetStream();
                Assert.IsNotNull(ns, "#A1");
#if NET_2_0
                Assert.AreEqual(0, tcpClient.Available, "#A2");
                Assert.IsTrue(tcpClient.Connected, "#A3");
                // Assert.IsFalse (tcpClient.ExclusiveAddressUse, "#A4");
#endif
                tcpClient.Close();
#if NET_2_0
                Assert.IsNotNull(tcpClient.Client, "#A5");
                try {
                    int available = tcpClient.Available;
                    Assert.Fail("#A6: " + available);
                } catch (ObjectDisposedException) {
                }
                Assert.IsFalse(tcpClient.Connected, "#A7");
                // not supported on linux

                /*
                 * try {
                 *      bool exclusive = tcpClient.ExclusiveAddressUse;
                 *      Assert.Fail ("#A8: " + exclusive);
                 * } catch (ObjectDisposedException) {
                 * }
                 */
#endif
            }

            using (SocketResponder sr = new SocketResponder(localEP, new SocketRequestHandler(CloseRequestHandler))) {
                sr.Start();

                TcpClient tcpClient = new TcpClient(IPAddress.Loopback.ToString(), 8765);
#if NET_2_0
                Assert.AreEqual(0, tcpClient.Available, "#B1");
                Assert.IsTrue(tcpClient.Connected, "#B2");
                // Assert.IsFalse (tcpClient.ExclusiveAddressUse, "#B3");
#endif
                tcpClient.Close();
#if NET_2_0
                Assert.IsNull(tcpClient.Client, "#B4");
                try {
                    int available = tcpClient.Available;
                    Assert.Fail("#B5: " + available);
                } catch (NullReferenceException) {
                }
                try {
                    bool connected = tcpClient.Connected;
                    Assert.Fail("#B6: " + connected);
                } catch (NullReferenceException) {
                }
                // not supported on linux

                /*
                 * try {
                 *      bool exclusive = tcpClient.ExclusiveAddressUse;
                 *      Assert.Fail ("#B7: " + exclusive);
                 * } catch (NullReferenceException) {
                 * }
                 */
#endif
            }
        }
Example #8
0
		public void UploadAsyncCancelEventTest (int port, Action<WebClient, Uri, EventWaitHandle> uploadAction)
		{
			var ep = NetworkHelpers.LocalEphemeralEndPoint ();
			string url = "http://" + ep.ToString() + "/test/";

			using (var responder = new SocketResponder (ep, EchoRequestHandler))
			{
				responder.Start ();

				var webClient = new WebClient ();

				var cancellationTokenSource = new CancellationTokenSource ();
				cancellationTokenSource.Token.Register (webClient.CancelAsync);

				var cancelEvent = new ManualResetEvent (false);

				uploadAction.Invoke (webClient, new Uri (url), cancelEvent);

				cancellationTokenSource.Cancel ();

				Assert.IsTrue (cancelEvent.WaitOne (1000));
			}
		}
Example #9
0
		[Category ("AndroidNotWorking")] // Fails when ran as part of the entire BCL test suite. Works when only this fixture is ran
		public void UploadValues1 ()
		{
			IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
			string url = "http://" + ep.ToString () + "/test/";

			using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
				responder.Start ();

				WebClient wc = new WebClient ();
				wc.Encoding = Encoding.ASCII;

				NameValueCollection nvc = new NameValueCollection ();
				nvc.Add ("Name", "\u0041\u2262\u0391\u002E");
				nvc.Add ("Address", "\u002E\u2262\u0041\u0391");

				byte [] buffer = wc.UploadValues (url, nvc);
				string response = Encoding.UTF8.GetString (buffer);
				Assert.AreEqual ("Name=A%e2%89%a2%ce%91.&Address=.%e2%89%a2A%ce%91\r\n", response);
			}
		}
Example #10
0
		[Test] // bug #81886
		public void FaultTest ()
		{
			IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 5000);
			using (SocketResponder sr = new SocketResponder (localEP, new SocketRequestHandler (FaultResponse_Qualified))) {
				sr.Start ();

				FooService service = new FooService ();
				service.Url = "http://" + IPAddress.Loopback.ToString () + ":5000/";
				try {
					service.Run ();
					Assert.Fail ("#A1");
				} catch (SoapException ex) {
					Assert.AreEqual ("Mono Web Service", ex.Actor, "#A2");
					Assert.AreEqual (SoapException.ServerFaultCode, ex.Code, "#A3");
					Assert.IsNotNull (ex.Detail, "#A4");
					Assert.AreEqual ("detail", ex.Detail.LocalName, "#A5");
					Assert.AreEqual ("http://schemas.xmlsoap.org/soap/envelope/", ex.Detail.NamespaceURI, "#A6");

					XmlNamespaceManager nsMgr = new XmlNamespaceManager (ex.Detail.OwnerDocument.NameTable);
					nsMgr.AddNamespace ("se", "http://www.mono-project/System");

					XmlElement systemError = (XmlElement) ex.Detail.SelectSingleNode (
						"se:systemerror", nsMgr);
					Assert.IsNotNull (systemError, "#A7");
					Assert.IsNull (ex.InnerException, "#A8");
					Assert.AreEqual ("Failure processing request.", ex.Message, "#A9");
				}
				service.Dispose ();

				sr.Stop ();
			}

			using (SocketResponder sr = new SocketResponder (localEP, new SocketRequestHandler (FaultResponse_Unqualified))) {
				sr.Start ();

				FooService service = new FooService ();
				service.Url = "http://" + IPAddress.Loopback.ToString () + ":5000/";
				try {
					service.Run ();
					Assert.Fail ("#B1");
				} catch (SoapException ex) {
					Assert.AreEqual ("Mono Web Service", ex.Actor, "#B2");
					Assert.AreEqual (SoapException.ServerFaultCode, ex.Code, "#B3");
					Assert.IsNotNull (ex.Detail, "#B4");
					Assert.AreEqual ("detail", ex.Detail.LocalName, "#B5");
					Assert.AreEqual (string.Empty, ex.Detail.NamespaceURI, "#B6");

					XmlNamespaceManager nsMgr = new XmlNamespaceManager (ex.Detail.OwnerDocument.NameTable);
					nsMgr.AddNamespace ("se", "http://www.mono-project/System");

					XmlElement systemError = (XmlElement) ex.Detail.SelectSingleNode (
						"se:systemerror", nsMgr);
					Assert.IsNotNull (systemError, "#B7");
					Assert.IsNull (ex.InnerException, "#B8");
					Assert.AreEqual ("Failure processing request.", ex.Message, "#B9");
				}
				service.Dispose ();

				sr.Stop ();
			}
		}
Example #11
0
		public void Read_Offset_Overflow ()
		{
			IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
			string url = "http://" + ep.ToString () + "/test/";

			using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebResponseTest.FullResponseHandler))) {
				responder.Start ();

				HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
				req.Method = "GET";
				req.Timeout = 2000;
				req.ReadWriteTimeout = 2000;
				req.KeepAlive = false;

				using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
					Stream rs = resp.GetResponseStream ();
					byte [] buffer = new byte [5];
					try {
						try {
							rs.Read (buffer, buffer.Length + 1, 0);
							Assert.Fail ("#A1");
						} catch (ArgumentOutOfRangeException ex) {
							// Specified argument was out of the range of valid values
							Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
							Assert.IsNull (ex.InnerException, "#A3");
							Assert.IsNotNull (ex.Message, "#A4");
							Assert.AreEqual ("offset", ex.ParamName, "#A5");
						}

						// read full response
						buffer = new byte [24];
						Assert.AreEqual (9, rs.Read (buffer, 0, buffer.Length));

						try {
							rs.Read (buffer, buffer.Length + 1, 0);
							Assert.Fail ("#B1");
						} catch (ArgumentOutOfRangeException ex) {
							// Specified argument was out of the range of valid values
							Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
							Assert.IsNull (ex.InnerException, "#B3");
							Assert.IsNotNull (ex.Message, "#B4");
							Assert.AreEqual ("offset", ex.ParamName, "#B5");
						}
					} finally {
						rs.Close ();
						req.Abort ();
					}
				}
			}
		}
Example #12
0
		public void Read ()
		{
			IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
			string url = "http://" + ep.ToString () + "/test/";

			using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebResponseTest.FullResponseHandler))) {
				responder.Start ();

				HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
				req.Method = "GET";
				req.Timeout = 2000;
				req.ReadWriteTimeout = 2000;
				req.KeepAlive = false;

				using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
					Stream rs = resp.GetResponseStream ();
					byte [] buffer = new byte [5];
					try {
						Assert.AreEqual (1, rs.Read (buffer, 4, 1), "#A1");
						Assert.AreEqual (new byte [] { 0x00, 0x00, 0x00, 0x00, 0x3c }, buffer, "#A2");
						Assert.AreEqual (2, rs.Read (buffer, 0, 2), "#B1");
						Assert.AreEqual (new byte [] { 0x64, 0x75, 0x00, 0x00, 0x3c }, buffer, "#B2");
						Assert.AreEqual (4, rs.Read (buffer, 1, 4), "#C1");
						Assert.AreEqual (new byte [] { 0x64, 0x6d, 0x6d, 0x79, 0x20 }, buffer, "#C2");
						Assert.AreEqual (2, rs.Read (buffer, 0, 3), "#D1");
						Assert.AreEqual (new byte [] { 0x2f, 0x3e, 0x6d, 0x79, 0x20 }, buffer, "#D2");
						Assert.AreEqual (0, rs.Read (buffer, 1, 3), "#E1");
						Assert.AreEqual (new byte [] { 0x2f, 0x3e, 0x6d, 0x79, 0x20 }, buffer, "#E2");
						Assert.AreEqual (0, rs.Read (buffer, buffer.Length, 0), "#G1");
						Assert.AreEqual (new byte [] { 0x2f, 0x3e, 0x6d, 0x79, 0x20 }, buffer, "#G2");
					} finally {
						rs.Close ();
						req.Abort ();
					}
				}
			}
		}
Example #13
0
		public void CanWrite ()
		{
			IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
			string url = "http://" + ep.ToString () + "/test/";

			using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebResponseTest.FullResponseHandler))) {
				responder.Start ();

				HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
				req.Method = "GET";
				req.Timeout = 2000;
				req.ReadWriteTimeout = 2000;
				req.KeepAlive = false;

				using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
					Stream rs = resp.GetResponseStream ();
					try {
						Assert.IsFalse (rs.CanWrite, "#1");
						rs.Close ();
						Assert.IsFalse (rs.CanWrite, "#2");
					} finally {
						rs.Close ();
						req.Abort ();
					}
				}
			}
		}
Example #14
0
		public void BeginWrite ()
		{
			IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
			string url = "http://" + ep.ToString () + "/test/";

			using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebResponseTest.FullResponseHandler))) {
				responder.Start ();

				HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
				req.Method = "GET";
				req.Timeout = 2000;
				req.ReadWriteTimeout = 2000;
				req.KeepAlive = false;

				using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
					Stream rs = resp.GetResponseStream ();
					byte [] buffer = new byte [5];
					try {
						rs.BeginWrite (buffer, 0, buffer.Length, null, null);
						Assert.Fail ("#1");
					} catch (NotSupportedException ex) {
						// The stream does not support writing
						Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
						Assert.IsNull (ex.InnerException, "#3");
						Assert.IsNotNull (ex.Message, "#4");
					} finally {
						rs.Close ();
						req.Abort ();
					}
				}
			}
		}
Example #15
0
		public void Headers_Disposed ()
		{
			IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
			string url = "http://" + ep.ToString () + "/test/";

			using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (FullResponseHandler))) {
				responder.Start ();

				HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
				req.Method = "GET";
				req.Timeout = 2000;
				req.ReadWriteTimeout = 2000;
				req.KeepAlive = false;

				HttpWebResponse resp = (HttpWebResponse) req.GetResponse ();
				((IDisposable) resp).Dispose ();

				WebHeaderCollection headers = resp.Headers;
				Assert.AreEqual (6, headers.Count, "#1");
				Assert.AreEqual ("9", headers ["Content-Length"], "#2");
				Assert.AreEqual ("utf-8", headers ["Content-Encoding"], "#3");
				Assert.AreEqual ("text/xml; charset=UTF-8", headers ["Content-Type"], "#4");
				Assert.AreEqual ("Wed, 08 Jan 2003 23:11:55 GMT", headers ["Last-Modified"], "#5");
				Assert.AreEqual ("UserID=Miguel,StoreProfile=true", headers ["Set-Cookie"], "#6");
				Assert.AreEqual ("Mono/Test", headers ["Server"], "#7");
			}
		}
Example #16
0
		public void Read_Stream_Closed ()
		{
			IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint();
			string url = "http://" + ep.ToString () + "/test/";

			using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebResponseTest.FullResponseHandler))) {
				responder.Start ();

				HttpWebRequest req;
				
				req = (HttpWebRequest) WebRequest.Create (url);
				req.Method = "GET";
				req.Timeout = 2000;
				req.ReadWriteTimeout = 2000;
				req.KeepAlive = false;

				using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
					Stream rs = resp.GetResponseStream ();
					rs.Close ();
					try {
						rs.Read (new byte [0], 0, 0);
						Assert.Fail ("#A1");
					} catch (WebException ex) {
						// The request was aborted: The connection was closed unexpectedly
						Assert.AreEqual (typeof (WebException), ex.GetType (), "#A2");
						Assert.IsNull (ex.InnerException, "#A3");
						Assert.IsNotNull (ex.Message, "#A4");
						Assert.IsNull (ex.Response, "#A5");
						Assert.AreEqual (WebExceptionStatus.ConnectionClosed, ex.Status, "#A6");
					} finally {
						rs.Close ();
						req.Abort ();
					}
				}

				req = (HttpWebRequest) WebRequest.Create (url);
				req.Method = "GET";
				req.Timeout = 2000;
				req.ReadWriteTimeout = 2000;
				req.KeepAlive = false;

				using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
					Stream rs = resp.GetResponseStream ();
					byte [] buffer = new byte [24];
					Assert.AreEqual (9, rs.Read (buffer, 0, buffer.Length));
					rs.Close ();
					try {
						rs.Read (new byte [0], 0, 0);
						Assert.Fail ("#B1");
					} catch (WebException ex) {
						// The request was aborted: The connection was closed unexpectedly
						Assert.AreEqual (typeof (WebException), ex.GetType (), "#B2");
						Assert.IsNull (ex.InnerException, "#B3");
						Assert.IsNotNull (ex.Message, "#B4");
						Assert.IsNull (ex.Response, "#B5");
						Assert.AreEqual (WebExceptionStatus.ConnectionClosed, ex.Status, "#B6");
					} finally {
						rs.Close ();
						req.Abort ();
					}
				}
			}
		}