Skip to content

Commit b2a18a8

Browse files
committed
#283 Adding comments for the public methods in the session project.
Including tests for the newly introduced logic for clearing session key or the whole http session.
1 parent 989010b commit b2a18a8

File tree

7 files changed

+189
-68
lines changed

7 files changed

+189
-68
lines changed
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Data
22
{
3-
using System.Collections.Generic;
4-
53
/// <summary>
64
/// Used for building <see cref="Microsoft.AspNetCore.Http.ISession"/>.
75
/// </summary>
86
public interface IWithoutSessionBuilder
97
{
8+
/// <summary>
9+
/// Removes session key from <see cref="Microsoft.AspNetCore.Http.ISession"/>.
10+
/// </summary>
11+
/// <param name="key">The key to remove as string.</param>
12+
/// <returns>The same <see cref="IAndWithoutSessionBuilder"/>.</returns>
1013
IAndWithoutSessionBuilder WithoutEntry(string key);
1114

15+
/// <summary>
16+
/// Clears the whole <see cref="Microsoft.AspNetCore.Http.ISession"/>.
17+
/// </summary>
18+
/// <returns>The same <see cref="IAndWithoutSessionBuilder"/>.</returns>
1219
IAndWithoutSessionBuilder ClearSession();
1320
}
1421
}

src/MyTested.AspNetCore.Mvc.Session/Builders/Data/SessionBaseBuilder.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ public abstract class SessionBaseBuilder
1010
/// <value>The built <see cref="ISession"/>.</value>
1111
protected ISession Session { get; }
1212

13+
/// <summary>
14+
/// Abstract base <see cref="SessionBaseBuilder"/> class.
15+
/// </summary>
16+
/// <param name="session"><see cref="ISession"/> to built.</param>
1317
public SessionBaseBuilder(ISession session) => this.Session = session;
1418
}
1519
}

src/MyTested.AspNetCore.Mvc.Session/Builders/Data/WithoutSessionBuilder.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,34 @@
33
using Contracts.Data;
44
using Microsoft.AspNetCore.Http;
55

6+
/// <summary>
7+
/// Used for building <see cref="ISession"/>.
8+
/// </summary>
69
public class WithoutSessionBuilder : SessionBaseBuilder, IAndWithoutSessionBuilder
710
{
11+
/// <summary>
12+
/// Initializes a new instance of the <see cref="WithoutSessionBuilder"/> class.
13+
/// </summary>
14+
/// <param name="session"><see cref="ISession"/> to built.</param>
815
public WithoutSessionBuilder(ISession session) : base(session)
916
{
1017
}
1118

19+
/// <inheritdoc />
1220
public IAndWithoutSessionBuilder WithoutEntry(string key)
1321
{
1422
this.Session.Remove(key);
1523
return this;
1624
}
1725

26+
/// <inheritdoc />
1827
public IAndWithoutSessionBuilder ClearSession()
1928
{
2029
this.Session.Clear();
2130
return this;
2231
}
2332

33+
/// <inheritdoc />
2434
public IWithoutSessionBuilder AndAlso() => this;
2535
}
2636
}

src/MyTested.AspNetCore.Mvc.Session/ComponentBuilderSessionExtensions.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
namespace MyTested.AspNetCore.Mvc
22
{
3-
using System;
43
using Builders.Base;
54
using Builders.Contracts.Base;
65
using Builders.Contracts.Data;
76
using Builders.Data;
7+
using System;
88

99
/// <summary>
1010
/// Contains <see cref="Microsoft.AspNetCore.Http.ISession"/> extension methods for <see cref="IBaseTestBuilderWithComponentBuilder{TBuilder}"/>.
@@ -30,11 +30,24 @@ public static TBuilder WithSession<TBuilder>(
3030
return actualBuilder.Builder;
3131
}
3232

33+
/// <summary>
34+
/// Clears the whole http <see cref="Microsoft.AspNetCore.Http.ISession"/>.
35+
/// </summary>
36+
/// <typeparam name="TBuilder">Class representing ASP.NET Core MVC test builder.</typeparam>
37+
/// <param name="builder">Instance of <see cref="IBaseTestBuilderWithComponentBuilder{TBuilder}"/> type.</param>
38+
/// <returns>The same component builder.</returns>
3339
public static TBuilder WithoutSession<TBuilder>(
3440
this IBaseTestBuilderWithComponentBuilder<TBuilder> builder)
3541
where TBuilder : IBaseTestBuilder
3642
=> builder.WithoutSession(session => session.ClearSession());
3743

44+
/// <summary>
45+
/// Remove session key from the HTTP <see cref="Microsoft.AspNetCore.Http.ISession"/>.
46+
/// </summary>
47+
/// <typeparam name="TBuilder">Class representing ASP.NET Core MVC test builder.</typeparam>
48+
/// <param name="builder">Instance of <see cref="IBaseTestBuilderWithComponentBuilder{TBuilder}"/> type.</param>
49+
/// <param name="sessionBuilder">Action setting the <see cref="Microsoft.AspNetCore.Http.ISession"/> values by using <see cref="IWithoutSessionBuilder"/>.</param>
50+
/// <returns>The same component builder.</returns>
3851
public static TBuilder WithoutSession<TBuilder>(
3952
this IBaseTestBuilderWithComponentBuilder<TBuilder> builder,
4053
Action<IWithoutSessionBuilder> sessionBuilder)

test/MyTested.AspNetCore.Mvc.Session.Test/BuildersTests/ActionsTests/ShouldHaveTests/ShouldHaveSessionTests.cs

Lines changed: 20 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,7 @@ public class ShouldHaveSessionTests
1111
[Fact]
1212
public void NoSessionShouldNotThrowExceptionWithNoEntries()
1313
{
14-
MyApplication
15-
.StartsFrom<DefaultStartup>()
16-
.WithServices(services =>
17-
{
18-
services.AddMemoryCache();
19-
services.AddDistributedMemoryCache();
20-
services.AddSession();
21-
});
14+
this.SetDefaultSession();
2215

2316
MyController<MvcController>
2417
.Instance()
@@ -35,14 +28,7 @@ public void NoSessionShouldNotThrowExceptionWithNoEntries()
3528
[Fact]
3629
public void NoSessionShouldThrowExceptionWithEntries()
3730
{
38-
MyApplication
39-
.StartsFrom<DefaultStartup>()
40-
.WithServices(services =>
41-
{
42-
services.AddMemoryCache();
43-
services.AddDistributedMemoryCache();
44-
services.AddSession();
45-
});
31+
this.SetDefaultSession();
4632

4733
Test.AssertException<DataProviderAssertionException>(
4834
() =>
@@ -64,14 +50,7 @@ public void NoSessionShouldThrowExceptionWithEntries()
6450
[Fact]
6551
public void SessionWithNoNumberShouldNotThrowExceptionWithAnyEntries()
6652
{
67-
MyApplication
68-
.StartsFrom<DefaultStartup>()
69-
.WithServices(services =>
70-
{
71-
services.AddMemoryCache();
72-
services.AddDistributedMemoryCache();
73-
services.AddSession();
74-
});
53+
this.SetDefaultSession();
7554

7655
MyController<MvcController>
7756
.Instance()
@@ -88,14 +67,7 @@ public void SessionWithNoNumberShouldNotThrowExceptionWithAnyEntries()
8867
[Fact]
8968
public void SessionWithNoNumberShouldThrowExceptionWithNoEntries()
9069
{
91-
MyApplication
92-
.StartsFrom<DefaultStartup>()
93-
.WithServices(services =>
94-
{
95-
services.AddMemoryCache();
96-
services.AddDistributedMemoryCache();
97-
services.AddSession();
98-
});
70+
this.SetDefaultSession();
9971

10072
Test.AssertException<DataProviderAssertionException>(
10173
() =>
@@ -117,14 +89,7 @@ public void SessionWithNoNumberShouldThrowExceptionWithNoEntries()
11789
[Fact]
11890
public void SessionWithNumberShouldNotThrowExceptionWithCorrectEntries()
11991
{
120-
MyApplication
121-
.StartsFrom<DefaultStartup>()
122-
.WithServices(services =>
123-
{
124-
services.AddMemoryCache();
125-
services.AddDistributedMemoryCache();
126-
services.AddSession();
127-
});
92+
this.SetDefaultSession();
12893

12994
MyController<MvcController>
13095
.Instance()
@@ -141,14 +106,7 @@ public void SessionWithNumberShouldNotThrowExceptionWithCorrectEntries()
141106
[Fact]
142107
public void SessionWithNumberShouldThrowExceptionWithInvalidEntries()
143108
{
144-
MyApplication
145-
.StartsFrom<DefaultStartup>()
146-
.WithServices(services =>
147-
{
148-
services.AddMemoryCache();
149-
services.AddDistributedMemoryCache();
150-
services.AddSession();
151-
});
109+
this.SetDefaultSession();
152110

153111
Test.AssertException<DataProviderAssertionException>(
154112
() =>
@@ -170,14 +128,7 @@ public void SessionWithNumberShouldThrowExceptionWithInvalidEntries()
170128
[Fact]
171129
public void SessionWithNumberShouldThrowExceptionWithInvalidManyEntries()
172130
{
173-
MyApplication
174-
.StartsFrom<DefaultStartup>()
175-
.WithServices(services =>
176-
{
177-
services.AddMemoryCache();
178-
services.AddDistributedMemoryCache();
179-
services.AddSession();
180-
});
131+
this.SetDefaultSession();
181132

182133
Test.AssertException<DataProviderAssertionException>(
183134
() =>
@@ -199,14 +150,7 @@ public void SessionWithNumberShouldThrowExceptionWithInvalidManyEntries()
199150
[Fact]
200151
public void SessionWithBuilderShouldWorkCorrectly()
201152
{
202-
MyApplication
203-
.StartsFrom<DefaultStartup>()
204-
.WithServices(services =>
205-
{
206-
services.AddMemoryCache();
207-
services.AddDistributedMemoryCache();
208-
services.AddSession();
209-
});
153+
this.SetDefaultSession();
210154

211155
MyController<MvcController>
212156
.Instance()
@@ -220,5 +164,17 @@ public void SessionWithBuilderShouldWorkCorrectly()
220164

221165
MyApplication.StartsFrom<DefaultStartup>();
222166
}
167+
168+
private void SetDefaultSession()
169+
{
170+
MyApplication
171+
.StartsFrom<DefaultStartup>()
172+
.WithServices(services =>
173+
{
174+
services.AddMemoryCache();
175+
services.AddDistributedMemoryCache();
176+
services.AddSession();
177+
});
178+
}
223179
}
224180
}

test/MyTested.AspNetCore.Mvc.Session.Test/BuildersTests/ControllerTests/ControllerBuilderTests.cs

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
namespace MyTested.AspNetCore.Mvc.Test.BuildersTests.ControllerTests
22
{
33
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
46
using Microsoft.Extensions.DependencyInjection;
57
using Setups;
68
using Setups.Controllers;
@@ -53,7 +55,7 @@ public void WithSessionShouldThrowExceptionIfSessionIsNotSet()
5355
},
5456
"Session has not been configured for this application or request.");
5557
}
56-
58+
5759
[Fact]
5860
public void WithSessionShouldPopulateSessionCorrectlyForPocoController()
5961
{
@@ -97,5 +99,124 @@ public void WithSessionShouldThrowExceptionIfSessionIsNotSetForPocoController()
9799
},
98100
"Session has not been configured for this application or request.");
99101
}
102+
103+
[Fact]
104+
public void RemovingSessionEntryByKeyShouldReturnCorrectSession()
105+
{
106+
this.SetDefaultSession();
107+
108+
IDictionary<string, string> entries = new Dictionary<string, string>
109+
{
110+
{ "testKey1", "testValue1" },
111+
{ "testKey2", "testValue2" }
112+
};
113+
114+
var keyToRemove = "testKey1";
115+
116+
var keys = entries.Keys.ToList();
117+
keys.Remove(keyToRemove);
118+
119+
MyController<MvcController>
120+
.Instance()
121+
.WithSession(session =>
122+
{
123+
session.WithEntries(entries);
124+
})
125+
.WithoutSession(session => session.WithoutEntry(keyToRemove))
126+
.Calling(c => c.GetSessionKeys())
127+
.ShouldReturn()
128+
.Ok(ok => ok
129+
.WithModel(keys));
130+
131+
MyApplication.StartsFrom<DefaultStartup>();
132+
}
133+
134+
[Fact]
135+
public void ClearingSessionShouldReturnCorrectEmptyCollectionOfKeys()
136+
{
137+
this.SetDefaultSession();
138+
139+
IDictionary<string, string> entries = new Dictionary<string, string>
140+
{
141+
{ "testKey1", "testValue1" },
142+
{ "testKey2", "testValue2" }
143+
};
144+
145+
MyController<MvcController>
146+
.Instance()
147+
.WithSession(session =>
148+
{
149+
session.WithEntries(entries);
150+
})
151+
.WithoutSession()
152+
.Calling(c => c.GetSessionKeysCount())
153+
.ShouldReturn()
154+
.Ok(ok => ok
155+
.WithModel(0));
156+
157+
MyApplication.StartsFrom<DefaultStartup>();
158+
}
159+
160+
[Fact]
161+
public void ClearingEmptySessionShouldReturnCorrectEmptyCollectionOfKeys()
162+
{
163+
this.SetDefaultSession();
164+
165+
MyController<MvcController>
166+
.Instance()
167+
.WithoutSession()
168+
.Calling(c => c.GetSessionKeysCount())
169+
.ShouldReturn()
170+
.Ok(ok => ok
171+
.WithModel(0));
172+
173+
MyApplication.StartsFrom<DefaultStartup>();
174+
}
175+
176+
[Fact]
177+
public void RemovingSessionEntryByKeyAndAlsoShouldReturnCorrectSession()
178+
{
179+
this.SetDefaultSession();
180+
181+
IDictionary<string, string> entries = new Dictionary<string, string>
182+
{
183+
{ "testKey1", "testValue1" },
184+
{ "testKey2", "testValue2" },
185+
{ "testKey3", "testValue3" },
186+
{ "testKey4", "testValue4" }
187+
};
188+
189+
var keysToRemove = new List<string> { "testKey1", "testKey3" };
190+
var keys = entries.Keys.ToList();
191+
keysToRemove.ForEach(key => keys.Remove(key));
192+
193+
MyController<MvcController>
194+
.Instance()
195+
.WithSession(session =>
196+
{
197+
session.WithEntries(entries);
198+
})
199+
.WithoutSession(session => session.WithoutEntry(keysToRemove.First()))
200+
.AndAlso()
201+
.WithoutSession(session => session.WithoutEntry(keysToRemove.Last()))
202+
.Calling(c => c.GetSessionKeys())
203+
.ShouldReturn()
204+
.Ok(ok => ok
205+
.WithModel(keys));
206+
207+
MyApplication.StartsFrom<DefaultStartup>();
208+
}
209+
210+
private void SetDefaultSession()
211+
{
212+
MyApplication
213+
.StartsFrom<DefaultStartup>()
214+
.WithServices(services =>
215+
{
216+
services.AddMemoryCache();
217+
services.AddDistributedMemoryCache();
218+
services.AddSession();
219+
});
220+
}
100221
}
101222
}

0 commit comments

Comments
 (0)