1717namespace MyWebApi . Tests . BuildersTests . HttpActionResultsTests . ContentTests
1818{
1919 using System . Collections . Generic ;
20+ using System . Linq ;
2021 using System . Net ;
22+ using System . Net . Http . Formatting ;
23+ using System . Net . Http . Headers ;
2124 using Exceptions ;
2225 using NUnit . Framework ;
26+ using Setups ;
27+ using Setups . Common ;
2328 using Setups . Controllers ;
2429 using Setups . Models ;
2530
@@ -51,6 +56,232 @@ public void WithStatusCodeShouldThrowExceptionWhenActionReturnsWrongStatusCode()
5156 . WithStatusCode ( HttpStatusCode . NotFound ) ;
5257 }
5358
59+ [ Test ]
60+ public void WithMediaTypeShouldNotThrowExceptionWithString ( )
61+ {
62+ MyWebApi
63+ . Controller < WebApiController > ( )
64+ . Calling ( c => c . ContentActionWithMediaType ( ) )
65+ . ShouldReturn ( )
66+ . Content ( )
67+ . WithMediaType ( TestObjectFactory . MediaType ) ;
68+ }
69+
70+ [ Test ]
71+ public void WithMediaTypeShouldNotThrowExceptionWithMediaTypeHeaderValue ( )
72+ {
73+ MyWebApi
74+ . Controller < WebApiController > ( )
75+ . Calling ( c => c . ContentActionWithMediaType ( ) )
76+ . ShouldReturn ( )
77+ . Content ( )
78+ . WithMediaType ( new MediaTypeHeaderValue ( TestObjectFactory . MediaType ) ) ;
79+ }
80+
81+ [ Test ]
82+ [ ExpectedException (
83+ typeof ( ContentResultAssertionException ) ,
84+ ExpectedMessage = "When calling ContentActionWithMediaType action in WebApiController expected content result MediaType to be text/plain, but instead received application/json." ) ]
85+ public void WithMediaTypeShouldThrowExceptionWithMediaTypeHeaderValue ( )
86+ {
87+ MyWebApi
88+ . Controller < WebApiController > ( )
89+ . Calling ( c => c . ContentActionWithMediaType ( ) )
90+ . ShouldReturn ( )
91+ . Content ( )
92+ . WithMediaType ( new MediaTypeHeaderValue ( "text/plain" ) ) ;
93+ }
94+
95+ [ Test ]
96+ [ ExpectedException (
97+ typeof ( ContentResultAssertionException ) ,
98+ ExpectedMessage = "When calling ContentActionWithMediaType action in WebApiController expected content result MediaType to be null, but instead received application/json." ) ]
99+ public void WithMediaTypeShouldThrowExceptionWithNullMediaTypeHeaderValue ( )
100+ {
101+ MyWebApi
102+ . Controller < WebApiController > ( )
103+ . Calling ( c => c . ContentActionWithMediaType ( ) )
104+ . ShouldReturn ( )
105+ . Content ( )
106+ . WithMediaType ( ( MediaTypeHeaderValue ) null ) ;
107+ }
108+
109+ [ Test ]
110+ public void WithMediaTypeShouldThrowExceptionWithNullMediaTypeHeaderValueAndNullActual ( )
111+ {
112+ MyWebApi
113+ . Controller < WebApiController > ( )
114+ . Calling ( c => c . ContentActionWithNullMediaType ( ) )
115+ . ShouldReturn ( )
116+ . Content ( )
117+ . WithMediaType ( ( MediaTypeHeaderValue ) null ) ;
118+ }
119+
120+ [ Test ]
121+ [ ExpectedException (
122+ typeof ( ContentResultAssertionException ) ,
123+ ExpectedMessage = "When calling ContentActionWithNullMediaType action in WebApiController expected content result MediaType to be application/json, but instead received null." ) ]
124+ public void WithMediaTypeShouldThrowExceptionWithMediaTypeHeaderValueAndNullActual ( )
125+ {
126+ MyWebApi
127+ . Controller < WebApiController > ( )
128+ . Calling ( c => c . ContentActionWithNullMediaType ( ) )
129+ . ShouldReturn ( )
130+ . Content ( )
131+ . WithMediaType ( new MediaTypeHeaderValue ( TestObjectFactory . MediaType ) ) ;
132+ }
133+
134+ [ Test ]
135+ public void WithDefaultContentNegotiatorShouldNotThrowExceptionWhenActionReturnsDefaultContentNegotiator ( )
136+ {
137+ MyWebApi
138+ . Controller < WebApiController > ( )
139+ . Calling ( c => c . ContentAction ( ) )
140+ . ShouldReturn ( )
141+ . Content ( )
142+ . WithDefaultContentNegotiator ( ) ;
143+ }
144+
145+ [ Test ]
146+ [ ExpectedException (
147+ typeof ( ContentResultAssertionException ) ,
148+ ExpectedMessage = "When calling ContentActionWithCustomFormatters action in WebApiController expected content result IContentNegotiator to be DefaultContentNegotiator, but instead received CustomContentNegotiator." ) ]
149+ public void WithDefaultContentNegotiatorShouldThrowExceptionWhenActionReturnsNotDefaultContentNegotiator ( )
150+ {
151+ MyWebApi
152+ . Controller < WebApiController > ( )
153+ . Calling ( c => c . ContentActionWithCustomFormatters ( ) )
154+ . ShouldReturn ( )
155+ . Content ( )
156+ . WithDefaultContentNegotiator ( ) ;
157+ }
158+
159+ [ Test ]
160+ public void WithContentNegotiatorShouldNotThrowExceptionWhenActionReturnsCorrectContentNegotiator ( )
161+ {
162+ MyWebApi
163+ . Controller < WebApiController > ( )
164+ . Calling ( c => c . ContentActionWithCustomFormatters ( ) )
165+ . ShouldReturn ( )
166+ . Content ( )
167+ . WithContentNegotiator ( new CustomContentNegotiator ( ) ) ;
168+ }
169+
170+ [ Test ]
171+ public void WithContentNegotiatorOfTypeShouldNotThrowExceptionWhenActionReturnsCorrectContentNegotiator ( )
172+ {
173+ MyWebApi
174+ . Controller < WebApiController > ( )
175+ . Calling ( c => c . ContentActionWithCustomFormatters ( ) )
176+ . ShouldReturn ( )
177+ . Content ( )
178+ . WithContentNegotiatorOfType < CustomContentNegotiator > ( ) ;
179+ }
180+
181+ [ Test ]
182+ public void ContainingMediaTypeFormatterShouldNotThrowExceptionWhenActionResultHasTheProvidedMediaTypeFormatter ( )
183+ {
184+ MyWebApi
185+ . Controller < WebApiController > ( )
186+ . Calling ( c => c . ContentActionWithMediaType ( ) )
187+ . ShouldReturn ( )
188+ . Content ( )
189+ . ContainingMediaTypeFormatter ( TestObjectFactory . GetCustomMediaTypeFormatter ( ) ) ;
190+ }
191+
192+ [ Test ]
193+ public void ContainingMediaTypeOfTypeFormatterShouldNotThrowExceptionWhenActionResultHasTheProvidedMediaTypeFormatter ( )
194+ {
195+ MyWebApi
196+ . Controller < WebApiController > ( )
197+ . Calling ( c => c . ContentActionWithCustomFormatters ( ) )
198+ . ShouldReturn ( )
199+ . Content ( )
200+ . ContainingMediaTypeFormatterOfType < JsonMediaTypeFormatter > ( ) ;
201+ }
202+
203+ [ Test ]
204+ public void ContainingDefaultFormattersShouldNotThrowExceptionWhenActionResultHasDefaultMediaTypeFormatters ( )
205+ {
206+ MyWebApi
207+ . Controller < WebApiController > ( )
208+ . Calling ( c => c . ContentAction ( ) )
209+ . ShouldReturn ( )
210+ . Content ( )
211+ . ContainingDefaultFormatters ( ) ;
212+ }
213+
214+ [ Test ]
215+ [ ExpectedException ( typeof (
216+ ContentResultAssertionException ) ,
217+ ExpectedMessage = "When calling ContentActionWithMediaType action in WebApiController expected content result Formatters to be 4, but instead found 1." ) ]
218+ public void ContainingDefaultFormattersShouldThrowExceptionWhenActionResultHasNotDefaultMediaTypeFormatters ( )
219+ {
220+ MyWebApi
221+ . Controller < WebApiController > ( )
222+ . Calling ( c => c . ContentActionWithMediaType ( ) )
223+ . ShouldReturn ( )
224+ . Content ( )
225+ . ContainingDefaultFormatters ( ) ;
226+ }
227+
228+ [ Test ]
229+ public void ContainingFormattersShouldNotThrowExceptionWhenActionResultHasCorrectMediaTypeFormatters ( )
230+ {
231+ MyWebApi
232+ . Controller < WebApiController > ( )
233+ . Calling ( c => c . ContentActionWithCustomFormatters ( ) )
234+ . ShouldReturn ( )
235+ . Content ( )
236+ . ContainingMediaTypeFormatters ( TestObjectFactory . GetFormatters ( ) . Reverse ( ) ) ;
237+ }
238+
239+ [ Test ]
240+ public void ContainingFormattersWithBuilderShouldNotThrowExceptionWhenActionResultHasCorrectMediaTypeFormatters ( )
241+ {
242+ MyWebApi
243+ . Controller < WebApiController > ( )
244+ . Calling ( c => c . ContentAction ( ) )
245+ . ShouldReturn ( )
246+ . Content ( )
247+ . ContainingMediaTypeFormatters (
248+ formatters => formatters
249+ . ContainingMediaTypeFormatter ( new JsonMediaTypeFormatter ( ) )
250+ . AndAlso ( )
251+ . ContainingMediaTypeFormatterOfType < FormUrlEncodedMediaTypeFormatter > ( ) ) ;
252+ }
253+
254+ [ Test ]
255+ public void ContainingFormattersShouldNotThrowExceptionWhenActionResultHasCorrectMediaTypeFormattersAsParams ( )
256+ {
257+ var mediaTypeFormatters = TestObjectFactory . GetFormatters ( ) . ToList ( ) ;
258+
259+ MyWebApi
260+ . Controller < WebApiController > ( )
261+ . Calling ( c => c . ContentActionWithCustomFormatters ( ) )
262+ . ShouldReturn ( )
263+ . Content ( )
264+ . ContainingMediaTypeFormatters (
265+ mediaTypeFormatters [ 0 ] ,
266+ mediaTypeFormatters [ 1 ] ,
267+ mediaTypeFormatters [ 2 ] ,
268+ mediaTypeFormatters [ 3 ] ,
269+ mediaTypeFormatters [ 4 ] ) ;
270+ }
271+
272+ [ Test ]
273+ public void AndAlsoShouldWorkCorrectly ( )
274+ {
275+ MyWebApi
276+ . Controller < WebApiController > ( )
277+ . Calling ( c => c . ContentAction ( ) )
278+ . ShouldReturn ( )
279+ . Content ( )
280+ . WithStatusCode ( HttpStatusCode . OK )
281+ . AndAlso ( )
282+ . WithResponseModelOfType < ICollection < ResponseModel > > ( ) ;
283+ }
284+
54285 [ Test ]
55286 public void WithResponseModelOfTypeShouldWorkCorrectly ( )
56287 {
@@ -61,5 +292,6 @@ public void WithResponseModelOfTypeShouldWorkCorrectly()
61292 . Content ( )
62293 . WithResponseModelOfType < ICollection < ResponseModel > > ( ) ;
63294 }
295+
64296 }
65297}
0 commit comments