@@ -9,13 +9,17 @@ namespace DotNetOpenAuth.Test.Messaging {
99 using System . Collections . Generic ;
1010 using System . Collections . Specialized ;
1111 using System . Diagnostics ;
12+ using System . Globalization ;
1213 using System . IO ;
14+ using System . Linq ;
1315 using System . Net ;
16+ using System . Net . Http ;
1417 using System . Text ;
1518 using System . Text . RegularExpressions ;
1619 using System . Web ;
1720 using DotNetOpenAuth . Messaging ;
1821 using DotNetOpenAuth . Test . Mocks ;
22+ using Moq ;
1923 using NUnit . Framework ;
2024
2125 [ TestFixture ]
@@ -62,6 +66,30 @@ public void AppendQueryArgsNullDictionary() {
6266 MessagingUtilities . AppendQueryArgs ( new UriBuilder ( ) , null ) ;
6367 }
6468
69+ [ Test ]
70+ public void AsHttpResponseMessage ( ) {
71+ var responseContent = new byte [ 10 ] ;
72+ ( new Random ( ) ) . NextBytes ( responseContent ) ;
73+ var responseStream = new MemoryStream ( responseContent ) ;
74+ var outgoingResponse = new OutgoingWebResponse ( ) ;
75+ outgoingResponse . Headers . Add ( "X-SOME-HEADER" , "value" ) ;
76+ outgoingResponse . Headers . Add ( "Content-Length" , responseContent . Length . ToString ( CultureInfo . InvariantCulture ) ) ;
77+ outgoingResponse . ResponseStream = responseStream ;
78+
79+ var httpResponseMessage = outgoingResponse . AsHttpResponseMessage ( ) ;
80+ Assert . That ( httpResponseMessage , Is . Not . Null ) ;
81+ Assert . That ( httpResponseMessage . Headers . GetValues ( "X-SOME-HEADER" ) . ToList ( ) , Is . EqualTo ( new [ ] { "value" } ) ) ;
82+ Assert . That (
83+ httpResponseMessage . Content . Headers . GetValues ( "Content-Length" ) . ToList ( ) ,
84+ Is . EqualTo ( new [ ] { responseContent . Length . ToString ( CultureInfo . InvariantCulture ) } ) ) ;
85+ var actualContent = new byte [ responseContent . Length + 1 ] ; // give the opportunity to provide a bit more data than we expect.
86+ var bytesRead = httpResponseMessage . Content . ReadAsStreamAsync ( ) . Result . Read ( actualContent , 0 , actualContent . Length ) ;
87+ Assert . That ( bytesRead , Is . EqualTo ( responseContent . Length ) ) ; // verify that only the data we expected came back.
88+ var trimmedActualContent = new byte [ bytesRead ] ;
89+ Array . Copy ( actualContent , trimmedActualContent , bytesRead ) ;
90+ Assert . That ( trimmedActualContent , Is . EqualTo ( responseContent ) ) ;
91+ }
92+
6593 [ Test ]
6694 public void ToDictionary ( ) {
6795 NameValueCollection nvc = new NameValueCollection ( ) ;
@@ -151,7 +179,7 @@ public void PostMultipart() {
151179 var httpHandler = new TestWebRequestHandler ( ) ;
152180 bool callbackTriggered = false ;
153181 httpHandler . Callback = req => {
154- Match m = Regex . Match ( req . ContentType , "multipart/form-data; boundary=(.+)" ) ;
182+ var m = Regex . Match ( req . ContentType , "multipart/form-data; boundary=(.+)" ) ;
155183 Assert . IsTrue ( m . Success , "Content-Type HTTP header not set correctly." ) ;
156184 string boundary = m . Groups [ 1 ] . Value ;
157185 boundary = boundary . Substring ( 0 , boundary . IndexOf ( ';' ) ) ; // trim off charset
0 commit comments