Skip to content

Commit e2e6041

Browse files
committed
fix: add internal modules and validators with corresponding tests
1 parent eda041b commit e2e6041

8 files changed

Lines changed: 292 additions & 3 deletions

test/Carter.Tests/CarterExtensionTests.cs

Lines changed: 179 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace Carter.Tests
22
{
33
using System.Linq;
44
using Carter.Tests.ContentNegotiation;
5+
using Carter.Tests.InternalRooms;
56
using Carter.Tests.ModelBinding;
67
using Carter.Tests.StreamTests;
78
using FluentValidation;
@@ -123,7 +124,7 @@ public void Should_register_responsenegotiators_passed_in_by_configurator_and_de
123124
}
124125

125126
[Fact]
126-
public void Should_register_multiple_responsenegotiators_passed_in_by_configurator_and_default_json_negotiator()
127+
public void Should_register_multiple_response_negotiators_passed_in_by_configurator_and_default_json_negotiator()
127128
{
128129
//Given
129130
var serviceCollection = new ServiceCollection();
@@ -132,8 +133,8 @@ public void Should_register_multiple_responsenegotiators_passed_in_by_configurat
132133
serviceCollection.AddCarter(configurator: configurator => configurator.WithResponseNegotiators(typeof(TestResponseNegotiator), typeof(TestXmlResponseNegotiator)));
133134

134135
//Then
135-
var responsenegotiators = serviceCollection.Where(x => x.ServiceType == typeof(IResponseNegotiator));
136-
Assert.Equal(3,responsenegotiators.Count());
136+
var responseNegotiators = serviceCollection.Where(x => x.ServiceType == typeof(IResponseNegotiator));
137+
Assert.Equal(3, responseNegotiators.Count());
137138
}
138139

139140
[Fact]
@@ -177,5 +178,180 @@ public void Should_register_no_response_negotiators_passed_in_by_configurator()
177178
var responseNegotiators = serviceCollection.Where(x => x.ServiceType == typeof(IResponseNegotiator));
178179
Assert.Single(responseNegotiators);
179180
}
181+
182+
[Fact]
183+
public void Should_register_internal_modules_when_assembly_scanned()
184+
{
185+
//Given
186+
var serviceCollection = new ServiceCollection();
187+
188+
//When
189+
serviceCollection.AddCarter();
190+
191+
//Then
192+
var modules = serviceCollection.Where(x => x.ServiceType == typeof(ICarterModule));
193+
var internalModule = modules.FirstOrDefault(x => x.ImplementationType == typeof(InternalRoomModule));
194+
Assert.NotNull(internalModule);
195+
}
196+
197+
[Fact]
198+
public void Should_register_internal_module_passed_in_by_configurator()
199+
{
200+
//Given
201+
var serviceCollection = new ServiceCollection();
202+
203+
//When
204+
serviceCollection.AddCarter(configurator: configurator => configurator.WithModule<InternalRoomModule>());
205+
206+
//Then
207+
var modules = serviceCollection.Where(x => x.ServiceType == typeof(ICarterModule));
208+
var internalModule = modules.FirstOrDefault(x => x.ImplementationType == typeof(InternalRoomModule));
209+
Assert.NotNull(internalModule);
210+
}
211+
212+
[Fact]
213+
public void Should_register_multiple_internal_modules_passed_in_by_configurator()
214+
{
215+
//Given
216+
var serviceCollection = new ServiceCollection();
217+
218+
//When
219+
serviceCollection.AddCarter(configurator: configurator =>
220+
configurator.WithModules(typeof(InternalRoomModule), typeof(NestedInternalRoomModuleWrapper.NestedInternalRoomModule)));
221+
222+
//Then
223+
var modules = serviceCollection.Where(x => x.ServiceType == typeof(ICarterModule)).ToList();
224+
Assert.Contains(modules, m => m.ImplementationType == typeof(InternalRoomModule));
225+
Assert.Contains(modules, m => m.ImplementationType == typeof(NestedInternalRoomModuleWrapper.NestedInternalRoomModule));
226+
Assert.Equal(2, modules.Count());
227+
}
228+
229+
[Fact]
230+
public void Should_register_internal_validators_when_assembly_scanned()
231+
{
232+
//Given
233+
var serviceCollection = new ServiceCollection();
234+
235+
//When
236+
serviceCollection.AddCarter();
237+
238+
//Then
239+
var validators = serviceCollection.Where(x => x.ServiceType == typeof(IValidator));
240+
var internalValidator = validators.FirstOrDefault(x => x.ImplementationType == typeof(InternalRoomModelValidator));
241+
Assert.NotNull(internalValidator);
242+
}
243+
244+
[Fact]
245+
public void Should_register_internal_validator_passed_in_by_configurator()
246+
{
247+
//Given
248+
var serviceCollection = new ServiceCollection();
249+
250+
//When
251+
serviceCollection.AddCarter(configurator: configurator => configurator.WithValidator<InternalRoomModelValidator>());
252+
253+
//Then
254+
var validators = serviceCollection.Where(x => x.ServiceType == typeof(IValidator)).ToList();
255+
var internalValidator = validators.FirstOrDefault(x => x.ImplementationType == typeof(InternalRoomModelValidator));
256+
Assert.NotNull(internalValidator);
257+
Assert.Single(validators);
258+
}
259+
260+
[Fact]
261+
public void Should_register_multiple_internal_validators_passed_in_by_configurator()
262+
{
263+
//Given
264+
var serviceCollection = new ServiceCollection();
265+
266+
//When
267+
serviceCollection.AddCarter(configurator: configurator =>
268+
configurator.WithValidators(typeof(InternalRoomModelValidator), typeof(NestedInternalTestModelValidatorWrapper.InternalTestModelValidator)));
269+
270+
//Then
271+
var validators = serviceCollection.Where(x => x.ServiceType == typeof(IValidator)).ToList();
272+
Assert.Contains(validators, v => v.ImplementationType == typeof(InternalRoomModelValidator));
273+
Assert.Contains(validators, v => v.ImplementationType == typeof(NestedInternalTestModelValidatorWrapper.InternalTestModelValidator));
274+
Assert.Equal(2, validators.Count());
275+
}
276+
277+
[Fact]
278+
public void Should_register_internal_response_negotiators_when_assembly_scanned()
279+
{
280+
//Given
281+
var serviceCollection = new ServiceCollection();
282+
283+
//When
284+
serviceCollection.AddCarter();
285+
286+
//Then
287+
var responseNegotiators = serviceCollection.Where(x => x.ServiceType == typeof(IResponseNegotiator));
288+
var internalNegotiator = responseNegotiators.FirstOrDefault(x => x.ImplementationType == typeof(InternalResponseNegotiator));
289+
Assert.NotNull(internalNegotiator);
290+
}
291+
292+
[Fact]
293+
public void Should_register_internal_response_negotiator_passed_in_by_configurator()
294+
{
295+
//Given
296+
var serviceCollection = new ServiceCollection();
297+
298+
//When
299+
serviceCollection.AddCarter(configurator: configurator => configurator.WithResponseNegotiator<InternalResponseNegotiator>());
300+
301+
//Then
302+
var responseNegotiators = serviceCollection.Where(x => x.ServiceType == typeof(IResponseNegotiator));
303+
var internalNegotiator = responseNegotiators.FirstOrDefault(x => x.ImplementationType == typeof(InternalResponseNegotiator));
304+
Assert.NotNull(internalNegotiator);
305+
}
306+
307+
[Fact]
308+
public void Should_register_multiple_internal_response_negotiators_passed_in_by_configurator()
309+
{
310+
//Given
311+
var serviceCollection = new ServiceCollection();
312+
313+
//When
314+
serviceCollection.AddCarter(configurator: configurator =>
315+
configurator.WithResponseNegotiators(typeof(InternalResponseNegotiator), typeof(NestedInternalResponseNegotiatorWrapper.NestedInternalResponseNegotiator)));
316+
317+
//Then
318+
var responseNegotiators = serviceCollection.Where(x => x.ServiceType == typeof(IResponseNegotiator)).ToList();
319+
Assert.Contains(responseNegotiators, r => r.ImplementationType == typeof(InternalResponseNegotiator));
320+
Assert.Contains(responseNegotiators, r => r.ImplementationType == typeof(NestedInternalResponseNegotiatorWrapper.NestedInternalResponseNegotiator));
321+
}
322+
323+
[Fact]
324+
public void Should_register_mix_of_public_and_internal_modules()
325+
{
326+
//Given
327+
var serviceCollection = new ServiceCollection();
328+
329+
//When
330+
serviceCollection.AddCarter(configurator: configurator =>
331+
configurator.WithModules(typeof(TestModule), typeof(InternalRoomModule)));
332+
333+
//Then
334+
var modules = serviceCollection.Where(x => x.ServiceType == typeof(ICarterModule)).ToList();
335+
Assert.Contains(modules, m => m.ImplementationType == typeof(TestModule));
336+
Assert.Contains(modules, m => m.ImplementationType == typeof(InternalRoomModule));
337+
Assert.Equal(2, modules.Count());
338+
}
339+
340+
[Fact]
341+
public void Should_register_mix_of_public_and_internal_validators()
342+
{
343+
//Given
344+
var serviceCollection = new ServiceCollection();
345+
346+
//When
347+
serviceCollection.AddCarter(configurator: configurator =>
348+
configurator.WithValidators(typeof(TestModelValidator), typeof(InternalRoomModelValidator)));
349+
350+
//Then
351+
var validators = serviceCollection.Where(x => x.ServiceType == typeof(IValidator)).ToList();
352+
Assert.Contains(validators, v => v.ImplementationType == typeof(TestModelValidator));
353+
Assert.Contains(validators, v => v.ImplementationType == typeof(InternalRoomModelValidator));
354+
Assert.Equal(2, validators.Count());
355+
}
180356
}
181357
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace Carter.Tests.InternalRooms;
2+
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Http;
6+
using Microsoft.Net.Http.Headers;
7+
8+
internal class InternalResponseNegotiator: IResponseNegotiator
9+
{
10+
public bool CanHandle(MediaTypeHeaderValue accept)
11+
{
12+
return true;
13+
}
14+
15+
public Task Handle<T>(HttpRequest req, HttpResponse res, T model, CancellationToken cancellationToken)
16+
{
17+
return Task.CompletedTask;
18+
}
19+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Carter.Tests.InternalRooms;
2+
3+
internal sealed class InternalRoomModel
4+
{
5+
public string Name { get; set; }
6+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Carter.Tests.InternalRooms;
2+
3+
using FluentValidation;
4+
5+
internal class InternalRoomModelValidator : AbstractValidator<InternalRoomModel>
6+
{
7+
public InternalRoomModelValidator()
8+
{
9+
this.RuleFor(x => x.Name).NotEmpty();
10+
}
11+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace Carter.Tests.InternalRooms;
2+
3+
using Microsoft.AspNetCore.Builder;
4+
using Microsoft.AspNetCore.Http;
5+
using Microsoft.AspNetCore.Routing;
6+
7+
internal class InternalRoomModule : ICarterModule
8+
{
9+
public void AddRoutes(IEndpointRouteBuilder app)
10+
{
11+
app.MapGet("/", (HttpResponse res) =>
12+
{
13+
res.StatusCode = 409;
14+
return Results.Text("There's no place like 127.0.0.1");
15+
});
16+
}
17+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace Carter.Tests.InternalRooms;
2+
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Http;
6+
using Microsoft.Net.Http.Headers;
7+
8+
internal static class NestedInternalResponseNegotiatorWrapper
9+
{
10+
internal class NestedInternalResponseNegotiator: IResponseNegotiator
11+
{
12+
public bool CanHandle(MediaTypeHeaderValue accept)
13+
{
14+
return true;
15+
}
16+
17+
public Task Handle<T>(HttpRequest req, HttpResponse res, T model, CancellationToken cancellationToken)
18+
{
19+
return Task.CompletedTask;
20+
}
21+
}
22+
}
23+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace Carter.Tests.InternalRooms;
2+
3+
using FluentValidation;
4+
5+
internal static class NestedInternalTestModelValidatorWrapper
6+
{
7+
internal class InternalTestModelValidator : AbstractValidator<InternalRoomModel>
8+
{
9+
public InternalTestModelValidator()
10+
{
11+
this.RuleFor(x => x.Name).NotEmpty();
12+
}
13+
}
14+
}
15+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace Carter.Tests.InternalRooms;
2+
3+
using Carter;
4+
using Microsoft.AspNetCore.Builder;
5+
using Microsoft.AspNetCore.Http;
6+
using Microsoft.AspNetCore.Routing;
7+
8+
internal static class NestedInternalRoomModuleWrapper
9+
{
10+
internal class NestedInternalRoomModule : ICarterModule
11+
{
12+
public void AddRoutes(IEndpointRouteBuilder app)
13+
{
14+
app.MapGet("/nested-room", (HttpResponse res) =>
15+
{
16+
res.StatusCode = 409;
17+
return Results.Text("There's no place like 127.0.0.1");
18+
});
19+
}
20+
}
21+
}
22+

0 commit comments

Comments
 (0)