@@ -80,28 +80,33 @@ public class NetmockeryTestCase
8080 public string ExpectedRequestMatcher ;
8181 public string ExpectedResponseCreator ;
8282
83+ public string ExpectedContentType ;
84+ public string ExpectedCharSet ;
8385 public string ExpectedResponseBody ;
8486
8587 public bool NeedsResponseBody
8688 {
8789 get
8890 {
89- return ( new [ ] { ExpectedResponseBody } ) . Any ( val => val != null ) ;
91+ return ( new [ ] { ExpectedResponseBody , ExpectedContentType , ExpectedCharSet } ) . Any ( val => val != null ) ;
9092 }
9193 }
9294
9395 public bool HasExpectations
9496 {
9597 get
9698 {
97- return ( new [ ] { ExpectedResponseBody , ExpectedRequestMatcher , ExpectedResponseCreator } ) . Any ( val => val != null ) ;
99+ return ( new [ ] { ExpectedResponseBody , ExpectedRequestMatcher , ExpectedResponseCreator , ExpectedContentType , ExpectedCharSet } ) . Any ( val => val != null ) ;
98100 }
99101 }
100102
101103
102- public bool Evaluate ( string requestMatcher , string responseCreator , string responseBody , out string message )
104+ public bool Evaluate ( string requestMatcher , string responseCreator , string responseBody , string contentType , string charset , out string message )
103105 {
104106 Debug . Assert ( responseBody != null || ! NeedsResponseBody ) ;
107+ Debug . Assert ( contentType != null || ! NeedsResponseBody ) ;
108+ Debug . Assert ( charset != null || ! NeedsResponseBody ) ;
109+
105110 Debug . Assert ( requestMatcher != null ) ;
106111 Debug . Assert ( responseCreator != null ) ;
107112 message = null ;
@@ -124,6 +129,18 @@ public bool Evaluate(string requestMatcher, string responseCreator, string respo
124129 return false ;
125130 }
126131
132+ if ( ExpectedContentType != null && ExpectedContentType != contentType )
133+ {
134+ message = $ "Expected contenttype: '{ ExpectedContentType } '\n Actual: '{ contentType } '";
135+ return false ;
136+ }
137+
138+ if ( ExpectedCharSet != null && ExpectedCharSet != charset )
139+ {
140+ message = $ "Expected charset: '{ ExpectedCharSet } '\n Actual: '{ charset } '";
141+ return false ;
142+ }
143+
127144 Debug . Assert ( message == null ) ;
128145 return true ;
129146 }
@@ -157,7 +174,15 @@ async public Task<NetmockeryTestCaseResult> ExecuteAgainstHttpClientAsync(HttpCl
157174 {
158175 responseCreator = responseMessage . Headers . GetValues ( "X-Netmockery-ResponseCreator" ) . ElementAt ( 0 ) ;
159176 }
160- if ( Evaluate ( requestMatcher , responseCreator , body , out message ) )
177+ var contentType = "" ;
178+ var charset = "" ;
179+ if ( responseMessage . Content . Headers . ContentType != null )
180+ {
181+ contentType = responseMessage . Content . Headers . ContentType . MediaType ;
182+ charset = responseMessage . Content . Headers . ContentType . CharSet ;
183+ }
184+
185+ if ( Evaluate ( requestMatcher , responseCreator , body , contentType , charset , out message ) )
161186 {
162187 retval . SetSuccess ( ) ;
163188 }
@@ -204,6 +229,8 @@ async public Task<NetmockeryTestCaseResult> ExecuteAsync(EndpointCollection endp
204229
205230 var responseCreator = matcher_and_creator . ResponseCreator ;
206231 string responseBody = null ;
232+ string charset = "" ;
233+ string contenttype = "" ;
207234 if ( NeedsResponseBody )
208235 {
209236 var simpleResponseCreator = responseCreator as SimpleResponseCreator ;
@@ -225,9 +252,11 @@ async public Task<NetmockeryTestCaseResult> ExecuteAsync(EndpointCollection endp
225252 requestInfo . SetStaticNow ( now . Value ) ;
226253 }
227254 responseBody = simpleResponseCreator . GetBodyAndExecuteReplacements ( requestInfo ) ;
255+ contenttype = simpleResponseCreator . ContentType ?? "" ;
256+ charset = simpleResponseCreator . Encoding . WebName ;
228257 }
229258 string message ;
230- if ( Evaluate ( matcher_and_creator . RequestMatcher . ToString ( ) , matcher_and_creator . ResponseCreator . ToString ( ) , responseBody , out message ) )
259+ if ( Evaluate ( matcher_and_creator . RequestMatcher . ToString ( ) , matcher_and_creator . ResponseCreator . ToString ( ) , responseBody , contenttype , charset , out message ) )
231260 {
232261 return testResult . SetSuccess ( ) ;
233262 }
0 commit comments