// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
+using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
+using System.Threading.Tasks;
using NUnit.Framework;
using MonoTests.Helpers;
[TestFixture]
public class HttpListenerTest {
- int port;
-
- [SetUp]
- public void SetUp () {
- port = NetworkHelpers.FindFreePort ();
+ int? _port;
+ int port {
+ get { return _port ?? (_port = NetworkHelpers.FindFreePort ()).Value; }
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void DefaultProperties ()
{
HttpListener listener = new HttpListener ();
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void Start1 ()
{
HttpListener listener = new HttpListener ();
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void Stop1 ()
{
HttpListener listener = new HttpListener ();
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#else
[ExpectedException (typeof (InvalidOperationException))]
+#endif
public void GetContext1 ()
{
HttpListener listener = new HttpListener ();
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#else
[ExpectedException (typeof (InvalidOperationException))]
+#endif
public void GetContext2 ()
{
HttpListener listener = new HttpListener ();
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#else
[ExpectedException (typeof (InvalidOperationException))]
+#endif
public void BeginGetContext1 ()
{
HttpListener listener = new HttpListener ();
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void BeginGetContext2 ()
{
HttpListener listener = new HttpListener ();
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void DefaultHttpPort ()
{
if (!CanOpenPort (80))
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void DefaultHttpsPort ()
{
if (!CanOpenPort (443))
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void TwoListeners_SameAddress ()
{
if (!CanOpenPort (port))
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#else
[ExpectedException (typeof (HttpListenerException))]
+#endif
public void TwoListeners_SameURL ()
{
if (!CanOpenPort (port))
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#else
[ExpectedException (typeof (HttpListenerException))]
+#endif
public void MultipleSlashes ()
{
if (!CanOpenPort (port))
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#else
[ExpectedException (typeof (HttpListenerException))]
+#endif
public void PercentSign ()
{
if (!CanOpenPort (port))
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void CloseBeforeStart ()
{
HttpListener listener = new HttpListener ();
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void CloseTwice ()
{
if (!CanOpenPort (port))
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void StartStopStart ()
{
if (!CanOpenPort (port))
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void StartStopDispose ()
{
if (!CanOpenPort (port))
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void AbortBeforeStart ()
{
HttpListener listener = new HttpListener ();
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void AbortTwice ()
{
if (!CanOpenPort (port))
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void PropertiesWhenClosed1 ()
{
HttpListener listener = new HttpListener ();
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#else
[ExpectedException (typeof (ObjectDisposedException))]
+#endif
public void PropertiesWhenClosed2 ()
{
HttpListener listener = new HttpListener ();
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#else
[ExpectedException (typeof (ObjectDisposedException))]
+#endif
public void PropertiesWhenClosedSet1 ()
{
HttpListener listener = new HttpListener ();
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#else
[ExpectedException (typeof (ObjectDisposedException))]
+#endif
public void PropertiesWhenClosedSet2 ()
{
HttpListener listener = new HttpListener ();
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#else
[ExpectedException (typeof (ObjectDisposedException))]
+#endif
public void PropertiesWhenClosedSet3 ()
{
HttpListener listener = new HttpListener ();
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#else
[ExpectedException (typeof (ObjectDisposedException))]
+#endif
public void PropertiesWhenClosedSet4 ()
{
HttpListener listener = new HttpListener ();
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#else
[ExpectedException (typeof (ObjectDisposedException))]
+#endif
public void PropertiesWhenClosedSet5 ()
{
HttpListener listener = new HttpListener ();
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void PropertiesWhenClosed3 ()
{
HttpListener listener = new HttpListener ();
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void CloseWhileBegin ()
{
HttpListener listener = new HttpListener ();
- listener.Prefixes.Add ("http://127.0.0.1:9001/closewhilebegin/");
+ listener.Prefixes.Add ("http://127.0.0.1:" + NetworkHelpers.FindFreePort () + "/closewhilebegin/");
listener.Start ();
CallMe cm = new CallMe ();
listener.BeginGetContext (cm.Callback, listener);
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void AbortWhileBegin ()
{
HttpListener listener = new HttpListener ();
- listener.Prefixes.Add ("http://127.0.0.1:9001/abortwhilebegin/");
+ listener.Prefixes.Add ("http://127.0.0.1:" + NetworkHelpers.FindFreePort () + "/abortwhilebegin/");
listener.Start ();
CallMe cm = new CallMe ();
listener.BeginGetContext (cm.Callback, listener);
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#else
[ExpectedException (typeof (HttpListenerException))]
+#endif
public void CloseWhileGet ()
{
// "System.Net.HttpListener Exception : The I/O operation has been aborted
// at MonoTests.System.Net.HttpListenerTest.CloseWhileGet()
HttpListener listener = new HttpListener ();
- listener.Prefixes.Add ("http://127.0.0.1:9001/closewhileget/");
+ listener.Prefixes.Add ("http://127.0.0.1:" + NetworkHelpers.FindFreePort () + "/closewhileget/");
listener.Start ();
RunMe rm = new RunMe (1000, new ThreadStart (listener.Close), new object [0]);
rm.Start ();
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#else
[ExpectedException (typeof (HttpListenerException))]
+#endif
public void AbortWhileGet ()
{
// "System.Net.HttpListener Exception : The I/O operation has been aborted
// at MonoTests.System.Net.HttpListenerTest.CloseWhileGet()
HttpListener listener = new HttpListener ();
- listener.Prefixes.Add ("http://127.0.0.1:9001/abortwhileget/");
+ listener.Prefixes.Add ("http://127.0.0.1:" + NetworkHelpers.FindFreePort () + "/abortwhileget/");
listener.Start ();
RunMe rm = new RunMe (1000, new ThreadStart (listener.Abort), new object [0]);
rm.Start ();
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void ConnectionReuse ()
{
var uri = "http://localhost:" + NetworkHelpers.FindFreePort () + "/";
return clientEndPoint;
}
+
+ [Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
+ public void UserHeaderWithDoubleMultiValue ()
+ {
+ string uri = "http://localhost:" + NetworkHelpers.FindFreePort () + "/";
+
+ var l = new HttpListener ();
+ l.Prefixes.Add (uri);
+ l.Start ();
+ l.BeginGetContext (ar => {
+ var ctx = l.EndGetContext (ar);
+
+ var response = ctx.Response;
+ response.Headers.Add ("X-Custom-Header", "A");
+ response.Headers.Add ("X-Custom-Header", "B");
+
+ response.Close ();
+ }, null);
+
+ HttpWebRequest wr = HttpWebRequest.CreateHttp (uri);
+ var resp = wr.GetResponse ();
+ var vls = resp.Headers.GetValues ("X-Custom-Header");
+
+ Assert.AreEqual (2, vls.Length);
+
+ l.Close ();
+ }
+
+ [Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
+ public void HttpClientIsDisconnectedCheckForWriteException()
+ {
+ string uri = "http://localhost:" + NetworkHelpers.FindFreePort () + "/";
+
+ AutoResetEvent exceptionOccuredEvent = new AutoResetEvent (false);
+ HttpListener listener = new HttpListener {
+ IgnoreWriteExceptions = false
+ };
+ listener.Prefixes.Add (uri);
+ listener.Start ();
+ listener.BeginGetContext (result =>
+ {
+ HttpListenerContext context = listener.EndGetContext (result);
+ context.Response.SendChunked = true;
+ context.Request.InputStream.Close ();
+
+ var bytes = new byte [1024];
+ using(Stream outputStream = context.Response.OutputStream) {
+ try {
+ while (true)
+ outputStream.Write (bytes, 0, bytes.Length);
+ } catch {
+ exceptionOccuredEvent.Set ();
+ }
+ }
+ }, null);
+
+ Task.Factory.StartNew (() =>
+ {
+ var webRequest = (HttpWebRequest)WebRequest.Create (uri);
+ webRequest.Method = "POST";
+ webRequest.KeepAlive = false;
+ Stream requestStream = webRequest.GetRequestStream ();
+ requestStream.WriteByte (1);
+ requestStream.Close ();
+ using (WebResponse response = webRequest.GetResponse ())
+ using (Stream stream = response.GetResponseStream ()) {
+ byte[] clientBytes = new byte [1024];
+ Assert.IsNotNull (stream, "#01");
+ stream.Read (clientBytes, 0, clientBytes.Length);
+ }
+ });
+
+ Assert.IsTrue (exceptionOccuredEvent.WaitOne (15 * 1000), "#02");
+ }
}
}