Skip to content

Commit

Permalink
adding Session Delete test
Browse files Browse the repository at this point in the history
  • Loading branch information
kuyoska committed Apr 14, 2017
1 parent febb97f commit 82fbd5a
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 2 deletions.
4 changes: 2 additions & 2 deletions test/Unosquare.Labs.EmbedIO.Tests/FluentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void FluentWithWebApi()

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

webServer.Dispose();
}
Expand All @@ -66,7 +66,7 @@ public void FluentLoadWebApiControllers()

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

webServer.Dispose();
}
Expand Down
62 changes: 62 additions & 0 deletions test/Unosquare.Labs.EmbedIO.Tests/LocalSessionModuleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using Modules;
using TestObjects;
using System.IO;

[TestFixture]
public class LocalSessionModuleTest
Expand All @@ -28,6 +29,8 @@ public void Init()
WebServer = new WebServer(WebServerUrl);
WebServer.RegisterModule(new LocalSessionModule() { Expiration = WaitTimeSpan });
WebServer.RegisterModule(new StaticFilesModule(RootPath));
WebServer.RegisterModule(new WebApiModule());
WebServer.Module<WebApiModule>().RegisterController<TestLocalSessionController>();
WebServer.RunAsync();
}

Expand Down Expand Up @@ -101,6 +104,65 @@ protected async Task GetFile(string content)
}
}

[Test]
public async Task DeleteSession()
{
var request = (HttpWebRequest)WebRequest.Create(WebServerUrl);
CookieContainer container = new CookieContainer();
request.CookieContainer = container;

request = (HttpWebRequest)WebRequest.Create(WebServerUrl + TestLocalSessionController.PutData);

request.CookieContainer = container;

using (var response = (HttpWebResponse)await request.GetResponseAsync())
{
Assert.AreEqual(response.StatusCode, HttpStatusCode.OK, "Status Code OK");

var body = new StreamReader(response.GetResponseStream()).ReadToEnd();

Assert.AreEqual(body, TestLocalSessionController.MyData);
}

request = (HttpWebRequest)WebRequest.Create(WebServerUrl + TestLocalSessionController.GetData);

request.CookieContainer = container;

using (var response = (HttpWebResponse)await request.GetResponseAsync())
{
Assert.AreEqual(response.StatusCode, HttpStatusCode.OK, "Status Code OK");

var body = new StreamReader(response.GetResponseStream()).ReadToEnd();

Assert.AreEqual(body, TestLocalSessionController.MyData);
}

request = (HttpWebRequest)WebRequest.Create(WebServerUrl + TestLocalSessionController.DeleteSession);
request.CookieContainer = container;

using (var response = (HttpWebResponse)await request.GetResponseAsync())
{
Assert.AreEqual(response.StatusCode, HttpStatusCode.OK, "Status Code OK");

var body = new StreamReader(response.GetResponseStream()).ReadToEnd();

Assert.AreEqual(body, "Deleted");
}

request = (HttpWebRequest)WebRequest.Create(WebServerUrl + TestLocalSessionController.GetData);

request.CookieContainer = container;

using (var response = (HttpWebResponse)await request.GetResponseAsync())
{
Assert.AreEqual(response.StatusCode, HttpStatusCode.OK, "Status Code OK");

var body = new StreamReader(response.GetResponseStream()).ReadToEnd();

Assert.AreEqual("", body);
}
}

[TearDown]
public void Kill()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
namespace Unosquare.Labs.EmbedIO.Tests.TestObjects
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Unosquare.Labs.EmbedIO.Modules;
#if NET46
using System.Net;
#else
using Net;
#endif

public class TestLocalSessionController : WebApiController
{
public const string DeleteSession = "deletesession";
public const string PutData = "putdata";
public const string GetData = "getdata";
public const string MyData = "MyData";

[WebApiHandler(HttpVerbs.Get, "/deletesession")]
public bool DeleteSessionC(WebServer server, HttpListenerContext context)
{
server.DeleteSession(context);

return context.JsonResponse("Deleted");
}

[WebApiHandler(HttpVerbs.Get, "/putdata")]
public bool PutDataSession(WebServer server, HttpListenerContext context)
{
server.GetSession(context).Data.TryAdd("sessionData", MyData);

return context.JsonResponse(server.GetSession(context).Data["sessionData"].ToString());
}

[WebApiHandler(HttpVerbs.Get, "/getdata")]
public bool GetDataSession(WebServer server, HttpListenerContext context)
{
object _data = null;
if (server.GetSession(context).Data.TryGetValue("sessionData", out _data))
return context.JsonResponse(server.GetSession(context).Data["sessionData"].ToString());
else
return context.JsonResponse("");

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
<Compile Include="TestObjects\Resources.cs" />
<Compile Include="TestObjects\TestController.cs" />
<Compile Include="TestObjects\TestHelper.cs" />
<Compile Include="TestObjects\TestLocalSessionController.cs" />
<Compile Include="TestObjects\TestRegexController.cs" />
<Compile Include="TestObjects\TestWebModule.cs" />
<Compile Include="TestObjects\TestWebSocket.cs" />
Expand Down

0 comments on commit 82fbd5a

Please sign in to comment.