|
| 1 | +// Copyright (c) 2020 Laurent Ellerbach and the project contributors |
| 2 | +// See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +using nanoFramework.Networking; |
| 5 | +using nanoFramework.WebServer; |
| 6 | +using System; |
| 7 | +using System.Diagnostics; |
| 8 | +using System.IO; |
| 9 | +using System.Net; |
| 10 | +using System.Net.NetworkInformation; |
| 11 | +using System.Threading; |
| 12 | + |
| 13 | +namespace WebServerE2ETests |
| 14 | +{ |
| 15 | + public class Program |
| 16 | + { |
| 17 | + private const string Ssid = "yourSSID"; |
| 18 | + private const string Password = "YourPAssword"; |
| 19 | + private static WebServer _server; |
| 20 | + |
| 21 | + public static void Main() |
| 22 | + { |
| 23 | + Debug.WriteLine("Hello from nanoFramework WebServer end to end tests!"); |
| 24 | + |
| 25 | + var res = WifiNetworkHelper.ConnectDhcp(Ssid, Password, requiresDateTime: true, token: new CancellationTokenSource(60_000).Token); |
| 26 | + if (!res) |
| 27 | + { |
| 28 | + Debug.WriteLine("Impossible to connect to wifi, most likely invalid credentials"); |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + Debug.WriteLine($"Connected with wifi credentials. IP Address: {GetCurrentIPAddress()}"); |
| 33 | + _server = new WebServer(80, HttpProtocol.Http, new Type[] { typeof(SimpleRouteController), typeof(AuthController) }); |
| 34 | + // To test authentication with various scenarios |
| 35 | + _server.ApiKey = "ATopSecretAPIKey1234"; |
| 36 | + _server.Credential = new NetworkCredential("topuser", "topPassword"); |
| 37 | + // Add a handler for commands that are received by the server. |
| 38 | + _server.CommandReceived += ServerCommandReceived; |
| 39 | + |
| 40 | + // Start the server. |
| 41 | + _server.Start(); |
| 42 | + |
| 43 | + Thread.Sleep(Timeout.Infinite); |
| 44 | + |
| 45 | + // Browse our samples repository: https://github.com/nanoframework/samples |
| 46 | + // Check our documentation online: https://docs.nanoframework.net/ |
| 47 | + // Join our lively Discord community: https://discord.gg/gCyBu8T |
| 48 | + } |
| 49 | + |
| 50 | + private static void ServerCommandReceived(object obj, WebServerEventArgs e) |
| 51 | + { |
| 52 | + const string FileName = "I:\\Text.txt"; |
| 53 | + var url = e.Context.Request.RawUrl; |
| 54 | + Debug.WriteLine($"{nameof(ServerCommandReceived)} {e.Context.Request.HttpMethod} {url}"); |
| 55 | + |
| 56 | + if (url.ToLower().IndexOf("/param.htm") == 0) |
| 57 | + { |
| 58 | + // Test with parameters |
| 59 | + var parameters = WebServer.DecodeParam(url); |
| 60 | + string toOutput = "<html><head>" + |
| 61 | + "<title>Hi from nanoFramework Server</title></head><body>Here are the parameters of this URL: <br />"; |
| 62 | + foreach (var par in parameters) |
| 63 | + { |
| 64 | + toOutput += $"Parameter name: {par.Name}, Value: {par.Value}<br />"; |
| 65 | + } |
| 66 | + toOutput += "</body></html>"; |
| 67 | + WebServer.OutPutStream(e.Context.Response, toOutput); |
| 68 | + return; |
| 69 | + } |
| 70 | + else if (url.IndexOf("/Text.txt") == 0) |
| 71 | + { |
| 72 | + if (File.Exists(FileName)) |
| 73 | + { |
| 74 | + WebServer.SendFileOverHTTP(e.Context.Response, FileName); |
| 75 | + return; |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + WebServer.OutputHttpCode(e.Context.Response, HttpStatusCode.NotFound); |
| 80 | + return; |
| 81 | + } |
| 82 | + } |
| 83 | + else if (url.ToLower().IndexOf("/useinternal") == 0) |
| 84 | + { |
| 85 | + File.WriteAllText(FileName, "This is a test file for WebServer"); |
| 86 | + } |
| 87 | + else |
| 88 | + { |
| 89 | + WebServer.OutPutStream(e.Context.Response, "<html><head>" + |
| 90 | + "<title>Hi from nanoFramework Server</title></head><body>You want me to say hello in a real HTML page!<br/><a href='/useinternal'>Generate an internal text.txt file</a><br />" + |
| 91 | + "<a href='/Text.txt'>Download the Text.txt file</a><br>" + |
| 92 | + "Try this url with parameters: <a href='/param.htm?param1=42&second=24&NAme=Ellerbach'>/param.htm?param1=42&second=24&NAme=Ellerbach</a></body></html>"); |
| 93 | + return; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public static string GetCurrentIPAddress() |
| 98 | + { |
| 99 | + NetworkInterface ni = NetworkInterface.GetAllNetworkInterfaces()[0]; |
| 100 | + |
| 101 | + // get first NI ( Wifi on ESP32 ) |
| 102 | + return ni.IPv4Address.ToString(); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments