66
77namespace PosInformatique . Testing . Azure . Functions . Http . Tests
88{
9+ using System . Text ;
910 using System . Text . Json ;
1011 using Xunit . Sdk ;
1112
@@ -141,5 +142,79 @@ public void WithEmptyBody_Failed()
141142
142143 response . Body . Position . Should ( ) . Be ( position ) ;
143144 }
145+
146+ [ Fact ]
147+ public void WithStringBody ( )
148+ {
149+ var response = new HttpResponseDataImplementation ( new FunctionContextImplementation ( new HttpRequestDataMock ( ) ) ) ;
150+ response . Body . Write ( Encoding . Unicode . GetBytes ( "The string" ) ) ;
151+
152+ var assertions = new HttpResponseDataAssertions ( response ) ;
153+
154+ var position = response . Body . Position ;
155+
156+ assertions . WithStringBody ( "The string" , Encoding . Unicode )
157+ . Should ( ) . BeSameAs ( assertions ) ;
158+
159+ response . Body . Position . Should ( ) . Be ( position ) ;
160+ }
161+
162+ [ Fact ]
163+ public void WithStringBody_DefaultEncoding ( )
164+ {
165+ var response = new HttpResponseDataImplementation ( new FunctionContextImplementation ( new HttpRequestDataMock ( ) ) ) ;
166+ response . Body . Write ( Encoding . UTF8 . GetBytes ( "The string" ) ) ;
167+
168+ var assertions = new HttpResponseDataAssertions ( response ) ;
169+
170+ var position = response . Body . Position ;
171+
172+ assertions . WithStringBody ( "The string" )
173+ . Should ( ) . BeSameAs ( assertions ) ;
174+
175+ response . Body . Position . Should ( ) . Be ( position ) ;
176+ }
177+
178+ [ Fact ]
179+ public void WithStringBody_DifferentValue_Failed ( )
180+ {
181+ var response = new HttpResponseDataImplementation ( new FunctionContextImplementation ( new HttpRequestDataMock ( ) ) ) ;
182+ response . Body . Write ( Encoding . Unicode . GetBytes ( "The other string" ) ) ;
183+
184+ var assertions = new HttpResponseDataAssertions ( response ) ;
185+
186+ var position = response . Body . Position ;
187+
188+ var act = ( ) =>
189+ {
190+ assertions . WithStringBody ( "The string" , Encoding . Unicode ) ;
191+ } ;
192+
193+ act . Should ( ) . ThrowExactly < XunitException > ( )
194+ . WithMessage ( "Expected actualString to be \" The string\" with a length of 10, but \" The other string\" has a length of 16, differs near \" oth\" (index 4)." ) ;
195+
196+ response . Body . Position . Should ( ) . Be ( position ) ;
197+ }
198+
199+ [ Fact ]
200+ public void WithStringBody_DifferentEncoding_Failed ( )
201+ {
202+ var response = new HttpResponseDataImplementation ( new FunctionContextImplementation ( new HttpRequestDataMock ( ) ) ) ;
203+ response . Body . Write ( Encoding . UTF8 . GetBytes ( "The other string" ) ) ;
204+
205+ var assertions = new HttpResponseDataAssertions ( response ) ;
206+
207+ var position = response . Body . Position ;
208+
209+ var act = ( ) =>
210+ {
211+ assertions . WithStringBody ( "The string" , Encoding . Unicode ) ;
212+ } ;
213+
214+ act . Should ( ) . ThrowExactly < XunitException > ( )
215+ . WithMessage ( "Expected actualString to be \" The string\" with a length of 10, but \" 桔\u2065 瑯敨\u2072 瑳楲杮\" has a length of 8, differs near \" 桔\u2065 瑯\" (index 0)." ) ;
216+
217+ response . Body . Position . Should ( ) . Be ( position ) ;
218+ }
144219 }
145220}
0 commit comments