public void Test_HostInUri() { var wait = new ManualResetEvent(false); var wait2 = new ManualResetEvent(false); Thread t = new Thread(delegate(object a) { wait.WaitOne(); NetworkStream ns = HttpListener2Test.CreateNS(9145); HttpListener2Test.Send(ns, "GET http://www.google.com/ HTTP/1.1\r\nHost: www.google.com\r\nContent-Length: 3\r\n\r\n123456"); wait2.WaitOne(); ns.Close(); }); t.Start(); HttpListener listener = HttpListener2Test.CreateAndStartListener("http://*:9145/"); wait.Set(); HttpListenerContext ctx = listener.GetContext(); Assert.AreEqual("http://www.google.com:9145/", ctx.Request.Url.ToString()); Assert.AreEqual("http://www.google.com/", ctx.Request.RawUrl); wait2.Set(); listener.Close(); }
public void TestNonChunkedAsync() { HttpListener listener = HttpListener2Test.CreateAndStartListener("http://127.0.0.1:9123/"); listener.BeginGetContext(callback, listener); HttpListener2Test.MyNetworkStream ns = HttpListener2Test.CreateNS(9123); string message = "<script>\n" + " <!-- register the blueprint for our show-headers service -->\n" + " <action verb=\"POST\" path=\"/host/register\">\n" + " <blueprint>\n" + " <assembly>dream.tutorial.show-headers</assembly>\n" + " <class>MindTouch.Dream.Tutorial.ShowHeadersService</class>\n" + " </blueprint>\n" + " </action>\n" + "\n" + " <!-- instantiate it -->\n" + " <action verb=\"POST\" path=\"/host/start\">\n" + " <config>\n" + " <path>show-headers</path>\n" + " <class>MindTouch.Dream.Tutorial.ShowHeadersService</class>\n" + " </config>\n" + " </action>\n" + "</script>"; string s = String.Format("POST / HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: {0}\r\n\r\n{1}", message.Length, message); HttpListener2Test.Send(ns, s); bool timedout; string response = HttpListener2Test.ReceiveWithTimeout(ns, 1024, 3000, out timedout); ns.Close(); listener.Close(); Assert.IsFalse(timedout); }
public void Test_MultipleConnections() { HttpListener listener = HttpListener2Test.CreateAndStartListener("http://127.0.0.1:9000/multiple/"); // First one NetworkStream ns = HttpListener2Test.CreateNS(9000); HttpListener2Test.Send(ns, "POST /multiple/ HTTP/1.0\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123"); HttpListenerContext ctx = listener.GetContext(); HttpListener2Test.Send(ctx.Response.OutputStream, "%%%OK%%%"); ctx.Response.OutputStream.Close(); string response = HttpListener2Test.Receive(ns, 1024); ns.Close(); // Second one ns = HttpListener2Test.CreateNS(9000); HttpListener2Test.Send(ns, "POST /multiple/ HTTP/1.0\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123"); ctx = listener.GetContext(); HttpListener2Test.Send(ctx.Response.OutputStream, "%%%OK%%%"); ctx.Response.OutputStream.Close(); response = HttpListener2Test.Receive(ns, 1024); ns.Close(); listener.Close(); }
public void HttpRequestIsLocal() { var port = NetworkHelpers.FindFreePort(); var ips = new List <IPAddress> (); ips.Add(IPAddress.Loopback); foreach (var adapter in NetworkInterface.GetAllNetworkInterfaces()) { if (adapter.OperationalStatus != OperationalStatus.Up) { continue; } foreach (var ip in adapter.GetIPProperties().UnicastAddresses) { ips.Add(ip.Address); } } foreach (var ip in ips) { if (ip.AddressFamily != AddressFamily.InterNetwork) { continue; } HttpListener listener = HttpListener2Test.CreateAndStartListener( "http://" + ip + ":" + port + "/HttpRequestIsLocal/"); NetworkStream ns = HttpListener2Test.CreateNS(ip, port); HttpListener2Test.Send(ns, "GET /HttpRequestIsLocal/ HTTP/1.0\r\n\r\n"); HttpListenerContext ctx = listener.GetContext(); HttpListenerRequest request = ctx.Request; Assert.AreEqual(true, request.IsLocal, "IP " + ip + " is not local"); listener.Close(); } }
void callback (IAsyncResult ar) { HttpListener l = (HttpListener) ar.AsyncState; HttpListenerContext c = l.EndGetContext (ar); HttpListenerRequest request = c.Request; StreamReader r = new StreamReader (request.InputStream); string sr =r.ReadToEnd (); HttpListener2Test.Send (c.Response.OutputStream, "Miguel is love"); c.Response.Close (); }
public void HttpRequestUriIsNotDecoded() { HttpListener listener = HttpListener2Test.CreateAndStartListener( "http://127.0.0.1:9000/RequestUriDecodeTest/"); NetworkStream ns = HttpListener2Test.CreateNS(9000); HttpListener2Test.Send(ns, "GET /RequestUriDecodeTest/?a=b&c=d%26e HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n"); HttpListenerContext ctx = listener.GetContext(); HttpListenerRequest request = ctx.Request; Assert.AreEqual("/RequestUriDecodeTest/?a=b&c=d%26e", request.Url.PathAndQuery); listener.Close(); }
public void HttpMethod() { HttpListener listener = HttpListener2Test.CreateAndStartListener( "http://127.0.0.1:9000/HttpMethod/"); NetworkStream ns = HttpListener2Test.CreateNS(9000); HttpListener2Test.Send(ns, "pOsT /HttpMethod/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123"); HttpListenerContext ctx = listener.GetContext(); HttpListenerRequest request = ctx.Request; Assert.AreEqual("pOsT", request.HttpMethod); listener.Close(); }
public void HttpRequestIgnoreBadCookies() { HttpListener listener = NetworkHelpers.CreateAndStartHttpListener( "http://127.0.0.1:", out var port, "/HttpRequestIgnoreBadCookiesTest/"); NetworkStream ns = HttpListener2Test.CreateNS(port); HttpListener2Test.Send(ns, "GET /HttpRequestIgnoreBadCookiesTest/?a=b HTTP/1.1\r\nHost: 127.0.0.1\r\nCookie: ELOQUA=GUID=5ca2346347357f4-f877-4eff-96aa-70fe0b677650; ELQSTATUS=OK; WRUID=609099666.123259461695; CommunityServer-UserCookie2101=lv=Thu, 26 Jul 2012 15:25:11 GMT&mra=Mon, 01 Oct 2012 17:40:05 GMT; PHPSESSID=1234dg3opfjb4qafp0oo645; __utma=9761706.1153317537.1357240270.1357240270.1357317902.2; __utmb=9761706.6.10.1357317902; __utmc=9761706; __utmz=9761706.1357240270.1.1.utmcsr=test.testdomain.com|utmccn=(referral)|utmcmd=referral|utmcct=/test/1234\r\n\r\n"); HttpListenerContext ctx = listener.GetContext(); HttpListenerRequest request = ctx.Request; Assert.AreEqual("/HttpRequestIgnoreBadCookiesTest/?a=b", request.Url.PathAndQuery); listener.Close(); }
public void HttpBasicAuthScheme() { HttpListener listener = HttpListener2Test.CreateAndStartListener("http://*:9000/authTest/", AuthenticationSchemes.Basic); //dummy-wait for context listener.BeginGetContext(null, listener); NetworkStream ns = HttpListener2Test.CreateNS(9000); HttpListener2Test.Send(ns, "GET /authTest/ HTTP/1.0\r\n\r\n"); String response = HttpListener2Test.Receive(ns, 512); Assert.IsTrue(response.Contains("WWW-Authenticate: Basic realm"), "#A"); ns.Close(); listener.Close(); }
public void Test_EmptyLineAtStart () { var listener = NetworkHelpers.CreateAndStartHttpListener ("http://127.0.0.1:", out var port, "/"); var ns = HttpListener2Test.CreateNS (port); HttpListener2Test.Send (ns, "\r\nGET / HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n"); bool timedout; HttpListener2Test.GetContextWithTimeout (listener, 1000, out timedout); Assert.IsFalse (timedout, "timed out"); ns.Close (); listener.Close (); }
public void HttpRequestIsLocal() { var ips = new List <IPAddress> (Dns.GetHostAddresses(Dns.GetHostName())); ips.Add(IPAddress.Loopback); foreach (var ip in ips) { if (ip.AddressFamily != AddressFamily.InterNetwork) { continue; } HttpListener listener = HttpListener2Test.CreateAndStartListener( "http://" + ip + ":9000/HttpRequestIsLocal/"); NetworkStream ns = HttpListener2Test.CreateNS(ip, 9000); HttpListener2Test.Send(ns, "GET /HttpRequestIsLocal/ HTTP/1.0\r\n\r\n"); HttpListenerContext ctx = listener.GetContext(); HttpListenerRequest request = ctx.Request; Assert.AreEqual(true, request.IsLocal, "IP " + ip + " is not local"); listener.Close(); } }
[Category("NotWorking")] // Bug #5742 public void HasEntityBody() { HttpListenerContext ctx; HttpListenerRequest request; NetworkStream ns; var port = NetworkHelpers.FindFreePort(); HttpListener listener = HttpListener2Test.CreateAndStartListener( "http://127.0.0.1:" + port + "/HasEntityBody/"); // POST with non-zero Content-Lenth ns = HttpListener2Test.CreateNS(port); HttpListener2Test.Send(ns, "POST /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123"); ctx = listener.GetContext(); request = ctx.Request; Assert.IsTrue(request.HasEntityBody, "#A"); HttpListener2Test.Send(ctx.Response.OutputStream, "%%%OK%%%"); // POST with zero Content-Lenth ns = HttpListener2Test.CreateNS(port); HttpListener2Test.Send(ns, "POST /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 0\r\n\r\n123"); ctx = listener.GetContext(); request = ctx.Request; Assert.IsFalse(request.HasEntityBody, "#B"); HttpListener2Test.Send(ctx.Response.OutputStream, "%%%OK%%%"); // POST with chunked encoding ns = HttpListener2Test.CreateNS(port); HttpListener2Test.Send(ns, "POST /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n"); ctx = listener.GetContext(); request = ctx.Request; Assert.IsTrue(request.HasEntityBody, "#C"); HttpListener2Test.Send(ctx.Response.OutputStream, "%%%OK%%%"); // GET with no Content-Length ns = HttpListener2Test.CreateNS(port); HttpListener2Test.Send(ns, "GET /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n"); ctx = listener.GetContext(); request = ctx.Request; Assert.IsFalse(request.HasEntityBody, "#D"); HttpListener2Test.Send(ctx.Response.OutputStream, "%%%OK%%%"); // GET with non-zero Content-Length ns = HttpListener2Test.CreateNS(port); HttpListener2Test.Send(ns, "GET /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n"); ctx = listener.GetContext(); request = ctx.Request; Assert.IsTrue(request.HasEntityBody, "#E"); HttpListener2Test.Send(ctx.Response.OutputStream, "%%%OK%%%"); // GET with zero Content-Length ns = HttpListener2Test.CreateNS(port); HttpListener2Test.Send(ns, "GET /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 0\r\n\r\n"); ctx = listener.GetContext(); request = ctx.Request; Assert.IsFalse(request.HasEntityBody, "#F"); HttpListener2Test.Send(ctx.Response.OutputStream, "%%%OK%%%"); // GET with chunked encoding ns = HttpListener2Test.CreateNS(port); HttpListener2Test.Send(ns, "GET /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n"); ctx = listener.GetContext(); request = ctx.Request; Assert.IsTrue(request.HasEntityBody, "#G"); HttpListener2Test.Send(ctx.Response.OutputStream, "%%%OK%%%"); // PUT with non-zero Content-Lenth ns = HttpListener2Test.CreateNS(port); HttpListener2Test.Send(ns, "PUT /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123"); ctx = listener.GetContext(); request = ctx.Request; Assert.IsTrue(request.HasEntityBody, "#H"); // PUT with zero Content-Lenth ns = HttpListener2Test.CreateNS(port); HttpListener2Test.Send(ns, "PUT /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 0\r\n\r\n123"); ctx = listener.GetContext(); request = ctx.Request; Assert.IsFalse(request.HasEntityBody, "#I"); // INVALID with non-zero Content-Lenth ns = HttpListener2Test.CreateNS(port); HttpListener2Test.Send(ns, "INVALID /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123"); ctx = listener.GetContext(); request = ctx.Request; Assert.IsTrue(request.HasEntityBody, "#J"); // INVALID with zero Content-Lenth ns = HttpListener2Test.CreateNS(port); HttpListener2Test.Send(ns, "INVALID /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 0\r\n\r\n123"); ctx = listener.GetContext(); request = ctx.Request; Assert.IsFalse(request.HasEntityBody, "#K"); // INVALID with chunked encoding ns = HttpListener2Test.CreateNS(port); HttpListener2Test.Send(ns, "INVALID /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n"); ctx = listener.GetContext(); request = ctx.Request; Assert.IsTrue(request.HasEntityBody, "#L"); listener.Close(); }