public void TestMiniWebServerOpenClose() { Console.WriteLine("Test to Open/Close the Mini Web Server."); MiniWebServer server = new MiniWebServer(); server.SetupListeningSocket(); server.CloseListeningSocket(); Console.WriteLine("Mini Web Server Open/Close Test successful."); }
public void TestMiniWebServerRequest() { Console.WriteLine("Test to see if requesting works."); MiniWebServer server = new MiniWebServer(); server.Port = 80; server.SetupListeningSocket(); bool wait = true; server.RequestReceived += delegate(MiniWebServer request_server, MiniWebServer.Request request) { Console.WriteLine("Request received: "); Console.WriteLine("URL: " + request.Url); Console.WriteLine("Method: " + request.Method); Console.WriteLine("Version: " + request.Version); Console.WriteLine("Headers:"); foreach (string key in request.Headers.Keys) { Console.WriteLine("[" + key + "]" + ":[" + request.Headers.Get(key) + "]"); } //request.RequestClient.TellNotFound(); //request.RequestClient.Answer("HTTP/1.0 302 Moved Temporarily\r\nSERVER: MiniWebServer\r\nLOCATION: http://192.168.0.1:80/Public_UPNP_gatedesc.xml\r\n\r\n"); if (request.Url == "/") { string page = ""; //string type = "text/plain"; page = "<html>\n<head>\n<title>MiniWebServer Test Page</title>\n</head>\n<body bgcolor=\"#333355\">Test Page of the Miniwebserver running on port: " + server.Port + "<br><a href=\"/test.mp3\">Test Mp3</a></body>\n</html>\n"; string type = "text/html"; request.RequestClient.Answer(page, type); } else if (request.Url == "/test.mp3") { byte[] mp3 = File.ReadAllBytes("..\\..\\..\\TestDateien\\test.mp3"); string type = "audio/mpeg"; request.RequestClient.Answer(mp3, type); } //request.RequestClient.Answer("HTTP/1.1 200 OK\r\nSERVER: MiniWebServer\r\nContent-Type: text/plain\r\nContent-Length: " + page.Length + "\r\nConnection: Close\r\n\r\n" + page); //Thread.Sleep(300); //request.RequestClient.Disconnect(); //Assert.IsTrue(!string.IsNullOrEmpty(ex_ip_completed.MyIP), "no ip address fetched"); //wait = false; }; Console.WriteLine("Waiting for data"); DateTime start = DateTime.Now; while (wait) { if (DateTime.Now - start > new TimeSpan(0, 0,120)) { Console.WriteLine(""); Console.WriteLine("Operation took too long"); Assert.Fail("Operation took too long"); wait = false; } Console.Write("."); Thread.Sleep(250); } Console.WriteLine(""); server.CloseListeningSocket(); Console.WriteLine("Mini Web Server Request Test successful."); }
public void TestMiniWebServerAutoFileAnswerRequest() { Console.WriteLine("Test to see if requesting using the file answer method works."); MiniWebServer server = new MiniWebServer(); server.Port = 80; server.SetupListeningSocket(); long page_len = 0; long ret_page_len = 1; //to make sure no false positive could occur bool wait = true; server.RequestReceived += delegate(MiniWebServer request_server, MiniWebServer.Request request) { Console.WriteLine("Request received: "); Console.WriteLine("URL: " + request.Url); Console.WriteLine("Method: " + request.Method); Console.WriteLine("Version: " + request.Version); Console.WriteLine("Headers:"); foreach (string key in request.Headers.Keys) { Console.WriteLine("[" + key + "]" + ":[" + request.Headers.Get(key) + "]"); } //request.RequestClient.TellNotFound(); //request.RequestClient.Answer("HTTP/1.0 302 Moved Temporarily\r\nSERVER: MiniWebServer\r\nLOCATION: http://192.168.0.1:80/Public_UPNP_gatedesc.xml\r\n\r\n"); if (request.Url == "/") { string page = ""; //string type = "text/plain"; page = "<html>\n<head>\n<title>MiniWebServer Test Page</title>\n</head>\n<body bgcolor=\"#333355\">Test Page of the Miniwebserver running on port: " + server.Port + "<br><a href=\"/test.mp3\">Test Mp3</a></body>\n</html>\n"; string type = "text/html"; page_len = page.Length; request.RequestClient.Answer(page, type); } else if (request.Url == "/test.mp3") { FileInfo fi = new FileInfo("..\\..\\..\\TestDateien\\test.mp3"); page_len = fi.Length; request.RequestClient.FileAnswer("..\\..\\..\\TestDateien\\test.mp3"); } //request.RequestClient.Answer("HTTP/1.1 200 OK\r\nSERVER: MiniWebServer\r\nContent-Type: text/plain\r\nContent-Length: " + page.Length + "\r\nConnection: Close\r\n\r\n" + page); //Thread.Sleep(300); //request.RequestClient.Disconnect(); //Assert.IsTrue(!string.IsNullOrEmpty(ex_ip_completed.MyIP), "no ip address fetched"); //wait = false; }; WebClient wc = new WebClient(); wc.DownloadDataCompleted += delegate(object sender, DownloadDataCompletedEventArgs e) { if (!e.Cancelled && e.Result != null) { for (int i = 0; i < wc.ResponseHeaders.Count; i++) { Console.WriteLine("Header[" + i + "]: " + wc.ResponseHeaders.GetKey(i)+" : "+wc.ResponseHeaders[i]); } //Console.WriteLine("Content-Type: " + wc.Headers.Get("Content-Type")); ret_page_len = e.Result.Length; wait = false; } }; wc.DownloadDataAsync(new Uri("http://127.0.0.1/test.mp3")); Console.WriteLine("Waiting for data"); DateTime start = DateTime.Now; while (wait) { if (DateTime.Now - start > new TimeSpan(0, 0, 120)) { Console.WriteLine(""); Console.WriteLine("Operation took too long"); Assert.Fail("Operation took too long"); wait = false; } Console.Write("."); Thread.Sleep(250); } Assert.IsTrue(page_len == ret_page_len, "Test failed: received an incomplete page"); Console.WriteLine(""); server.CloseListeningSocket(); wc.Dispose(); Console.WriteLine("Mini Web Server Request using File Answer Method Test successful."); }