1- namespace TestableHttpClient ;
1+ using System . Text . Json ;
2+
3+ namespace TestableHttpClient ;
24
35public static class HttpRequestMessagesCheckExtensions
46{
@@ -136,7 +138,16 @@ public static IHttpRequestMessagesCheck WithContentHeader(this IHttpRequestMessa
136138 /// <param name="check">The implementation that hold all the request messages.</param>
137139 /// <param name="jsonObject">The object representation of the expected request content.</param>
138140 /// <returns>The <seealso cref="IHttpRequestMessagesCheck"/> for further assertions.</returns>
139- public static IHttpRequestMessagesCheck WithJsonContent ( this IHttpRequestMessagesCheck check , object ? jsonObject ) => WithJsonContent ( check , jsonObject , null , null ) ;
141+ public static IHttpRequestMessagesCheck WithJsonContent ( this IHttpRequestMessagesCheck check , object ? jsonObject )
142+ {
143+
144+ Guard . ThrowIfNull ( check ) ;
145+
146+ var jsonString = JsonSerializer . Serialize ( jsonObject , check . Options . JsonSerializerOptions ) ;
147+
148+ return check . WithContent ( jsonString )
149+ . WithHeader ( "Content-Type" , "application/json*" ) ;
150+ }
140151
141152 /// <summary>
142153 /// Asserts whether requests are made with specific json content.
@@ -145,7 +156,15 @@ public static IHttpRequestMessagesCheck WithContentHeader(this IHttpRequestMessa
145156 /// <param name="jsonObject">The object representation of the expected request content.</param>
146157 /// <param name="jsonSerializerOptions">The serializer options that should be used for serializing te content.</param>
147158 /// <returns>The <seealso cref="IHttpRequestMessagesCheck"/> for further assertions.</returns>
148- public static IHttpRequestMessagesCheck WithJsonContent ( this IHttpRequestMessagesCheck check , object ? jsonObject , JsonSerializerOptions jsonSerializerOptions ) => WithJsonContent ( check , jsonObject , jsonSerializerOptions , null ) ;
159+ public static IHttpRequestMessagesCheck WithJsonContent ( this IHttpRequestMessagesCheck check , object ? jsonObject , JsonSerializerOptions jsonSerializerOptions )
160+ {
161+ Guard . ThrowIfNull ( check ) ;
162+
163+ var jsonString = JsonSerializer . Serialize ( jsonObject , jsonSerializerOptions ?? check . Options . JsonSerializerOptions ) ;
164+
165+ return check . WithContent ( jsonString )
166+ . WithHeader ( "Content-Type" , "application/json*" ) ;
167+ }
149168
150169 /// <summary>
151170 /// Asserts whether requests are made with specific json content.
@@ -154,7 +173,15 @@ public static IHttpRequestMessagesCheck WithContentHeader(this IHttpRequestMessa
154173 /// <param name="jsonObject">The object representation of the expected request content.</param>
155174 /// <param name="expectedNumberOfRequests">The expected number of requests.</param>
156175 /// <returns>The <seealso cref="IHttpRequestMessagesCheck"/> for further assertions.</returns>
157- public static IHttpRequestMessagesCheck WithJsonContent ( this IHttpRequestMessagesCheck check , object ? jsonObject , int expectedNumberOfRequests ) => WithJsonContent ( check , jsonObject , null , ( int ? ) expectedNumberOfRequests ) ;
176+ public static IHttpRequestMessagesCheck WithJsonContent ( this IHttpRequestMessagesCheck check , object ? jsonObject , int expectedNumberOfRequests )
177+ {
178+ Guard . ThrowIfNull ( check ) ;
179+
180+ var jsonString = JsonSerializer . Serialize ( jsonObject , check . Options . JsonSerializerOptions ) ;
181+
182+ return check . WithContent ( jsonString , expectedNumberOfRequests )
183+ . WithHeader ( "Content-Type" , "application/json*" , expectedNumberOfRequests ) ;
184+ }
158185
159186 /// <summary>
160187 /// Asserts whether requests are made with specific json content.
@@ -164,15 +191,14 @@ public static IHttpRequestMessagesCheck WithContentHeader(this IHttpRequestMessa
164191 /// <param name="jsonSerializerOptions">The serializer options that should be used for serializing the content.</param>
165192 /// <param name="expectedNumberOfRequests">The expected number of requests.</param>
166193 /// <returns>The <seealso cref="IHttpRequestMessagesCheck"/> for further assertions.</returns>
167- public static IHttpRequestMessagesCheck WithJsonContent ( this IHttpRequestMessagesCheck check , object ? jsonObject , JsonSerializerOptions jsonSerializerOptions , int expectedNumberOfRequests ) => WithJsonContent ( check , jsonObject , jsonSerializerOptions , ( int ? ) expectedNumberOfRequests ) ;
168-
169- private static IHttpRequestMessagesCheck WithJsonContent ( this IHttpRequestMessagesCheck check , object ? jsonObject , JsonSerializerOptions ? jsonSerializerOptions , int ? expectedNumberOfRequests )
194+ public static IHttpRequestMessagesCheck WithJsonContent ( this IHttpRequestMessagesCheck check , object ? jsonObject , JsonSerializerOptions jsonSerializerOptions , int expectedNumberOfRequests )
170195 {
171196 Guard . ThrowIfNull ( check ) ;
172197
173198 var jsonString = JsonSerializer . Serialize ( jsonObject , jsonSerializerOptions ?? check . Options . JsonSerializerOptions ) ;
174199
175- return check . WithFilter ( x => x . HasContent ( jsonString ) && x . HasHeader ( "Content-Type" , "application/json*" ) , expectedNumberOfRequests , $ "json content '{ jsonString } '") ;
200+ return check . WithContent ( jsonString , expectedNumberOfRequests )
201+ . WithHeader ( "Content-Type" , "application/json*" , expectedNumberOfRequests ) ;
176202 }
177203
178204 /// <summary>
@@ -181,7 +207,17 @@ private static IHttpRequestMessagesCheck WithJsonContent(this IHttpRequestMessag
181207 /// <param name="check">The implementation that hold all the request messages.</param>
182208 /// <param name="nameValueCollection">The collection of key/value pairs that should be url encoded.</param>
183209 /// <returns>The <seealso cref="IHttpRequestMessagesCheck"/> for further assertions.</returns>
184- public static IHttpRequestMessagesCheck WithFormUrlEncodedContent ( this IHttpRequestMessagesCheck check , IEnumerable < KeyValuePair < string ? , string ? > > nameValueCollection ) => WithFormUrlEncodedContent ( check , nameValueCollection , null ) ;
210+ public static IHttpRequestMessagesCheck WithFormUrlEncodedContent ( this IHttpRequestMessagesCheck check , IEnumerable < KeyValuePair < string ? , string ? > > nameValueCollection )
211+ {
212+ Guard . ThrowIfNull ( check ) ;
213+ Guard . ThrowIfNull ( nameValueCollection ) ;
214+
215+ using var content = new FormUrlEncodedContent ( nameValueCollection ) ;
216+ var contentString = content . ReadAsStringAsync ( ) . Result ;
217+
218+ return check . WithContent ( contentString )
219+ . WithHeader ( "Content-Type" , "application/x-www-form-urlencoded*" ) ;
220+ }
185221
186222 /// <summary>
187223 /// Asserts whether requests are made with specific url encoded content.
@@ -190,16 +226,15 @@ private static IHttpRequestMessagesCheck WithJsonContent(this IHttpRequestMessag
190226 /// <param name="nameValueCollection">The collection of key/value pairs that should be url encoded.</param>
191227 /// <param name="expectedNumberOfRequests">The expected number of requests.</param>
192228 /// <returns>The <seealso cref="IHttpRequestMessagesCheck"/> for further assertions.</returns>
193- public static IHttpRequestMessagesCheck WithFormUrlEncodedContent ( this IHttpRequestMessagesCheck check , IEnumerable < KeyValuePair < string ? , string ? > > nameValueCollection , int expectedNumberOfRequests ) => WithFormUrlEncodedContent ( check , nameValueCollection , ( int ? ) expectedNumberOfRequests ) ;
194-
195- private static IHttpRequestMessagesCheck WithFormUrlEncodedContent ( this IHttpRequestMessagesCheck check , IEnumerable < KeyValuePair < string ? , string ? > > nameValueCollection , int ? expectedNumberOfRequests )
229+ public static IHttpRequestMessagesCheck WithFormUrlEncodedContent ( this IHttpRequestMessagesCheck check , IEnumerable < KeyValuePair < string ? , string ? > > nameValueCollection , int expectedNumberOfRequests )
196230 {
197231 Guard . ThrowIfNull ( check ) ;
198232 Guard . ThrowIfNull ( nameValueCollection ) ;
199233
200234 using var content = new FormUrlEncodedContent ( nameValueCollection ) ;
201235 var contentString = content . ReadAsStringAsync ( ) . Result ;
202236
203- return check . WithFilter ( x => x . HasContent ( contentString ) && x . HasHeader ( "Content-Type" , "application/x-www-form-urlencoded*" ) , expectedNumberOfRequests , $ "form url encoded content '{ contentString } '") ;
237+ return check . WithContent ( contentString , expectedNumberOfRequests )
238+ . WithHeader ( "Content-Type" , "application/x-www-form-urlencoded*" , expectedNumberOfRequests ) ;
204239 }
205240}
0 commit comments