Skip to content

Commit a25bc65

Browse files
committed
Added MiddlewareFilterAttributeTestBuilder controller attribute unit tests and applied some minor style fixes.
1 parent 46ce668 commit a25bc65

File tree

8 files changed

+139
-24
lines changed

8 files changed

+139
-24
lines changed

src/MyTested.AspNetCore.Mvc.Controllers.Attributes/Builders/Attributes/MiddlewareFilterAttributeTestBuilder.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
namespace MyTested.AspNetCore.Mvc.Builders.Attributes
22
{
3+
using System;
34
using Microsoft.AspNetCore.Mvc;
45
using Contracts.Attributes;
5-
using System;
66
using Internal.TestContexts;
77
using Utilities;
88

@@ -24,7 +24,7 @@ public MiddlewareFilterAttributeTestBuilder(
2424
=> this.Attribute = new MiddlewareFilterAttribute(typeof(object));
2525

2626
/// <inheritdoc />
27-
public IAndMiddlewareFilterAttributeTestBuilder WithType(Type configurationType)
27+
public IAndMiddlewareFilterAttributeTestBuilder OfType(Type configurationType)
2828
{
2929
this.Attribute = new MiddlewareFilterAttribute(configurationType);
3030
this.Validations.Add((expected, actual) =>
@@ -43,6 +43,7 @@ public IAndMiddlewareFilterAttributeTestBuilder WithType(Type configurationType)
4343
return this;
4444
}
4545

46+
/// <inheritdoc />
4647
public IAndMiddlewareFilterAttributeTestBuilder WithOrder(int order)
4748
{
4849
this.ValidateOrder(order);

src/MyTested.AspNetCore.Mvc.Controllers.Attributes/Builders/Attributes/ProducesAttributeTestBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public IAndProducesAttributeTestBuilder WithContentTypes(string contentType, par
7070
=> this.WithContentTypes(new List<string>(otherContentTypes) { contentType });
7171

7272
/// <inheritdoc />
73-
public IAndProducesAttributeTestBuilder WithType(Type type)
73+
public IAndProducesAttributeTestBuilder OfType(Type type)
7474
{
7575
this.Attribute.Type = type;
7676
this.Validations.Add((expected, actual) =>

src/MyTested.AspNetCore.Mvc.Controllers.Attributes/Builders/Contracts/Attributes/IMiddlewareFilterAttributeTestBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ public interface IMiddlewareFilterAttributeTestBuilder : IBaseAttributeTestBuild
1111
/// Tests whether a <see cref="Microsoft.AspNetCore.Mvc.MiddlewareFilterAttribute"/>
1212
/// has the same <see cref="Microsoft.AspNetCore.Mvc.MiddlewareFilterAttribute.ConfigurationType"/> value as the provided one.
1313
/// </summary>
14-
/// <param name="type">Expected type value.</param>
14+
/// <param name="configurationType">A type which configures a middleware pipeline.</param>
1515
/// <returns>The same <see cref="IMiddlewareFilterAttributeTestBuilder"/>.</returns>
16-
IAndMiddlewareFilterAttributeTestBuilder WithType(Type type);
16+
IAndMiddlewareFilterAttributeTestBuilder OfType(Type configurationType);
1717
}
1818
}

src/MyTested.AspNetCore.Mvc.Controllers.Attributes/Builders/Contracts/Attributes/IProducesAttributeTestBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ public interface IProducesAttributeTestBuilder : IBaseAttributeTestBuilderWithOr
3939
/// </summary>
4040
/// <param name="type">Expected type value.</param>
4141
/// <returns>The same <see cref="IAndProducesAttributeTestBuilder"/>.</returns>
42-
IAndProducesAttributeTestBuilder WithType(Type type);
42+
IAndProducesAttributeTestBuilder OfType(Type type);
4343
}
4444
}

src/MyTested.AspNetCore.Mvc.Controllers.Attributes/ControllerActionAttributesTestBuilderExtensions.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ public static TAttributesTestBuilder SpecifyingProduction<TAttributesTestBuilder
272272
Type withType)
273273
where TAttributesTestBuilder : IControllerActionAttributesTestBuilder<TAttributesTestBuilder>
274274
=> controllerActionAttributesTestBuilder
275-
.SpecifyingProduction(production => production.WithType(withType));
275+
.SpecifyingProduction(production => production.OfType(withType));
276276

277277
/// <summary>
278278
/// Tests whether the collected attributes contain <see cref="ProducesAttribute"/>.
@@ -290,7 +290,7 @@ public static TAttributesTestBuilder SpecifyingProduction<TAttributesTestBuilder
290290
where TAttributesTestBuilder : IControllerActionAttributesTestBuilder<TAttributesTestBuilder>
291291
=> controllerActionAttributesTestBuilder
292292
.SpecifyingProduction(production => production
293-
.WithType(withType)
293+
.OfType(withType)
294294
.WithContentTypes(withContentTypes));
295295

296296
/// <summary>
@@ -358,8 +358,7 @@ public static TAttributesTestBuilder SpecifyingMiddleware<TAttributesTestBuilder
358358
Type configurationType)
359359
where TAttributesTestBuilder : IControllerActionAttributesTestBuilder<TAttributesTestBuilder>
360360
=> controllerActionAttributesTestBuilder
361-
.SpecifyingMiddleware(middleware =>
362-
middleware.WithType(configurationType));
361+
.SpecifyingMiddleware(middleware => middleware.OfType(configurationType));
363362

364363
/// <summary>
365364
/// Tests whether the collected attributes contain <see cref="MiddlewareFilterAttribute"/>.

test/MyTested.AspNetCore.Mvc.Controllers.Attributes.Test/BuildersTests/AttributesTests/ActionAttributesTestBuilderTests.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ public void SpecifyingProductionShouldNotThrowExceptionWithCorrectAttributeTypeA
500500
.ShouldHave()
501501
.ActionAttributes(attributes => attributes
502502
.SpecifyingProduction(production => production
503-
.WithType(typeof(RequestModel))
503+
.OfType(typeof(RequestModel))
504504
.WithContentTypes("application/javascript", "application/pdf")
505505
.WithOrder(2)));
506506
}
@@ -526,7 +526,7 @@ public void SpecifyingProductionShouldNotThrowExceptionWithCorrectAttributeTypeA
526526
.ShouldHave()
527527
.ActionAttributes(attributes => attributes
528528
.SpecifyingProduction(production => production
529-
.WithType(typeof(RequestModel))
529+
.OfType(typeof(RequestModel))
530530
.AndAlso()
531531
.WithContentTypes(new List<string> { "application/pdf", "application/javascript" })
532532
.AndAlso()
@@ -601,8 +601,8 @@ public void SpecifyingMiddlewareShouldNotThrowExceptionWithCorrectAttributeConfi
601601
.Calling(c => c.VariousAttributesAction())
602602
.ShouldHave()
603603
.ActionAttributes(attributes => attributes
604-
.SpecifyingMiddleware(middleware =>
605-
middleware.WithType(typeof(MyPipeline))));
604+
.SpecifyingMiddleware(middleware => middleware
605+
.OfType(typeof(MyPipeline))));
606606
}
607607

608608
[Fact]
@@ -616,8 +616,8 @@ public void SpecifyingMiddlewareShouldThrowExceptionWithWrongConfigurationType()
616616
.Calling(c => c.VariousAttributesAction())
617617
.ShouldHave()
618618
.ActionAttributes(attributes => attributes
619-
.SpecifyingMiddleware(middleware =>
620-
middleware.WithType(typeof(MyOtherPipeline))));
619+
.SpecifyingMiddleware(middleware => middleware
620+
.OfType(typeof(MyOtherPipeline))));
621621
},
622622
"When calling VariousAttributesAction action in MvcController expected action to have MiddlewareFilterAttribute with 'MyOtherPipeline' type, but in fact found 'MyPipeline'.");
623623
}
@@ -633,8 +633,7 @@ public void SpecifyingMiddlewareShouldThrowExceptionWithCorrectAttributeAndWrong
633633
.Calling(c => c.VariousAttributesAction())
634634
.ShouldHave()
635635
.ActionAttributes(attributes => attributes
636-
.SpecifyingMiddleware(middleware =>
637-
middleware.WithOrder(1)));
636+
.SpecifyingMiddleware(middleware => middleware.WithOrder(1)));
638637
},
639638
"When calling VariousAttributesAction action in MvcController expected action to have MiddlewareFilterAttribute with order of 1, but in fact found 2.");
640639
}
@@ -647,8 +646,7 @@ public void SpecifyingMiddlewareShouldNotThrowExceptionWithCorrectAttributeTypeA
647646
.Calling(c => c.VariousAttributesAction())
648647
.ShouldHave()
649648
.ActionAttributes(attributes => attributes
650-
.SpecifyingMiddleware(middleware =>
651-
middleware.WithOrder(2)));
649+
.SpecifyingMiddleware(middleware => middleware.WithOrder(2)));
652650
}
653651

654652
[Fact]
@@ -660,7 +658,7 @@ public void SpecifyingMiddlewareShouldNotThrowExceptionWithCorrectAttributeConfi
660658
.ShouldHave()
661659
.ActionAttributes(attributes => attributes
662660
.SpecifyingMiddleware(middleware => middleware
663-
.WithType(typeof(MyPipeline))
661+
.OfType(typeof(MyPipeline))
664662
.AndAlso()
665663
.WithOrder(2)));
666664
}
@@ -677,7 +675,7 @@ public void SpecifyingMiddlewareShouldThrowExceptionWithCorrectAttributeConfigTy
677675
.ShouldHave()
678676
.ActionAttributes(attributes => attributes
679677
.SpecifyingMiddleware(middleware => middleware
680-
.WithType(typeof(MyPipeline))
678+
.OfType(typeof(MyPipeline))
681679
.AndAlso()
682680
.WithOrder(1)));
683681
},

test/MyTested.AspNetCore.Mvc.Controllers.Attributes.Test/BuildersTests/AttributesTests/ControllerAttributesTestBuilderTests.cs

Lines changed: 117 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Setups;
77
using Setups.Controllers;
88
using Setups.Models;
9+
using Setups.Pipelines;
910
using Xunit;
1011

1112
public class ControllerAttributesTestBuilderTests
@@ -393,7 +394,7 @@ public void SpecifyingProductionShouldNotThrowExceptionWithCorrectAttributeTypeA
393394
.ShouldHave()
394395
.Attributes(attributes => attributes
395396
.SpecifyingProduction(production => production
396-
.WithType(typeof(ResponseModel))
397+
.OfType(typeof(ResponseModel))
397398
.WithContentTypes("application/xml", "application/json")
398399
.WithOrder(1)));
399400
}
@@ -415,7 +416,7 @@ public void SpecifyingProductionShouldNotThrowExceptionWithCorrectAttributeTypeA
415416
.ShouldHave()
416417
.Attributes(attributes => attributes
417418
.SpecifyingProduction(production => production
418-
.WithType(typeof(ResponseModel))
419+
.OfType(typeof(ResponseModel))
419420
.AndAlso()
420421
.WithContentTypes(new List<string> { "application/xml", "application/json" })
421422
.AndAlso()
@@ -437,6 +438,120 @@ public void SpecifyingProductionShouldThrowExceptionWithCorrectAttributeAndWrong
437438
"When testing ApiController was expected to have ProducesAttribute with order of 2, but in fact found 1.");
438439
}
439440

441+
[Fact]
442+
public void SpecifyingMiddlewareShouldNotThrowExceptionWithCorrectAttribute()
443+
{
444+
MyController<ApiController>
445+
.ShouldHave()
446+
.Attributes(attributes => attributes
447+
.SpecifyingMiddleware(typeof(MyPipeline)));
448+
}
449+
450+
[Fact]
451+
public void SpecifyingMiddlewareShouldThrowExceptionWithMissingAttribute()
452+
{
453+
Test.AssertException<AttributeAssertionException>(
454+
() =>
455+
{
456+
MyController<AttributesController>
457+
.ShouldHave()
458+
.Attributes(attributes => attributes
459+
.SpecifyingMiddleware(typeof(MyPipeline)));
460+
},
461+
"When testing AttributesController was expected to have MiddlewareFilterAttribute, but in fact such was not found.");
462+
}
463+
464+
[Fact]
465+
public void SpecifyingMiddlewareShouldThrowExceptionWithCorrectAttributeAndWrongConfigurationType()
466+
{
467+
Test.AssertException<AttributeAssertionException>(
468+
() =>
469+
{
470+
MyController<ApiController>
471+
.ShouldHave()
472+
.Attributes(attributes => attributes
473+
.SpecifyingMiddleware(typeof(MyOtherPipeline)));
474+
},
475+
"When testing ApiController was expected to have MiddlewareFilterAttribute with 'MyOtherPipeline' type, but in fact found 'MyPipeline'.");
476+
}
477+
478+
[Fact]
479+
public void SpecifyingMiddlewareShouldNotThrowExceptionWithCorrectAttributeConfigurationType()
480+
{
481+
MyController<ApiController>
482+
.ShouldHave()
483+
.Attributes(attributes => attributes
484+
.SpecifyingMiddleware(middleware => middleware
485+
.OfType(typeof(MyPipeline))));
486+
}
487+
488+
[Fact]
489+
public void SpecifyingMiddlewareShouldThrowExceptionWithWrongConfigurationType()
490+
{
491+
Test.AssertException<AttributeAssertionException>(
492+
() =>
493+
{
494+
MyController<ApiController>
495+
.ShouldHave()
496+
.Attributes(attributes => attributes
497+
.SpecifyingMiddleware(middleware => middleware
498+
.OfType(typeof(MyOtherPipeline))));
499+
},
500+
"When testing ApiController was expected to have MiddlewareFilterAttribute with 'MyOtherPipeline' type, but in fact found 'MyPipeline'.");
501+
}
502+
503+
[Fact]
504+
public void SpecifyingMiddlewareShouldThrowExceptionWithCorrectAttributeAndWrongOrder()
505+
{
506+
Test.AssertException<AttributeAssertionException>(
507+
() =>
508+
{
509+
MyController<ApiController>
510+
.ShouldHave()
511+
.Attributes(attributes => attributes
512+
.SpecifyingMiddleware(middleware => middleware.WithOrder(1)));
513+
},
514+
"When testing ApiController was expected to have MiddlewareFilterAttribute with order of 1, but in fact found 2.");
515+
}
516+
517+
[Fact]
518+
public void SpecifyingMiddlewareShouldNotThrowExceptionWithCorrectAttributeTypeAndUsingBuilderForOrder()
519+
{
520+
MyController<ApiController>
521+
.ShouldHave()
522+
.Attributes(attributes => attributes
523+
.SpecifyingMiddleware(middleware => middleware.WithOrder(2)));
524+
}
525+
526+
[Fact]
527+
public void SpecifyingMiddlewareShouldNotThrowExceptionWithCorrectAttributeConfigTypeAndUsingBuilderForOrder()
528+
{
529+
MyController<ApiController>
530+
.ShouldHave()
531+
.Attributes(attributes => attributes
532+
.SpecifyingMiddleware(middleware => middleware
533+
.OfType(typeof(MyPipeline))
534+
.AndAlso()
535+
.WithOrder(2)));
536+
}
537+
538+
[Fact]
539+
public void SpecifyingMiddlewareShouldThrowExceptionWithCorrectAttributeConfigTypeAndUsingBuilderForOrderWithWrongOrder()
540+
{
541+
Test.AssertException<AttributeAssertionException>(
542+
() =>
543+
{
544+
MyController<ApiController>
545+
.ShouldHave()
546+
.Attributes(attributes => attributes
547+
.SpecifyingMiddleware(middleware => middleware
548+
.OfType(typeof(MyPipeline))
549+
.AndAlso()
550+
.WithOrder(1)));
551+
},
552+
"When testing ApiController was expected to have MiddlewareFilterAttribute with order of 1, but in fact found 2.");
553+
}
554+
440555
[Fact]
441556
public void RequiringHttpsShouldNotThrowExceptionWithTheAttribute()
442557
{

test/MyTested.AspNetCore.Mvc.Test.Setups/Controllers/ApiController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
{
33
using Microsoft.AspNetCore.Mvc;
44
using Models;
5+
using Pipelines;
56

67
[ApiController]
78
[Consumes("application/json", "application/xml")]
89
[Produces("application/json", "application/xml", Type = typeof(ResponseModel), Order = 1)]
10+
[MiddlewareFilter(typeof(MyPipeline), Order = 2)]
911
public class ApiController : Controller
1012
{
1113
[Route("/route")]

0 commit comments

Comments
 (0)