Skip to content

Commit 0193d88

Browse files
authored
Adding E2E test application and fixing OutputHttpCode when no content (#240)
1 parent ec4022e commit 0193d88

18 files changed

+426
-15
lines changed

nanoFramework.WebServer.FileSystem/nanoFramework.WebServer.FileSystem.nfproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
<Private>True</Private>
105105
</Reference>
106106
<Reference Include="System.Net">
107-
<HintPath>..\packages\nanoFramework.System.Net.1.10.68\lib\System.Net.dll</HintPath>
107+
<HintPath>..\packages\nanoFramework.System.Net.1.10.70\lib\System.Net.dll</HintPath>
108108
</Reference>
109109
<Reference Include="System.Net.Http">
110110
<HintPath>..\packages\nanoFramework.System.Net.Http.Server.1.5.118\lib\System.Net.Http.dll</HintPath>

nanoFramework.WebServer.FileSystem/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<package id="nanoFramework.System.Collections" version="1.5.31" targetFramework="netnano1.0" />
66
<package id="nanoFramework.System.IO.FileSystem" version="1.1.32" targetFramework="netnano1.0" />
77
<package id="nanoFramework.System.IO.Streams" version="1.1.52" targetFramework="netnano1.0" />
8-
<package id="nanoFramework.System.Net" version="1.10.68" targetFramework="netnano1.0" />
8+
<package id="nanoFramework.System.Net" version="1.10.70" targetFramework="netnano1.0" />
99
<package id="nanoFramework.System.Net.Http.Server" version="1.5.118" targetFramework="netnano1.0" />
1010
<package id="nanoFramework.System.Runtime" version="1.0.6" targetFramework="netnano1.0" />
1111
<package id="nanoFramework.System.Text" version="1.2.54" targetFramework="netnano1.0" />

nanoFramework.WebServer.FileSystem/packages.lock.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
},
3535
"nanoFramework.System.Net": {
3636
"type": "Direct",
37-
"requested": "[1.10.68, 1.10.68]",
38-
"resolved": "1.10.68",
39-
"contentHash": "PoM4NadhtMBnuGiOPqi+TJZD8xicz0v72hG5gPGDvWXeKDCMYCMqiCqY+di2pRL3qGHkF+hh8kLOf+wBUJBWNQ=="
37+
"requested": "[1.10.70, 1.10.70]",
38+
"resolved": "1.10.70",
39+
"contentHash": "XP4XdmsCDrXcOJDSfo4zgqccTwPoygBH8y19pnVmrF/EYwbx4WzaMvRE5VnYKu2bp10T78su16+jxzeFZADqDw=="
4040
},
4141
"nanoFramework.System.Net.Http.Server": {
4242
"type": "Direct",

nanoFramework.WebServer.sln

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "nanoFramework.WebServer.Fil
1515
EndProject
1616
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "nanoFramework.WebServer.Tests", "tests\nanoFramework.WebServer.Tests\nanoFramework.WebServer.Tests.nfproj", "{2C2B4750-2A48-4D19-9404-178AAB946482}"
1717
EndProject
18+
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "WebServerE2ETests", "tests\WebServerE2ETests\WebServerE2ETests.nfproj", "{A0611EAD-FB04-44E7-BAD3-459DD0A7FF46}"
19+
EndProject
20+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{E76226D2-994C-4EE1-B346-050F31B175BD}"
21+
EndProject
1822
Global
1923
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2024
Debug|Any CPU = Debug|Any CPU
@@ -39,10 +43,20 @@ Global
3943
{2C2B4750-2A48-4D19-9404-178AAB946482}.Release|Any CPU.ActiveCfg = Release|Any CPU
4044
{2C2B4750-2A48-4D19-9404-178AAB946482}.Release|Any CPU.Build.0 = Release|Any CPU
4145
{2C2B4750-2A48-4D19-9404-178AAB946482}.Release|Any CPU.Deploy.0 = Release|Any CPU
46+
{A0611EAD-FB04-44E7-BAD3-459DD0A7FF46}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
47+
{A0611EAD-FB04-44E7-BAD3-459DD0A7FF46}.Debug|Any CPU.Build.0 = Debug|Any CPU
48+
{A0611EAD-FB04-44E7-BAD3-459DD0A7FF46}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
49+
{A0611EAD-FB04-44E7-BAD3-459DD0A7FF46}.Release|Any CPU.ActiveCfg = Release|Any CPU
50+
{A0611EAD-FB04-44E7-BAD3-459DD0A7FF46}.Release|Any CPU.Build.0 = Release|Any CPU
51+
{A0611EAD-FB04-44E7-BAD3-459DD0A7FF46}.Release|Any CPU.Deploy.0 = Release|Any CPU
4252
EndGlobalSection
4353
GlobalSection(SolutionProperties) = preSolution
4454
HideSolutionNode = FALSE
4555
EndGlobalSection
56+
GlobalSection(NestedProjects) = preSolution
57+
{2C2B4750-2A48-4D19-9404-178AAB946482} = {E76226D2-994C-4EE1-B346-050F31B175BD}
58+
{A0611EAD-FB04-44E7-BAD3-459DD0A7FF46} = {E76226D2-994C-4EE1-B346-050F31B175BD}
59+
EndGlobalSection
4660
GlobalSection(ExtensibilityGlobals) = postSolution
4761
SolutionGuid = {262CE437-AD82-4481-8B77-593288986C70}
4862
EndGlobalSection

nanoFramework.WebServer/WebServer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ private void StartListener()
560560
}
561561

562562
InvokeRoute(route, context);
563+
HandleContextResponse(context);
563564
}
564565

565566
if (!isRoute)

nanoFramework.WebServer/nanoFramework.WebServer.nfproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
<Private>True</Private>
6969
</Reference>
7070
<Reference Include="System.Net">
71-
<HintPath>..\packages\nanoFramework.System.Net.1.10.68\lib\System.Net.dll</HintPath>
71+
<HintPath>..\packages\nanoFramework.System.Net.1.10.70\lib\System.Net.dll</HintPath>
7272
</Reference>
7373
<Reference Include="System.Net.Http">
7474
<HintPath>..\packages\nanoFramework.System.Net.Http.Server.1.5.118\lib\System.Net.Http.dll</HintPath>

nanoFramework.WebServer/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<package id="nanoFramework.Runtime.Events" version="1.11.15" targetFramework="netnano1.0" />
55
<package id="nanoFramework.System.Collections" version="1.5.31" targetFramework="netnano1.0" />
66
<package id="nanoFramework.System.IO.Streams" version="1.1.52" targetFramework="netnano1.0" />
7-
<package id="nanoFramework.System.Net" version="1.10.68" targetFramework="netnano1.0" />
7+
<package id="nanoFramework.System.Net" version="1.10.70" targetFramework="netnano1.0" />
88
<package id="nanoFramework.System.Net.Http.Server" version="1.5.118" targetFramework="netnano1.0" />
99
<package id="nanoFramework.System.Text" version="1.2.54" targetFramework="netnano1.0" />
1010
<package id="nanoFramework.System.Threading" version="1.1.32" targetFramework="netnano1.0" />

nanoFramework.WebServer/packages.lock.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
},
2929
"nanoFramework.System.Net": {
3030
"type": "Direct",
31-
"requested": "[1.10.68, 1.10.68]",
32-
"resolved": "1.10.68",
33-
"contentHash": "PoM4NadhtMBnuGiOPqi+TJZD8xicz0v72hG5gPGDvWXeKDCMYCMqiCqY+di2pRL3qGHkF+hh8kLOf+wBUJBWNQ=="
31+
"requested": "[1.10.70, 1.10.70]",
32+
"resolved": "1.10.70",
33+
"contentHash": "XP4XdmsCDrXcOJDSfo4zgqccTwPoygBH8y19pnVmrF/EYwbx4WzaMvRE5VnYKu2bp10T78su16+jxzeFZADqDw=="
3434
},
3535
"nanoFramework.System.Net.Http.Server": {
3636
"type": "Direct",
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.WebServer;
5+
using System.Net;
6+
7+
namespace WebServerE2ETests
8+
{
9+
[Authentication("Basic")]
10+
class AuthController
11+
{
12+
[Route("authbasic")]
13+
public void Basic(WebServerEventArgs e)
14+
{
15+
WebServer.OutputHttpCode(e.Context.Response, HttpStatusCode.OK);
16+
}
17+
18+
[Route("authbasicspecial")]
19+
[Authentication("Basic:user2 password")]
20+
public void Special(WebServerEventArgs e)
21+
{
22+
WebServer.OutputHttpCode(e.Context.Response, HttpStatusCode.OK);
23+
}
24+
25+
[Authentication("ApiKey:superKey1234")]
26+
[Route("authapi")]
27+
public void Key(WebServerEventArgs e)
28+
{
29+
WebServer.OutputHttpCode(e.Context.Response, HttpStatusCode.OK);
30+
}
31+
32+
[Route("authnone")]
33+
[Authentication("None")]
34+
public void None(WebServerEventArgs e)
35+
{
36+
WebServer.OutputHttpCode(e.Context.Response, HttpStatusCode.OK);
37+
}
38+
39+
[Authentication("ApiKey")]
40+
[Route("authdefaultapi")]
41+
public void DefaultApi(WebServerEventArgs e)
42+
{
43+
WebServer.OutputHttpCode(e.Context.Response, HttpStatusCode.OK);
44+
}
45+
}
46+
}

tests/WebServerE2ETests/Program.cs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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

Comments
 (0)