Skip to content

Commit 82fbd5a

Browse files
committed
adding Session Delete test
1 parent febb97f commit 82fbd5a

File tree

4 files changed

+113
-2
lines changed

4 files changed

+113
-2
lines changed

test/Unosquare.Labs.EmbedIO.Tests/FluentTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void FluentWithWebApi()
4040

4141
Assert.AreEqual(webServer.Modules.Count, 1, "It has 1 modules loaded");
4242
Assert.IsNotNull(webServer.Module<WebApiModule>(), "It has WebApiModule");
43-
Assert.AreEqual(webServer.Module<WebApiModule>().ControllersCount, 3, "It has three controllers");
43+
Assert.AreEqual(webServer.Module<WebApiModule>().ControllersCount, 4, "It has four controllers");
4444

4545
webServer.Dispose();
4646
}
@@ -66,7 +66,7 @@ public void FluentLoadWebApiControllers()
6666

6767
Assert.AreEqual(webServer.Modules.Count, 1, "It has 1 modules loaded");
6868
Assert.IsNotNull(webServer.Module<WebApiModule>(), "It has WebApiModule");
69-
Assert.AreEqual(webServer.Module<WebApiModule>().ControllersCount, 3, "It has three controllers");
69+
Assert.AreEqual(webServer.Module<WebApiModule>().ControllersCount, 4, "It has four controllers");
7070

7171
webServer.Dispose();
7272
}

test/Unosquare.Labs.EmbedIO.Tests/LocalSessionModuleTest.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Threading.Tasks;
77
using Modules;
88
using TestObjects;
9+
using System.IO;
910

1011
[TestFixture]
1112
public class LocalSessionModuleTest
@@ -28,6 +29,8 @@ public void Init()
2829
WebServer = new WebServer(WebServerUrl);
2930
WebServer.RegisterModule(new LocalSessionModule() { Expiration = WaitTimeSpan });
3031
WebServer.RegisterModule(new StaticFilesModule(RootPath));
32+
WebServer.RegisterModule(new WebApiModule());
33+
WebServer.Module<WebApiModule>().RegisterController<TestLocalSessionController>();
3134
WebServer.RunAsync();
3235
}
3336

@@ -101,6 +104,65 @@ protected async Task GetFile(string content)
101104
}
102105
}
103106

107+
[Test]
108+
public async Task DeleteSession()
109+
{
110+
var request = (HttpWebRequest)WebRequest.Create(WebServerUrl);
111+
CookieContainer container = new CookieContainer();
112+
request.CookieContainer = container;
113+
114+
request = (HttpWebRequest)WebRequest.Create(WebServerUrl + TestLocalSessionController.PutData);
115+
116+
request.CookieContainer = container;
117+
118+
using (var response = (HttpWebResponse)await request.GetResponseAsync())
119+
{
120+
Assert.AreEqual(response.StatusCode, HttpStatusCode.OK, "Status Code OK");
121+
122+
var body = new StreamReader(response.GetResponseStream()).ReadToEnd();
123+
124+
Assert.AreEqual(body, TestLocalSessionController.MyData);
125+
}
126+
127+
request = (HttpWebRequest)WebRequest.Create(WebServerUrl + TestLocalSessionController.GetData);
128+
129+
request.CookieContainer = container;
130+
131+
using (var response = (HttpWebResponse)await request.GetResponseAsync())
132+
{
133+
Assert.AreEqual(response.StatusCode, HttpStatusCode.OK, "Status Code OK");
134+
135+
var body = new StreamReader(response.GetResponseStream()).ReadToEnd();
136+
137+
Assert.AreEqual(body, TestLocalSessionController.MyData);
138+
}
139+
140+
request = (HttpWebRequest)WebRequest.Create(WebServerUrl + TestLocalSessionController.DeleteSession);
141+
request.CookieContainer = container;
142+
143+
using (var response = (HttpWebResponse)await request.GetResponseAsync())
144+
{
145+
Assert.AreEqual(response.StatusCode, HttpStatusCode.OK, "Status Code OK");
146+
147+
var body = new StreamReader(response.GetResponseStream()).ReadToEnd();
148+
149+
Assert.AreEqual(body, "Deleted");
150+
}
151+
152+
request = (HttpWebRequest)WebRequest.Create(WebServerUrl + TestLocalSessionController.GetData);
153+
154+
request.CookieContainer = container;
155+
156+
using (var response = (HttpWebResponse)await request.GetResponseAsync())
157+
{
158+
Assert.AreEqual(response.StatusCode, HttpStatusCode.OK, "Status Code OK");
159+
160+
var body = new StreamReader(response.GetResponseStream()).ReadToEnd();
161+
162+
Assert.AreEqual("", body);
163+
}
164+
}
165+
104166
[TearDown]
105167
public void Kill()
106168
{
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
namespace Unosquare.Labs.EmbedIO.Tests.TestObjects
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
using Unosquare.Labs.EmbedIO.Modules;
8+
#if NET46
9+
using System.Net;
10+
#else
11+
using Net;
12+
#endif
13+
14+
public class TestLocalSessionController : WebApiController
15+
{
16+
public const string DeleteSession = "deletesession";
17+
public const string PutData = "putdata";
18+
public const string GetData = "getdata";
19+
public const string MyData = "MyData";
20+
21+
[WebApiHandler(HttpVerbs.Get, "/deletesession")]
22+
public bool DeleteSessionC(WebServer server, HttpListenerContext context)
23+
{
24+
server.DeleteSession(context);
25+
26+
return context.JsonResponse("Deleted");
27+
}
28+
29+
[WebApiHandler(HttpVerbs.Get, "/putdata")]
30+
public bool PutDataSession(WebServer server, HttpListenerContext context)
31+
{
32+
server.GetSession(context).Data.TryAdd("sessionData", MyData);
33+
34+
return context.JsonResponse(server.GetSession(context).Data["sessionData"].ToString());
35+
}
36+
37+
[WebApiHandler(HttpVerbs.Get, "/getdata")]
38+
public bool GetDataSession(WebServer server, HttpListenerContext context)
39+
{
40+
object _data = null;
41+
if (server.GetSession(context).Data.TryGetValue("sessionData", out _data))
42+
return context.JsonResponse(server.GetSession(context).Data["sessionData"].ToString());
43+
else
44+
return context.JsonResponse("");
45+
46+
}
47+
}
48+
}

test/Unosquare.Labs.EmbedIO.Tests/Unosquare.Labs.EmbedIO.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
<Compile Include="TestObjects\Resources.cs" />
6464
<Compile Include="TestObjects\TestController.cs" />
6565
<Compile Include="TestObjects\TestHelper.cs" />
66+
<Compile Include="TestObjects\TestLocalSessionController.cs" />
6667
<Compile Include="TestObjects\TestRegexController.cs" />
6768
<Compile Include="TestObjects\TestWebModule.cs" />
6869
<Compile Include="TestObjects\TestWebSocket.cs" />

0 commit comments

Comments
 (0)