Skip to content

Commit 3815385

Browse files
committed
Added testing for MediaTypeHeadeValue in Content assertions (#43)
1 parent b19ec67 commit 3815385

File tree

8 files changed

+91
-6
lines changed

8 files changed

+91
-6
lines changed

documentation/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,17 @@ MyWebApi
832832
.ShouldReturn()
833833
.Created();
834834

835+
// tests whether the action returns
836+
// CreatedNegotiatedContentResult<T>
837+
// or CreatedAtRouteNegotiatedContentResult<T>
838+
// with specific response model type
839+
MyWebApi
840+
.Controller<WebApiController>()
841+
.Calling(c => c.SomeAction())
842+
.ShouldReturn()
843+
.Created()
844+
.WithResponseModelOfType<ResponseModel>();
845+
835846
// tests whether the action returns created result
836847
// with DefaultContentNegotiator
837848
MyWebApi

src/MyWebApi.Tests/UtilitiesTests/ValidatorsTests/RuntimeBinderValidatorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void ValidateBindingShouldNotThrowExceptionWithValidPropertyCall()
4242
[Test]
4343
[ExpectedException(
4444
typeof(ActionCallAssertionException),
45-
ExpectedMessage = "Expected action result to contain a property to test, but in fact such property was not found.")]
45+
ExpectedMessage = "Expected action result to contain a 'ModelState' property to test, but in fact such property was not found.")]
4646
public void ValidateBindingShouldThrowExceptionWithInvalidPropertyCall()
4747
{
4848
var actionResultWithFormatters = new CreatedNegotiatedContentResult<int>(
File renamed without changes.

src/MyWebApi/Builders/Contracts/HttpActionResults/Content/IContentTestBuilder.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace MyWebApi.Builders.Contracts.HttpActionResults.Content
2020
using System.Collections.Generic;
2121
using System.Net;
2222
using System.Net.Http.Formatting;
23+
using System.Net.Http.Headers;
2324
using Formatters;
2425
using Models;
2526

@@ -35,6 +36,20 @@ public interface IContentTestBuilder : IBaseResponseModelTestBuilder
3536
/// <returns>The same content test builder.</returns>
3637
IAndContentTestBuilder WithStatusCode(HttpStatusCode statusCode);
3738

39+
/// <summary>
40+
/// Tests whether content result has the same content type as the provided string.
41+
/// </summary>
42+
/// <param name="mediaType">Media type as string.</param>
43+
/// <returns>The same content test builder.</returns>
44+
IAndContentTestBuilder WithMediaType(string mediaType);
45+
46+
/// <summary>
47+
/// Tests whether content result has the same content type as the provided MediaTypeHeaderValue.
48+
/// </summary>
49+
/// <param name="mediaType">Media type as MediaTypeHeaderValue.</param>
50+
/// <returns>The same content test builder.</returns>
51+
IAndContentTestBuilder WithMediaType(MediaTypeHeaderValue mediaType);
52+
3853
/// <summary>
3954
/// Tests whether content result has the default content negotiator.
4055
/// </summary>

src/MyWebApi/Builders/HttpActionResults/Content/ContentTestBuilder.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ namespace MyWebApi.Builders.HttpActionResults.Content
2121
using System.Linq;
2222
using System.Net;
2323
using System.Net.Http.Formatting;
24+
using System.Net.Http.Headers;
2425
using System.Web.Http;
2526
using Common.Extensions;
2627
using Contracts.Formatters;
@@ -78,6 +79,40 @@ public IAndContentTestBuilder WithStatusCode(HttpStatusCode statusCode)
7879
return this;
7980
}
8081

82+
/// <summary>
83+
/// Tests whether content result has the same content type as the provided string.
84+
/// </summary>
85+
/// <param name="mediaType">Media type as string.</param>
86+
/// <returns>The same content test builder.</returns>
87+
public IAndContentTestBuilder WithMediaType(string mediaType)
88+
{
89+
return this.WithMediaType(new MediaTypeHeaderValue(mediaType));
90+
}
91+
92+
/// <summary>
93+
/// Tests whether content result has the same content type as the provided MediaTypeHeaderValue.
94+
/// </summary>
95+
/// <param name="mediaType">Media type as MediaTypeHeaderValue.</param>
96+
/// <returns>The same content test builder.</returns>
97+
public IAndContentTestBuilder WithMediaType(MediaTypeHeaderValue mediaType)
98+
{
99+
RuntimeBinderValidator.ValidateBinding(() =>
100+
{
101+
var actualMediaType = this.GetActionResultAsDynamic().MediaType as MediaTypeHeaderValue;
102+
if ((mediaType == null && actualMediaType != null)
103+
|| (mediaType != null && actualMediaType == null)
104+
|| (mediaType != null && mediaType.MediaType != actualMediaType.MediaType))
105+
{
106+
this.ThrowNewContentResultAssertionException(
107+
"MediaType",
108+
string.Format("to be {0}", mediaType != null ? mediaType.MediaType : null),
109+
string.Format("instead received {0}", actualMediaType != null ? actualMediaType.MediaType : null));
110+
}
111+
});
112+
113+
return this;
114+
}
115+
81116
/// <summary>
82117
/// Tests whether content result has the default content negotiator.
83118
/// </summary>

src/MyWebApi/MyWebApi.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@
148148
<Compile Include="Builders\HttpActionResults\Unauthorized\ChallengesBuilder.cs" />
149149
<Compile Include="Builders\HttpActionResults\Unauthorized\UnauthorizedTestBuilder.cs" />
150150
<Compile Include="Builders\UserBuilder.cs" />
151-
<Compile Include="AuthenticationSchemes.cs" />
151+
<Compile Include="AuthenticationScheme.cs" />
152152
<Compile Include="Common\ActionInfo.cs" />
153153
<Compile Include="Common\Extensions\ObjectExtensions.cs" />
154154
<Compile Include="Common\Extensions\EnumerableExtensions.cs" />

src/MyWebApi/Utilities/Validators/MediaTypeFormatterValidator.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace MyWebApi.Utilities.Validators
2424
using Builders;
2525
using Builders.Contracts.Formatters;
2626
using Common.Extensions;
27+
using Microsoft.CSharp.RuntimeBinder;
2728

2829
/// <summary>
2930
/// Validator class containing MediaTypeFormatter validation logic.
@@ -58,7 +59,7 @@ public static void ValidateMediaTypeFormatter(
5859
{
5960
RuntimeBinderValidator.ValidateBinding(() =>
6061
{
61-
var formatters = actionResult.Formatters as IEnumerable<MediaTypeFormatter>;
62+
var formatters = TryGetMediaTypeFormatters(actionResult) as IEnumerable<MediaTypeFormatter>;
6263
if (formatters == null || formatters.All(f => Reflection.AreDifferentTypes(f, mediaTypeFormatter)))
6364
{
6465
failedValidationAction(
@@ -82,7 +83,7 @@ public static void ValidateMediaTypeFormatters(
8283
{
8384
RuntimeBinderValidator.ValidateBinding(() =>
8485
{
85-
var formatters = actionResult.Formatters as IEnumerable<MediaTypeFormatter>;
86+
var formatters = TryGetMediaTypeFormatters(actionResult) as IEnumerable<MediaTypeFormatter>;
8687
var actualMediaTypeFormatters = SortMediaTypeFormatters(formatters);
8788
var expectedMediaTypeFormatters = SortMediaTypeFormatters(mediaTypeFormatters);
8889

@@ -133,6 +134,26 @@ public static void ValidateMediaTypeFormattersBuilder(
133134
});
134135
}
135136

137+
private static IEnumerable<MediaTypeFormatter> TryGetMediaTypeFormatters(dynamic actionResult)
138+
{
139+
IEnumerable<MediaTypeFormatter> formatters = new List<MediaTypeFormatter>();
140+
141+
try
142+
{
143+
var formatter = actionResult.Formatter as MediaTypeFormatter;
144+
formatters = new List<MediaTypeFormatter> { formatter };
145+
}
146+
catch (RuntimeBinderException)
147+
{
148+
RuntimeBinderValidator.ValidateBinding(() =>
149+
{
150+
formatters = actionResult.Formatters as IEnumerable<MediaTypeFormatter>;
151+
});
152+
}
153+
154+
return formatters;
155+
}
156+
136157
private static IList<string> SortMediaTypeFormatters(IEnumerable<MediaTypeFormatter> mediaTypeFormatters)
137158
{
138159
return mediaTypeFormatters

src/MyWebApi/Utilities/Validators/RuntimeBinderValidator.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ public static void ValidateBinding(Action action)
1919
{
2020
action();
2121
}
22-
catch (RuntimeBinderException)
22+
catch (RuntimeBinderException ex)
2323
{
24-
throw new ActionCallAssertionException("Expected action result to contain a property to test, but in fact such property was not found.");
24+
var fullPropertyName = ex.Message.Split('\'')[3];
25+
throw new ActionCallAssertionException(string.Format(
26+
"Expected action result to contain a '{0}' property to test, but in fact such property was not found.",
27+
fullPropertyName));
2528
}
2629
}
2730
}

0 commit comments

Comments
 (0)