HttpWebRequestTest.cs 101 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380
  1. //
  2. // HttpWebRequestTest.cs - NUnit Test Cases for System.Net.HttpWebRequest
  3. //
  4. // Authors:
  5. // Lawrence Pit ([email protected])
  6. // Martin Willemoes Hansen ([email protected])
  7. // Gonzalo Paniagua Javier ([email protected])
  8. // Andres G. Aragoneses ([email protected])
  9. //
  10. // (C) 2003 Martin Willemoes Hansen
  11. // Copyright (c) 2005 Novell, Inc. (http://www.novell.com
  12. // Copyright (c) 2013 7digital Media Ltd (http://www.7digital.com)
  13. //
  14. using NUnit.Framework;
  15. using System;
  16. using System.Collections;
  17. using System.Collections.Specialized;
  18. using System.Globalization;
  19. using System.IO;
  20. using System.Net;
  21. using System.Net.Sockets;
  22. using System.Security.Cryptography;
  23. using System.Security.Cryptography.X509Certificates;
  24. using System.Text;
  25. using System.Threading;
  26. using Mono.Security.Authenticode;
  27. #if !MOBILE
  28. using Mono.Security.Protocol.Tls;
  29. #endif
  30. using MonoTests.Helpers;
  31. namespace MonoTests.System.Net
  32. {
  33. [TestFixture]
  34. public class HttpWebRequestTest
  35. {
  36. private Random rand = new Random ();
  37. private byte [] data64KB = new byte [64 * 1024];
  38. [TestFixtureSetUp]
  39. public void Setup ()
  40. {
  41. ServicePointManager.Expect100Continue = false;
  42. rand.NextBytes (data64KB);
  43. }
  44. [Test]
  45. public void Proxy_Null ()
  46. {
  47. HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
  48. Assert.IsNotNull (req.Proxy, "#1");
  49. req.Proxy = null;
  50. Assert.IsNull (req.Proxy, "#2");
  51. }
  52. [Test]
  53. [Category("InetAccess")]
  54. public void Sync ()
  55. {
  56. HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
  57. Assert.IsNotNull (req.IfModifiedSince, "req:If Modified Since: ");
  58. req.UserAgent = "MonoClient v1.0";
  59. Assert.AreEqual ("User-Agent", req.Headers.GetKey (0), "#A1");
  60. Assert.AreEqual ("MonoClient v1.0", req.Headers.Get (0), "#A2");
  61. HttpWebResponse res = (HttpWebResponse) req.GetResponse ();
  62. Assert.AreEqual ("OK", res.StatusCode.ToString (), "#B1");
  63. Assert.AreEqual ("OK", res.StatusDescription, "#B2");
  64. Assert.IsTrue (res.Headers.Get ("Content-Type").StartsWith ("text/html; charset=", StringComparison.OrdinalIgnoreCase), "#C1");
  65. Assert.IsNotNull (res.LastModified, "#C2");
  66. Assert.AreEqual (0, res.Cookies.Count, "#C3");
  67. res.Close ();
  68. }
  69. [Test]
  70. public void AddRange ()
  71. {
  72. HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
  73. req.AddRange (10);
  74. req.AddRange (50, 90);
  75. req.AddRange ("bytes", 100);
  76. req.AddRange ("bytes", 100, 120);
  77. Assert.AreEqual ("bytes=10-,50-90,100-,100-120", req.Headers ["Range"], "#1");
  78. try {
  79. req.AddRange ("bits", 2000);
  80. Assert.Fail ("#2");
  81. } catch (InvalidOperationException) {}
  82. }
  83. [Test] // bug #471782
  84. public void CloseRequestStreamAfterReadingResponse ()
  85. {
  86. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  87. string url = "http://" + ep.ToString () + "/test/";
  88. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
  89. responder.Start ();
  90. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  91. req.Method = "POST";
  92. req.Timeout = 2000;
  93. req.ReadWriteTimeout = 2000;
  94. byte [] data = new byte [128];
  95. req.ContentLength = data.Length;
  96. Stream rs = req.GetRequestStream ();
  97. rs.Write (data, 0, data.Length);
  98. rs.Flush ();
  99. HttpWebResponse response = (HttpWebResponse) req.GetResponse ();
  100. response.Close ();
  101. rs.Close ();
  102. responder.Stop ();
  103. }
  104. }
  105. [Test]
  106. [Category("InetAccess")]
  107. public void Cookies1 ()
  108. {
  109. // The purpose of this test is to ensure that the cookies we get from a request
  110. // are stored in both, the CookieCollection in HttpWebResponse and the CookieContainer
  111. // in HttpWebRequest.
  112. // If this URL stops sending *one* and only one cookie, replace it.
  113. string url = "http://xamarin.com";
  114. CookieContainer cookies = new CookieContainer ();
  115. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  116. req.KeepAlive = false;
  117. req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv; 1.7.6) Gecko/20050317 Firefox/1.0.2";
  118. req.CookieContainer = cookies;
  119. Assert.AreEqual (0, cookies.Count, "#01");
  120. using (HttpWebResponse res = (HttpWebResponse) req.GetResponse()) {
  121. CookieCollection coll = req.CookieContainer.GetCookies (new Uri (url));
  122. Assert.AreEqual (1, coll.Count, "#02");
  123. Assert.AreEqual (1, res.Cookies.Count, "#03");
  124. Cookie one = coll [0];
  125. Cookie two = res.Cookies [0];
  126. Assert.AreEqual (true, object.ReferenceEquals (one, two), "#04");
  127. }
  128. }
  129. #if !MOBILE
  130. [Test]
  131. [Ignore ("Fails on MS.NET")]
  132. public void SslClientBlock ()
  133. {
  134. // This tests that the write request/initread/write body sequence does not hang
  135. // when using SSL.
  136. // If there's a regression for this, the test will hang.
  137. ServicePointManager.CertificatePolicy = new AcceptAllPolicy ();
  138. try {
  139. SslHttpServer server = new SslHttpServer ();
  140. server.Start ();
  141. string url = String.Format ("https://{0}:{1}/nothing.html", server.IPAddress, server.Port);
  142. HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
  143. request.Method = "POST";
  144. Stream stream = request.GetRequestStream ();
  145. byte [] bytes = new byte [100];
  146. stream.Write (bytes, 0, bytes.Length);
  147. stream.Close ();
  148. HttpWebResponse resp = (HttpWebResponse) request.GetResponse ();
  149. Assert.AreEqual (200, (int) resp.StatusCode, "StatusCode");
  150. StreamReader sr = new StreamReader (resp.GetResponseStream (), Encoding.UTF8);
  151. sr.ReadToEnd ();
  152. sr.Close ();
  153. resp.Close ();
  154. server.Stop ();
  155. if (server.Error != null)
  156. throw server.Error;
  157. } finally {
  158. ServicePointManager.CertificatePolicy = null;
  159. }
  160. }
  161. #endif
  162. [Test]
  163. public void Missing_ContentEncoding ()
  164. {
  165. ServicePointManager.CertificatePolicy = new AcceptAllPolicy ();
  166. try {
  167. BadChunkedServer server = new BadChunkedServer ();
  168. server.Start ();
  169. string url = String.Format ("http://{0}:{1}/nothing.html", server.IPAddress, server.Port);
  170. HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
  171. request.Method = "GET";
  172. HttpWebResponse resp = (HttpWebResponse) request.GetResponse ();
  173. Assert.AreEqual ("", resp.ContentEncoding);
  174. resp.Close ();
  175. server.Stop ();
  176. if (server.Error != null)
  177. throw server.Error;
  178. } finally {
  179. ServicePointManager.CertificatePolicy = null;
  180. }
  181. }
  182. [Test]
  183. public void BadServer_ChunkedClose ()
  184. {
  185. // The server will send a chunked response without a 'last-chunked' mark
  186. // and then shutdown the socket for sending.
  187. BadChunkedServer server = new BadChunkedServer ();
  188. server.Start ();
  189. string url = String.Format ("http://{0}:{1}/nothing.html", server.IPAddress, server.Port);
  190. HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
  191. HttpWebResponse resp = (HttpWebResponse) request.GetResponse ();
  192. string x = null;
  193. try {
  194. byte [] bytes = new byte [32];
  195. // Using StreamReader+UTF8Encoding here fails on MS runtime
  196. Stream stream = resp.GetResponseStream ();
  197. int nread = stream.Read (bytes, 0, 32);
  198. Assert.AreEqual (16, nread, "#01");
  199. x = Encoding.ASCII.GetString (bytes, 0, 16);
  200. } finally {
  201. resp.Close ();
  202. server.Stop ();
  203. }
  204. if (server.Error != null)
  205. throw server.Error;
  206. Assert.AreEqual ("1234567890123456", x);
  207. }
  208. [Test]
  209. [Ignore ("This test asserts that our code violates RFC 2616")]
  210. public void MethodCase ()
  211. {
  212. ListDictionary methods = new ListDictionary ();
  213. methods.Add ("post", "POST");
  214. methods.Add ("puT", "PUT");
  215. methods.Add ("POST", "POST");
  216. methods.Add ("whatever", "whatever");
  217. methods.Add ("PUT", "PUT");
  218. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  219. string url = "http://" + ep.ToString () + "/test/";
  220. foreach (DictionaryEntry de in methods) {
  221. SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler));
  222. responder.Start ();
  223. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  224. req.Method = (string) de.Key;
  225. req.Timeout = 2000;
  226. req.ReadWriteTimeout = 2000;
  227. req.KeepAlive = false;
  228. Stream rs = req.GetRequestStream ();
  229. rs.Close ();
  230. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  231. StreamReader sr = new StreamReader (resp.GetResponseStream (),
  232. Encoding.UTF8);
  233. string line = sr.ReadLine ();
  234. sr.Close ();
  235. Assert.AreEqual (((string) de.Value) + " /test/ HTTP/1.1",
  236. line, req.Method);
  237. resp.Close ();
  238. }
  239. responder.Stop ();
  240. }
  241. }
  242. [Test]
  243. public void BeginGetRequestStream_Body_NotAllowed ()
  244. {
  245. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  246. string url = "http://" + ep.ToString () + "/test/";
  247. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
  248. responder.Start ();
  249. HttpWebRequest request;
  250. request = (HttpWebRequest) WebRequest.Create (url);
  251. request.Method = "GET";
  252. try {
  253. request.BeginGetRequestStream (null, null);
  254. Assert.Fail ("#A1");
  255. } catch (ProtocolViolationException ex) {
  256. // Cannot send a content-body with this
  257. // verb-type
  258. Assert.IsNull (ex.InnerException, "#A2");
  259. Assert.IsNotNull (ex.Message, "#A3");
  260. }
  261. request = (HttpWebRequest) WebRequest.Create (url);
  262. request.Method = "HEAD";
  263. try {
  264. request.BeginGetRequestStream (null, null);
  265. Assert.Fail ("#B1");
  266. } catch (ProtocolViolationException ex) {
  267. // Cannot send a content-body with this
  268. // verb-type
  269. Assert.IsNull (ex.InnerException, "#B2");
  270. Assert.IsNotNull (ex.Message, "#B3");
  271. }
  272. }
  273. }
  274. [Test] // bug #465613
  275. public void BeginGetRequestStream_NoBuffering ()
  276. {
  277. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  278. string url = "http://" + ep.ToString () + "/test/";
  279. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
  280. responder.Start ();
  281. HttpWebRequest req;
  282. Stream rs;
  283. IAsyncResult ar;
  284. req = (HttpWebRequest) WebRequest.Create (url);
  285. req.Method = "POST";
  286. req.SendChunked = false;
  287. req.KeepAlive = false;
  288. req.AllowWriteStreamBuffering = false;
  289. ar = req.BeginGetRequestStream (null, null);
  290. rs = req.EndGetRequestStream (ar);
  291. rs.Close ();
  292. req = (HttpWebRequest) WebRequest.Create (url);
  293. req.Method = "POST";
  294. req.SendChunked = false;
  295. req.KeepAlive = true;
  296. req.AllowWriteStreamBuffering = false;
  297. try {
  298. req.BeginGetRequestStream (null, null);
  299. Assert.Fail ("#A1");
  300. } catch (ProtocolViolationException ex) {
  301. // When performing a write operation with
  302. // AllowWriteStreamBuffering set to false,
  303. // you must either set ContentLength to a
  304. // non-negative number or set SendChunked
  305. // to true
  306. Assert.IsNull (ex.InnerException, "#A2");
  307. Assert.IsNotNull (ex.Message, "#A3");
  308. }
  309. req = (HttpWebRequest) WebRequest.Create (url);
  310. req.Method = "POST";
  311. req.SendChunked = false;
  312. req.KeepAlive = true;
  313. req.AllowWriteStreamBuffering = false;
  314. req.ContentLength = 0;
  315. ar = req.BeginGetRequestStream (null, null);
  316. rs = req.EndGetRequestStream (ar);
  317. rs.Close ();
  318. }
  319. }
  320. [Test] // bug #508027
  321. [Category ("NotWorking")] // #5842
  322. public void BeginGetResponse ()
  323. {
  324. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  325. string url = "http://" + ep.ToString () + "/test/";
  326. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
  327. responder.Start ();
  328. HttpWebRequest req;
  329. req = (HttpWebRequest) WebRequest.Create (url);
  330. req.Timeout = 5000;
  331. req.Method = "POST";
  332. req.SendChunked = false;
  333. req.KeepAlive = false;
  334. req.AllowWriteStreamBuffering = false;
  335. req.BeginGetResponse (null, null);
  336. req.Abort ();
  337. req = (HttpWebRequest) WebRequest.Create (url);
  338. req.Timeout = 5000;
  339. req.Method = "POST";
  340. req.SendChunked = true;
  341. req.KeepAlive = false;
  342. req.AllowWriteStreamBuffering = false;
  343. req.GetRequestStream ().WriteByte (1);
  344. req.BeginGetResponse (null, null);
  345. req.Abort ();
  346. req = (HttpWebRequest) WebRequest.Create (url);
  347. req.Timeout = 5000;
  348. req.Method = "POST";
  349. req.ContentLength = 5;
  350. req.SendChunked = false;
  351. req.KeepAlive = false;
  352. req.AllowWriteStreamBuffering = false;
  353. req.GetRequestStream ().WriteByte (5);
  354. req.BeginGetResponse (null, null);
  355. req.Abort ();
  356. req = (HttpWebRequest) WebRequest.Create (url);
  357. req.Timeout = 5000;
  358. req.Method = "POST";
  359. req.SendChunked = false;
  360. req.KeepAlive = true;
  361. req.AllowWriteStreamBuffering = false;
  362. req.BeginGetResponse (null, null);
  363. req.Abort ();
  364. req = (HttpWebRequest) WebRequest.Create (url);
  365. req.Timeout = 5000;
  366. req.Method = "POST";
  367. req.SendChunked = false;
  368. req.KeepAlive = false;
  369. req.AllowWriteStreamBuffering = false;
  370. req.ContentLength = 5;
  371. req.BeginGetResponse (null, null);
  372. req.Abort ();
  373. req = (HttpWebRequest) WebRequest.Create (url);
  374. req.Timeout = 5000;
  375. req.Method = "POST";
  376. req.SendChunked = false;
  377. req.KeepAlive = true;
  378. req.AllowWriteStreamBuffering = false;
  379. req.ContentLength = 5;
  380. req.BeginGetResponse (null, null);
  381. req.Abort ();
  382. req = (HttpWebRequest) WebRequest.Create (url);
  383. req.Timeout = 5000;
  384. req.Method = "GET";
  385. req.SendChunked = true;
  386. req.BeginGetResponse (null, null);
  387. req.Abort ();
  388. req = (HttpWebRequest) WebRequest.Create (url);
  389. req.Timeout = 5000;
  390. req.Method = "GET";
  391. req.ContentLength = 5;
  392. req.BeginGetResponse (null, null);
  393. req.Abort ();
  394. req = (HttpWebRequest) WebRequest.Create (url);
  395. req.Timeout = 5000;
  396. req.Method = "GET";
  397. req.ContentLength = 0;
  398. req.BeginGetResponse (null, null);
  399. req.Abort ();
  400. }
  401. }
  402. [Test] // bug #511851
  403. public void BeginGetRequestStream_Request_Aborted ()
  404. {
  405. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  406. string url = "http://" + ep.ToString () + "/test/";
  407. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
  408. responder.Start ();
  409. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  410. req.Method = "POST";
  411. req.Abort ();
  412. try {
  413. req.BeginGetRequestStream (null, null);
  414. Assert.Fail ("#1");
  415. } catch (WebException ex) {
  416. // The request was aborted: The request was canceled
  417. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  418. Assert.IsNull (ex.InnerException, "#3");
  419. Assert.IsNotNull (ex.Message, "#4");
  420. Assert.IsNull (ex.Response, "#5");
  421. Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
  422. }
  423. }
  424. }
  425. [Test] // bug #511851
  426. public void BeginGetResponse_Request_Aborted ()
  427. {
  428. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  429. string url = "http://" + ep.ToString () + "/test/";
  430. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
  431. responder.Start ();
  432. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  433. req.Method = "POST";
  434. req.Abort ();
  435. try {
  436. req.BeginGetResponse (null, null);
  437. Assert.Fail ("#1");
  438. } catch (WebException ex) {
  439. // The request was aborted: The request was canceled
  440. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  441. Assert.IsNull (ex.InnerException, "#3");
  442. Assert.IsNotNull (ex.Message, "#4");
  443. Assert.IsNull (ex.Response, "#5");
  444. Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
  445. }
  446. }
  447. }
  448. [Test]
  449. public void EndGetRequestStream_AsyncResult_Null ()
  450. {
  451. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  452. string url = "http://" + ep.ToString () + "/test/";
  453. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
  454. responder.Start ();
  455. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  456. req.Method = "POST";
  457. req.BeginGetRequestStream (null, null);
  458. try {
  459. req.EndGetRequestStream (null);
  460. Assert.Fail ("#1");
  461. } catch (ArgumentNullException ex) {
  462. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  463. Assert.IsNull (ex.InnerException, "#3");
  464. Assert.IsNotNull (ex.Message, "#4");
  465. Assert.AreEqual ("asyncResult", ex.ParamName, "#5");
  466. } finally {
  467. req.Abort ();
  468. }
  469. }
  470. }
  471. [Test]
  472. [Category ("NotWorking")] // do not get consistent result on MS
  473. public void EndGetRequestStream_Request_Aborted ()
  474. {
  475. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  476. string url = "http://" + ep.ToString () + "/test/";
  477. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
  478. responder.Start ();
  479. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  480. req.Method = "POST";
  481. IAsyncResult ar = req.BeginGetRequestStream (null, null);
  482. req.Abort ();
  483. Thread.Sleep (500);
  484. try {
  485. req.EndGetRequestStream (ar);
  486. Assert.Fail ("#1");
  487. } catch (WebException ex) {
  488. // The request was aborted: The request was canceled
  489. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  490. Assert.IsNull (ex.InnerException, "#3");
  491. Assert.IsNotNull (ex.Message, "#4");
  492. Assert.IsNull (ex.Response, "#5");
  493. Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
  494. }
  495. }
  496. }
  497. [Test] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=471522
  498. [Category ("NotWorking")]
  499. public void EndGetResponse_AsyncResult_Invalid ()
  500. {
  501. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  502. string url = "http://" + ep.ToString () + "/test/";
  503. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
  504. responder.Start ();
  505. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  506. req.Method = "POST";
  507. req.Timeout = 2000;
  508. req.ReadWriteTimeout = 2000;
  509. IAsyncResult ar = req.BeginGetRequestStream (null, null);
  510. // AsyncResult was not returned from call to BeginGetResponse
  511. try {
  512. req.EndGetResponse (ar);
  513. Assert.Fail ();
  514. } catch (InvalidCastException) {
  515. } finally {
  516. req.Abort ();
  517. }
  518. }
  519. }
  520. [Test]
  521. public void EndGetResponse_AsyncResult_Null ()
  522. {
  523. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  524. string url = "http://" + ep.ToString () + "/test/";
  525. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
  526. responder.Start ();
  527. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  528. req.Timeout = 2000;
  529. req.ReadWriteTimeout = 2000;
  530. req.Method = "POST";
  531. IAsyncResult ar = req.BeginGetResponse (null, null);
  532. try {
  533. req.EndGetResponse (null);
  534. Assert.Fail ("#1");
  535. } catch (ArgumentNullException ex) {
  536. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  537. Assert.IsNull (ex.InnerException, "#3");
  538. Assert.IsNotNull (ex.Message, "#4");
  539. Assert.AreEqual ("asyncResult", ex.ParamName, "#5");
  540. } finally {
  541. req.Abort ();
  542. /*
  543. using (HttpWebResponse resp = (HttpWebResponse) req.EndGetResponse (ar)) {
  544. resp.Close ();
  545. }*/
  546. }
  547. }
  548. }
  549. [Test] // bug #429200
  550. public void GetRequestStream ()
  551. {
  552. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  553. string url = "http://" + ep.ToString () + "/test/";
  554. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
  555. responder.Start ();
  556. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  557. req.Method = "POST";
  558. req.Timeout = 2000;
  559. req.ReadWriteTimeout = 2000;
  560. Stream rs1 = req.GetRequestStream ();
  561. Stream rs2 = req.GetRequestStream ();
  562. Assert.IsNotNull (rs1, "#1");
  563. Assert.AreSame (rs1, rs2, "#2");
  564. rs1.Close ();
  565. }
  566. }
  567. [Test] // bug #511851
  568. public void GetRequestStream_Request_Aborted ()
  569. {
  570. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  571. string url = "http://" + ep.ToString () + "/test/";
  572. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
  573. responder.Start ();
  574. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  575. req.Method = "POST";
  576. req.Abort ();
  577. try {
  578. req.GetRequestStream ();
  579. Assert.Fail ("#1");
  580. } catch (WebException ex) {
  581. // The request was aborted: The request was canceled
  582. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  583. Assert.IsNull (ex.InnerException, "#3");
  584. Assert.IsNotNull (ex.Message, "#4");
  585. Assert.IsNull (ex.Response, "#5");
  586. Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
  587. }
  588. }
  589. }
  590. [Test] // bug #510661
  591. [Category ("NotWorking")] // #5842
  592. public void GetRequestStream_Close_NotAllBytesWritten ()
  593. {
  594. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  595. string url = "http://" + ep.ToString () + "/test/";
  596. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
  597. responder.Start ();
  598. HttpWebRequest req;
  599. Stream rs;
  600. req = (HttpWebRequest) WebRequest.Create (url);
  601. req.Method = "POST";
  602. req.ContentLength = 2;
  603. rs = req.GetRequestStream ();
  604. try {
  605. rs.Close ();
  606. Assert.Fail ("#A1");
  607. } catch (WebException ex) {
  608. // The request was aborted: The request was canceled
  609. Assert.AreEqual (typeof (WebException), ex.GetType (), "#A2");
  610. Assert.IsNotNull (ex.Message, "#A3");
  611. Assert.IsNull (ex.Response, "#A4");
  612. Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#A5");
  613. // Cannot close stream until all bytes are written
  614. Exception inner = ex.InnerException;
  615. Assert.IsNotNull (inner, "#A6");
  616. Assert.AreEqual (typeof (IOException), inner.GetType (), "#A7");
  617. Assert.IsNull (inner.InnerException, "#A8");
  618. Assert.IsNotNull (inner.Message, "#A9");
  619. }
  620. req = (HttpWebRequest) WebRequest.Create (url);
  621. req.Method = "POST";
  622. req.ContentLength = 2;
  623. rs = req.GetRequestStream ();
  624. rs.WriteByte (0x0d);
  625. try {
  626. rs.Close ();
  627. Assert.Fail ("#B1");
  628. } catch (WebException ex) {
  629. // The request was aborted: The request was canceled
  630. Assert.AreEqual (typeof (WebException), ex.GetType (), "#B2");
  631. Assert.IsNotNull (ex.Message, "#B3");
  632. Assert.IsNull (ex.Response, "#B4");
  633. Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#B5");
  634. // Cannot close stream until all bytes are written
  635. Exception inner = ex.InnerException;
  636. Assert.IsNotNull (inner, "#B6");
  637. Assert.AreEqual (typeof (IOException), inner.GetType (), "#B7");
  638. Assert.IsNull (inner.InnerException, "#B8");
  639. Assert.IsNotNull (inner.Message, "#B9");
  640. }
  641. req = (HttpWebRequest) WebRequest.Create (url);
  642. req.Method = "POST";
  643. req.ContentLength = 2;
  644. rs = req.GetRequestStream ();
  645. rs.WriteByte (0x0d);
  646. rs.WriteByte (0x0d);
  647. rs.Close ();
  648. }
  649. }
  650. [Test] // bug #510642
  651. [Category ("NotWorking")] // #5842
  652. public void GetRequestStream_Write_Overflow ()
  653. {
  654. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  655. string url = "http://" + ep.ToString () + "/test/";
  656. // buffered, non-chunked
  657. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
  658. responder.Start ();
  659. HttpWebRequest req;
  660. Stream rs;
  661. byte [] buffer;
  662. req = (HttpWebRequest) WebRequest.Create (url);
  663. req.Method = "POST";
  664. req.Timeout = 1000;
  665. req.ReadWriteTimeout = 2000;
  666. req.ContentLength = 2;
  667. rs = req.GetRequestStream ();
  668. rs.WriteByte (0x2c);
  669. buffer = new byte [] { 0x2a, 0x1d };
  670. try {
  671. rs.Write (buffer, 0, buffer.Length);
  672. Assert.Fail ("#A1");
  673. } catch (ProtocolViolationException ex) {
  674. // Bytes to be written to the stream exceed
  675. // Content-Length bytes size specified
  676. Assert.IsNull (ex.InnerException, "#A2");
  677. Assert.IsNotNull (ex.Message, "#A3");
  678. } finally {
  679. req.Abort ();
  680. }
  681. req = (HttpWebRequest) WebRequest.Create (url);
  682. req.Method = "POST";
  683. req.Timeout = 1000;
  684. req.ReadWriteTimeout = 2000;
  685. req.ContentLength = 2;
  686. rs = req.GetRequestStream ();
  687. buffer = new byte [] { 0x2a, 0x2c, 0x1d };
  688. try {
  689. rs.Write (buffer, 0, buffer.Length);
  690. Assert.Fail ("#B1");
  691. } catch (ProtocolViolationException ex) {
  692. // Bytes to be written to the stream exceed
  693. // Content-Length bytes size specified
  694. Assert.IsNull (ex.InnerException, "#B2");
  695. Assert.IsNotNull (ex.Message, "#B3");
  696. } finally {
  697. req.Abort ();
  698. }
  699. }
  700. // buffered, chunked
  701. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
  702. responder.Start ();
  703. HttpWebRequest req;
  704. Stream rs;
  705. byte [] buffer;
  706. /*
  707. req = (HttpWebRequest) WebRequest.Create (url);
  708. req.Method = "POST";
  709. req.SendChunked = true;
  710. req.Timeout = 1000;
  711. req.ReadWriteTimeout = 2000;
  712. req.ContentLength = 2;
  713. rs = req.GetRequestStream ();
  714. rs.WriteByte (0x2c);
  715. buffer = new byte [] { 0x2a, 0x1d };
  716. rs.Write (buffer, 0, buffer.Length);
  717. req.Abort ();
  718. */
  719. req = (HttpWebRequest) WebRequest.Create (url);
  720. req.Method = "POST";
  721. req.SendChunked = true;
  722. req.Timeout = 1000;
  723. req.ReadWriteTimeout = 2000;
  724. req.ContentLength = 2;
  725. rs = req.GetRequestStream ();
  726. buffer = new byte [] { 0x2a, 0x2c, 0x1d };
  727. rs.Write (buffer, 0, buffer.Length);
  728. req.Abort ();
  729. }
  730. // non-buffered, non-chunked
  731. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
  732. responder.Start ();
  733. HttpWebRequest req;
  734. Stream rs;
  735. byte [] buffer;
  736. req = (HttpWebRequest) WebRequest.Create (url);
  737. req.AllowWriteStreamBuffering = false;
  738. req.Method = "POST";
  739. req.Timeout = 1000;
  740. req.ReadWriteTimeout = 2000;
  741. req.ContentLength = 2;
  742. rs = req.GetRequestStream ();
  743. rs.WriteByte (0x2c);
  744. buffer = new byte [] { 0x2a, 0x1d };
  745. try {
  746. rs.Write (buffer, 0, buffer.Length);
  747. Assert.Fail ("#C1");
  748. } catch (ProtocolViolationException ex) {
  749. // Bytes to be written to the stream exceed
  750. // Content-Length bytes size specified
  751. Assert.IsNull (ex.InnerException, "#C2");
  752. Assert.IsNotNull (ex.Message, "#3");
  753. } finally {
  754. req.Abort ();
  755. }
  756. req = (HttpWebRequest) WebRequest.Create (url);
  757. req.AllowWriteStreamBuffering = false;
  758. req.Method = "POST";
  759. req.Timeout = 1000;
  760. req.ReadWriteTimeout = 2000;
  761. req.ContentLength = 2;
  762. rs = req.GetRequestStream ();
  763. buffer = new byte [] { 0x2a, 0x2c, 0x1d };
  764. try {
  765. rs.Write (buffer, 0, buffer.Length);
  766. Assert.Fail ("#D1");
  767. } catch (ProtocolViolationException ex) {
  768. // Bytes to be written to the stream exceed
  769. // Content-Length bytes size specified
  770. Assert.IsNull (ex.InnerException, "#D2");
  771. Assert.IsNotNull (ex.Message, "#D3");
  772. } finally {
  773. req.Abort ();
  774. }
  775. }
  776. // non-buffered, chunked
  777. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
  778. responder.Start ();
  779. HttpWebRequest req;
  780. Stream rs;
  781. byte [] buffer;
  782. req = (HttpWebRequest) WebRequest.Create (url);
  783. req.AllowWriteStreamBuffering = false;
  784. req.Method = "POST";
  785. req.SendChunked = true;
  786. req.Timeout = 1000;
  787. req.ReadWriteTimeout = 2000;
  788. req.ContentLength = 2;
  789. rs = req.GetRequestStream ();
  790. rs.WriteByte (0x2c);
  791. buffer = new byte [] { 0x2a, 0x1d };
  792. rs.Write (buffer, 0, buffer.Length);
  793. req.Abort ();
  794. req = (HttpWebRequest) WebRequest.Create (url);
  795. req.AllowWriteStreamBuffering = false;
  796. req.Method = "POST";
  797. req.SendChunked = true;
  798. req.Timeout = 1000;
  799. req.ReadWriteTimeout = 2000;
  800. req.ContentLength = 2;
  801. rs = req.GetRequestStream ();
  802. buffer = new byte [] { 0x2a, 0x2c, 0x1d };
  803. rs.Write (buffer, 0, buffer.Length);
  804. req.Abort ();
  805. }
  806. }
  807. [Test]
  808. [Ignore ("This test asserts that our code violates RFC 2616")]
  809. public void GetRequestStream_Body_NotAllowed ()
  810. {
  811. string [] methods = new string [] { "GET", "HEAD", "CONNECT",
  812. "get", "HeAd", "ConNect" };
  813. foreach (string method in methods) {
  814. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (
  815. "http://localhost:8000");
  816. req.Method = method;
  817. try {
  818. req.GetRequestStream ();
  819. Assert.Fail ("#1:" + method);
  820. } catch (ProtocolViolationException ex) {
  821. Assert.AreEqual (typeof (ProtocolViolationException), ex.GetType (), "#2:" + method);
  822. Assert.IsNull (ex.InnerException, "#3:" + method);
  823. Assert.IsNotNull (ex.Message, "#4:" + method);
  824. }
  825. }
  826. }
  827. [Test] // bug #511851
  828. public void GetResponse_Request_Aborted ()
  829. {
  830. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  831. string url = "http://" + ep.ToString () + "/test/";
  832. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
  833. responder.Start ();
  834. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  835. req.Method = "POST";
  836. req.Abort ();
  837. try {
  838. req.GetResponse ();
  839. Assert.Fail ("#1");
  840. } catch (WebException ex) {
  841. // The request was aborted: The request was canceled
  842. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  843. Assert.IsNull (ex.InnerException, "#3");
  844. Assert.IsNotNull (ex.Message, "#4");
  845. Assert.IsNull (ex.Response, "#5");
  846. Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
  847. }
  848. }
  849. }
  850. [Test]
  851. [Ignore ("This does not timeout any more. That's how MS works when reading small responses")]
  852. public void ReadTimeout ()
  853. {
  854. IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8764);
  855. string url = "http://" + localEP.ToString () + "/original/";
  856. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
  857. responder.Start ();
  858. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  859. req.Method = "POST";
  860. req.AllowAutoRedirect = false;
  861. req.Timeout = 200;
  862. req.ReadWriteTimeout = 2000;
  863. req.KeepAlive = false;
  864. Stream rs = req.GetRequestStream ();
  865. rs.Close ();
  866. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  867. try {
  868. Stream s = resp.GetResponseStream ();
  869. s.ReadByte ();
  870. Assert.Fail ("#1");
  871. } catch (WebException ex) {
  872. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  873. Assert.IsNull (ex.InnerException, "#3");
  874. Assert.IsNull (ex.Response, "#4");
  875. Assert.AreEqual (WebExceptionStatus.Timeout, ex.Status, "#5");
  876. }
  877. }
  878. responder.Stop ();
  879. }
  880. }
  881. [Test] // bug #324300
  882. public void AllowAutoRedirect ()
  883. {
  884. IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8765);
  885. string url = "http://" + localEP.ToString () + "/original/";
  886. // allow autoredirect
  887. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
  888. responder.Start ();
  889. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  890. req.Method = "POST";
  891. req.Timeout = 2000;
  892. req.ReadWriteTimeout = 2000;
  893. req.KeepAlive = false;
  894. Stream rs = req.GetRequestStream ();
  895. rs.Close ();
  896. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  897. StreamReader sr = new StreamReader (resp.GetResponseStream (),
  898. Encoding.UTF8);
  899. string body = sr.ReadToEnd ();
  900. Assert.AreEqual (resp.StatusCode, HttpStatusCode.OK, "#A1");
  901. Assert.AreEqual (resp.ResponseUri.ToString (), "http://" +
  902. localEP.ToString () + "/moved/", "#A2");
  903. Assert.AreEqual ("GET", resp.Method, "#A3");
  904. Assert.AreEqual ("LOOKS OK", body, "#A4");
  905. }
  906. responder.Stop ();
  907. }
  908. // do not allow autoredirect
  909. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
  910. responder.Start ();
  911. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  912. req.Method = "POST";
  913. req.AllowAutoRedirect = false;
  914. req.Timeout = 1000;
  915. req.ReadWriteTimeout = 1000;
  916. req.KeepAlive = false;
  917. Stream rs = req.GetRequestStream ();
  918. rs.Close ();
  919. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  920. Assert.AreEqual (resp.StatusCode, HttpStatusCode.Found, "#B1");
  921. Assert.AreEqual (url, resp.ResponseUri.ToString (), "#B2");
  922. Assert.AreEqual ("POST", resp.Method, "#B3");
  923. }
  924. responder.Stop ();
  925. }
  926. }
  927. [Test]
  928. public void PostAndRedirect_NoCL ()
  929. {
  930. IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8769);
  931. string url = "http://" + localEP.ToString () + "/original/";
  932. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
  933. responder.Start ();
  934. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  935. req.Method = "POST";
  936. req.Timeout = 2000;
  937. req.ReadWriteTimeout = 2000;
  938. Stream rs = req.GetRequestStream ();
  939. rs.WriteByte (10);
  940. rs.Close ();
  941. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  942. StreamReader sr = new StreamReader (resp.GetResponseStream (),
  943. Encoding.UTF8);
  944. string body = sr.ReadToEnd ();
  945. Assert.AreEqual (resp.StatusCode, HttpStatusCode.OK, "#A1");
  946. Assert.AreEqual (resp.ResponseUri.ToString (), "http://" +
  947. localEP.ToString () + "/moved/", "#A2");
  948. Assert.AreEqual ("GET", resp.Method, "#A3");
  949. Assert.AreEqual ("LOOKS OK", body, "#A4");
  950. }
  951. responder.Stop ();
  952. }
  953. }
  954. [Test]
  955. public void PostAndRedirect_CL ()
  956. {
  957. IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8770);
  958. string url = "http://" + localEP.ToString () + "/original/";
  959. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
  960. responder.Start ();
  961. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  962. req.Method = "POST";
  963. req.Timeout = 2000;
  964. req.ReadWriteTimeout = 2000;
  965. req.ContentLength = 1;
  966. Stream rs = req.GetRequestStream ();
  967. rs.WriteByte (10);
  968. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  969. StreamReader sr = new StreamReader (resp.GetResponseStream (),
  970. Encoding.UTF8);
  971. string body = sr.ReadToEnd ();
  972. Assert.AreEqual (resp.StatusCode, HttpStatusCode.OK, "#A1");
  973. Assert.AreEqual (resp.ResponseUri.ToString (), "http://" +
  974. localEP.ToString () + "/moved/", "#A2");
  975. Assert.AreEqual ("GET", resp.Method, "#A3");
  976. Assert.AreEqual ("LOOKS OK", body, "#A4");
  977. }
  978. responder.Stop ();
  979. }
  980. }
  981. [Test]
  982. public void PostAnd401 ()
  983. {
  984. IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8771);
  985. string url = "http://" + localEP.ToString () + "/original/";
  986. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
  987. responder.Start ();
  988. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  989. req.Method = "POST";
  990. req.Timeout = 2000;
  991. req.ReadWriteTimeout = 2000;
  992. req.ContentLength = 1;
  993. Stream rs = req.GetRequestStream ();
  994. rs.WriteByte (10);
  995. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  996. StreamReader sr = new StreamReader (resp.GetResponseStream (),
  997. Encoding.UTF8);
  998. string body = sr.ReadToEnd ();
  999. Assert.AreEqual (resp.StatusCode, HttpStatusCode.OK, "#A1");
  1000. Assert.AreEqual (resp.ResponseUri.ToString (), "http://" +
  1001. localEP.ToString () + "/moved/", "#A2");
  1002. Assert.AreEqual ("GET", resp.Method, "#A3");
  1003. Assert.AreEqual ("LOOKS OK", body, "#A4");
  1004. }
  1005. responder.Stop ();
  1006. }
  1007. }
  1008. [Test] // bug #324347
  1009. [Category ("NotWorking")]
  1010. public void InternalServerError ()
  1011. {
  1012. IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8766);
  1013. string url = "http://" + localEP.ToString () + "/original/";
  1014. // POST
  1015. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (InternalErrorHandler))) {
  1016. responder.Start ();
  1017. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  1018. req.Method = "POST";
  1019. req.Timeout = 2000;
  1020. req.ReadWriteTimeout = 2000;
  1021. req.KeepAlive = false;
  1022. Stream rs = req.GetRequestStream ();
  1023. rs.Close ();
  1024. try {
  1025. req.GetResponse ();
  1026. Assert.Fail ("#A1");
  1027. } catch (WebException ex) {
  1028. Assert.AreEqual (typeof (WebException), ex.GetType (), "#A2");
  1029. Assert.IsNull (ex.InnerException, "#A3");
  1030. Assert.IsNotNull (ex.Message, "#A4");
  1031. Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#A5");
  1032. HttpWebResponse webResponse = ex.Response as HttpWebResponse;
  1033. Assert.IsNotNull (webResponse, "#A6");
  1034. Assert.AreEqual ("POST", webResponse.Method, "#A7");
  1035. webResponse.Close ();
  1036. }
  1037. responder.Stop ();
  1038. }
  1039. // GET
  1040. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (InternalErrorHandler))) {
  1041. responder.Start ();
  1042. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  1043. req.Method = "GET";
  1044. req.Timeout = 2000;
  1045. req.ReadWriteTimeout = 2000;
  1046. req.KeepAlive = false;
  1047. try {
  1048. req.GetResponse ();
  1049. Assert.Fail ("#B1");
  1050. } catch (WebException ex) {
  1051. Assert.AreEqual (typeof (WebException), ex.GetType (), "#B2");
  1052. Assert.IsNull (ex.InnerException, "#B3");
  1053. Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#B4");
  1054. HttpWebResponse webResponse = ex.Response as HttpWebResponse;
  1055. Assert.IsNotNull (webResponse, "#B5");
  1056. Assert.AreEqual ("GET", webResponse.Method, "#B6");
  1057. webResponse.Close ();
  1058. }
  1059. responder.Stop ();
  1060. }
  1061. }
  1062. [Test]
  1063. [Category ("NotWorking")] // #B3 fails; we get a SocketException: An existing connection was forcibly closed by the remote host
  1064. public void NoContentLength ()
  1065. {
  1066. IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8767);
  1067. string url = "http://" + localEP.ToString () + "/original/";
  1068. // POST
  1069. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (NoContentLengthHandler))) {
  1070. responder.Start ();
  1071. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  1072. req.Method = "POST";
  1073. req.Timeout = 2000;
  1074. req.ReadWriteTimeout = 2000;
  1075. req.KeepAlive = false;
  1076. Stream rs = req.GetRequestStream ();
  1077. rs.Close ();
  1078. try {
  1079. req.GetResponse ();
  1080. Assert.Fail ("#A1");
  1081. } catch (WebException ex) {
  1082. // The underlying connection was closed:
  1083. // An unexpected error occurred on a
  1084. // receive
  1085. Assert.AreEqual (typeof (WebException), ex.GetType (), "#A2");
  1086. Assert.IsNotNull (ex.InnerException, "#A3");
  1087. Assert.AreEqual (WebExceptionStatus.ReceiveFailure, ex.Status, "#A4");
  1088. Assert.AreEqual (typeof (IOException), ex.InnerException.GetType (), "#A5");
  1089. // Unable to read data from the transport connection:
  1090. // A connection attempt failed because the connected party
  1091. // did not properly respond after a period of time, or
  1092. // established connection failed because connected host has
  1093. // failed to respond
  1094. IOException ioe = (IOException) ex.InnerException;
  1095. Assert.IsNotNull (ioe.InnerException, "#A6");
  1096. Assert.IsNotNull (ioe.Message, "#A7");
  1097. Assert.AreEqual (typeof (SocketException), ioe.InnerException.GetType (), "#A8");
  1098. // An existing connection was forcibly
  1099. // closed by the remote host
  1100. SocketException soe = (SocketException) ioe.InnerException;
  1101. Assert.IsNull (soe.InnerException, "#A9");
  1102. Assert.IsNotNull (soe.Message, "#A10");
  1103. HttpWebResponse webResponse = ex.Response as HttpWebResponse;
  1104. Assert.IsNull (webResponse, "#A11");
  1105. }
  1106. responder.Stop ();
  1107. }
  1108. // GET
  1109. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (NoContentLengthHandler))) {
  1110. responder.Start ();
  1111. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  1112. req.Method = "GET";
  1113. req.Timeout = 2000;
  1114. req.ReadWriteTimeout = 2000;
  1115. req.KeepAlive = false;
  1116. try {
  1117. req.GetResponse ();
  1118. Assert.Fail ("#B1");
  1119. } catch (WebException ex) {
  1120. // The remote server returned an error:
  1121. // (500) Internal Server Error
  1122. Assert.AreEqual (typeof (WebException), ex.GetType (), "#B2");
  1123. Assert.IsNull (ex.InnerException, "#B3");
  1124. Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#B4");
  1125. HttpWebResponse webResponse = ex.Response as HttpWebResponse;
  1126. Assert.IsNotNull (webResponse, "#B5");
  1127. Assert.AreEqual ("GET", webResponse.Method, "#B6");
  1128. webResponse.Close ();
  1129. }
  1130. responder.Stop ();
  1131. }
  1132. }
  1133. [Test] // bug #513087
  1134. public void NonStandardVerb ()
  1135. {
  1136. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  1137. string url = "http://" + ep.ToString () + "/moved/";
  1138. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (VerbEchoHandler))) {
  1139. responder.Start ();
  1140. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  1141. req.Method = "WhatEver";
  1142. req.KeepAlive = false;
  1143. req.Timeout = 20000;
  1144. req.ReadWriteTimeout = 20000;
  1145. Stream rs = req.GetRequestStream ();
  1146. rs.Close ();
  1147. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  1148. StreamReader sr = new StreamReader (resp.GetResponseStream (),
  1149. Encoding.UTF8);
  1150. string body = sr.ReadToEnd ();
  1151. Assert.AreEqual (resp.StatusCode, HttpStatusCode.OK, "#1");
  1152. Assert.AreEqual (resp.ResponseUri.ToString (), "http://" +
  1153. ep.ToString () + "/moved/", "#2");
  1154. Assert.AreEqual ("WhatEver", resp.Method, "#3");
  1155. Assert.AreEqual ("WhatEver", body, "#4");
  1156. }
  1157. responder.Stop ();
  1158. }
  1159. }
  1160. [Test]
  1161. [Category ("NotWorking")] // Assert #2 fails
  1162. public void NotModifiedSince ()
  1163. {
  1164. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  1165. string url = "http://" + ep.ToString () + "/test/";
  1166. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (NotModifiedSinceHandler))) {
  1167. responder.Start ();
  1168. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  1169. req.Method = "GET";
  1170. req.KeepAlive = false;
  1171. req.Timeout = 20000;
  1172. req.ReadWriteTimeout = 20000;
  1173. req.Headers.Add (HttpRequestHeader.IfNoneMatch, "898bbr2347056cc2e096afc66e104653");
  1174. req.IfModifiedSince = new DateTime (2010, 01, 04);
  1175. DateTime start = DateTime.Now;
  1176. HttpWebResponse response = null;
  1177. try {
  1178. req.GetResponse ();
  1179. Assert.Fail ("#1");
  1180. } catch (WebException e) {
  1181. response = (HttpWebResponse) e.Response;
  1182. }
  1183. Assert.IsNotNull (response, "#2");
  1184. using (Stream stream = response.GetResponseStream ()) {
  1185. byte [] buffer = new byte [4096];
  1186. int bytesRead = stream.Read (buffer, 0, buffer.Length);
  1187. Assert.AreEqual (0, bytesRead, "#3");
  1188. }
  1189. TimeSpan elapsed = DateTime.Now - start;
  1190. Assert.IsTrue (elapsed.TotalMilliseconds < 2000, "#4");
  1191. responder.Stop ();
  1192. }
  1193. }
  1194. [Test] // bug #353495
  1195. [Category ("NotWorking")]
  1196. public void LastModifiedKind ()
  1197. {
  1198. const string reqURL = "http://coffeefaq.com/site/node/25";
  1199. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (reqURL);
  1200. HttpWebResponse resp = (HttpWebResponse) req.GetResponse ();
  1201. DateTime lastMod = resp.LastModified;
  1202. //string rawLastMod = resp.Headers ["Last-Modified"];
  1203. resp.Close ();
  1204. //Assert.AreEqual ("Tue, 15 Jan 2008 08:59:59 GMT", rawLastMod, "#1");
  1205. Assert.AreEqual (DateTimeKind.Local, lastMod.Kind, "#2");
  1206. req = (HttpWebRequest) WebRequest.Create (reqURL);
  1207. req.IfModifiedSince = lastMod;
  1208. try {
  1209. resp = (HttpWebResponse) req.GetResponse ();
  1210. resp.Close ();
  1211. Assert.Fail ("Should result in 304");
  1212. } catch (WebException ex) {
  1213. Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#3");
  1214. Assert.AreEqual (((HttpWebResponse) ex.Response).StatusCode, HttpStatusCode.NotModified, "#4");
  1215. }
  1216. }
  1217. #region Timeout_Bug // https://bugzilla.novell.com/show_bug.cgi?id=317553
  1218. class TimeoutTestHelper {
  1219. string url_to_test;
  1220. internal DateTime? Start { get; private set; }
  1221. internal DateTime? End { get; private set; }
  1222. internal Exception Exception { get; private set; }
  1223. internal string Body { get; private set; }
  1224. internal int TimeOutInMilliSeconds { get; private set; }
  1225. internal TimeoutTestHelper (string url, int timeoutInMilliseconds)
  1226. {
  1227. url_to_test = url;
  1228. TimeOutInMilliSeconds = timeoutInMilliseconds;
  1229. }
  1230. internal void LaunchWebRequest ()
  1231. {
  1232. var req = (HttpWebRequest) WebRequest.Create (url_to_test);
  1233. req.Timeout = TimeOutInMilliSeconds;
  1234. Start = DateTime.Now;
  1235. try {
  1236. using (var resp = (HttpWebResponse) req.GetResponse ())
  1237. {
  1238. var sr = new StreamReader (resp.GetResponseStream (), Encoding.UTF8);
  1239. Body = sr.ReadToEnd ();
  1240. }
  1241. } catch (Exception e) {
  1242. End = DateTime.Now;
  1243. Exception = e;
  1244. }
  1245. }
  1246. }
  1247. void TestTimeOut (string url, WebExceptionStatus expectedExceptionStatus)
  1248. {
  1249. var timeoutWorker = new TimeoutTestHelper (url, three_seconds_in_milliseconds);
  1250. var threadStart = new ThreadStart (timeoutWorker.LaunchWebRequest);
  1251. var thread = new Thread (threadStart);
  1252. thread.Start ();
  1253. Thread.Sleep (three_seconds_in_milliseconds * 3);
  1254. if (timeoutWorker.End == null) {
  1255. thread.Abort ();
  1256. Assert.Fail ("Thread finished after triple the timeout specified has passed");
  1257. }
  1258. if (!String.IsNullOrEmpty (timeoutWorker.Body)) {
  1259. if (timeoutWorker.Body == response_of_timeout_handler) {
  1260. Assert.Fail ("Should not be reached, timeout exception was not thrown and webrequest managed to retrieve proper body");
  1261. }
  1262. Assert.Fail ("Should not be reached, timeout exception was not thrown and webrequest managed to retrieve an incorrect body: " + timeoutWorker.Body);
  1263. }
  1264. Assert.IsNotNull (timeoutWorker.Exception, "Exception was not thrown");
  1265. var webEx = timeoutWorker.Exception as WebException;
  1266. Assert.IsNotNull (webEx, "Exception thrown should be WebException, but was: " +
  1267. timeoutWorker.Exception.GetType ().FullName);
  1268. Assert.AreEqual (expectedExceptionStatus, webEx.Status,
  1269. "WebException was thrown, but with a wrong status (should be " + expectedExceptionStatus + "): " + webEx.Status);
  1270. Assert.IsFalse (timeoutWorker.End > (timeoutWorker.Start + TimeSpan.FromMilliseconds (three_seconds_in_milliseconds + 500)),
  1271. "Timeout exception should have been thrown shortly after timeout is reached, however it was at least half-second late");
  1272. }
  1273. [Test] // 1st possible case of https://bugzilla.novell.com/show_bug.cgi?id=MONO74177
  1274. public void TestTimeoutPropertyWithServerThatExistsAndRespondsButTooLate ()
  1275. {
  1276. var ep = new IPEndPoint (IPAddress.Loopback, 8123);
  1277. string url = "http://" + ep + "/foobar/";
  1278. using (var responder = new SocketResponder (ep, TimeOutHandler))
  1279. {
  1280. responder.Start ();
  1281. TestTimeOut (url, WebExceptionStatus.Timeout);
  1282. responder.Stop ();
  1283. }
  1284. }
  1285. [Test] // 2nd possible case of https://bugzilla.novell.com/show_bug.cgi?id=MONO74177
  1286. public void TestTimeoutWithEndpointThatDoesntExistThrowsConnectFailureBeforeTimeout ()
  1287. {
  1288. string url = "http://127.0.0.1:8271/"; // some endpoint that is unlikely to exist
  1289. // connecting to a non-existing endpoint should throw a ConnectFailure before the timeout is reached
  1290. TestTimeOut (url, WebExceptionStatus.ConnectFailure);
  1291. }
  1292. const string response_of_timeout_handler = "RESPONSE_OF_TIMEOUT_HANDLER";
  1293. const int three_seconds_in_milliseconds = 3000;
  1294. private static byte[] TimeOutHandler (Socket socket)
  1295. {
  1296. socket.Receive (new byte[4096]);
  1297. Thread.Sleep (three_seconds_in_milliseconds * 2);
  1298. var sw = new StringWriter ();
  1299. sw.WriteLine ("HTTP/1.1 200 OK");
  1300. sw.WriteLine ("Content-Type: text/plain");
  1301. sw.WriteLine ("Content-Length: " + response_of_timeout_handler.Length);
  1302. sw.WriteLine ();
  1303. sw.Write (response_of_timeout_handler);
  1304. sw.Flush ();
  1305. return Encoding.UTF8.GetBytes (sw.ToString ());
  1306. }
  1307. #endregion
  1308. internal static byte [] EchoRequestHandler (Socket socket)
  1309. {
  1310. MemoryStream ms = new MemoryStream ();
  1311. byte [] buffer = new byte [4096];
  1312. int bytesReceived = socket.Receive (buffer);
  1313. while (bytesReceived > 0) {
  1314. ms.Write (buffer, 0, bytesReceived);
  1315. // We don't check for Content-Length or anything else here, so we give the client a little time to write
  1316. // after sending the headers
  1317. Thread.Sleep (200);
  1318. if (socket.Available > 0) {
  1319. bytesReceived = socket.Receive (buffer);
  1320. } else {
  1321. bytesReceived = 0;
  1322. }
  1323. }
  1324. ms.Flush ();
  1325. ms.Position = 0;
  1326. StreamReader sr = new StreamReader (ms, Encoding.UTF8);
  1327. string request = sr.ReadToEnd ();
  1328. StringWriter sw = new StringWriter ();
  1329. sw.WriteLine ("HTTP/1.1 200 OK");
  1330. sw.WriteLine ("Content-Type: text/xml");
  1331. sw.WriteLine ("Content-Length: " + request.Length.ToString (CultureInfo.InvariantCulture));
  1332. sw.WriteLine ();
  1333. sw.Write (request);
  1334. sw.Flush ();
  1335. return Encoding.UTF8.GetBytes (sw.ToString ());
  1336. }
  1337. static byte [] RedirectRequestHandler (Socket socket)
  1338. {
  1339. MemoryStream ms = new MemoryStream ();
  1340. byte [] buffer = new byte [4096];
  1341. int bytesReceived = socket.Receive (buffer);
  1342. while (bytesReceived > 0) {
  1343. ms.Write (buffer, 0, bytesReceived);
  1344. // We don't check for Content-Length or anything else here, so we give the client a little time to write
  1345. // after sending the headers
  1346. Thread.Sleep (200);
  1347. if (socket.Available > 0) {
  1348. bytesReceived = socket.Receive (buffer);
  1349. } else {
  1350. bytesReceived = 0;
  1351. }
  1352. }
  1353. ms.Flush ();
  1354. ms.Position = 0;
  1355. string statusLine = null;
  1356. using (StreamReader sr = new StreamReader (ms, Encoding.UTF8)) {
  1357. statusLine = sr.ReadLine ();
  1358. }
  1359. StringWriter sw = new StringWriter ();
  1360. if (statusLine.StartsWith ("POST /original/")) {
  1361. sw.WriteLine ("HTTP/1.0 302 Found");
  1362. EndPoint ep = socket.LocalEndPoint;
  1363. sw.WriteLine ("Location: " + "http://" + ep.ToString () + "/moved/");
  1364. sw.WriteLine ();
  1365. sw.Flush ();
  1366. } else if (statusLine.StartsWith ("GET /moved/")) {
  1367. sw.WriteLine ("HTTP/1.0 200 OK");
  1368. sw.WriteLine ("Content-Type: text/plain");
  1369. sw.WriteLine ("Content-Length: 8");
  1370. sw.WriteLine ();
  1371. sw.Write ("LOOKS OK");
  1372. sw.Flush ();
  1373. } else {
  1374. sw.WriteLine ("HTTP/1.0 500 Too Lazy");
  1375. sw.WriteLine ();
  1376. sw.Flush ();
  1377. }
  1378. return Encoding.UTF8.GetBytes (sw.ToString ());
  1379. }
  1380. static byte [] InternalErrorHandler (Socket socket)
  1381. {
  1382. byte [] buffer = new byte [4096];
  1383. int bytesReceived = socket.Receive (buffer);
  1384. while (bytesReceived > 0) {
  1385. // We don't check for Content-Length or anything else here, so we give the client a little time to write
  1386. // after sending the headers
  1387. Thread.Sleep (200);
  1388. if (socket.Available > 0) {
  1389. bytesReceived = socket.Receive (buffer);
  1390. } else {
  1391. bytesReceived = 0;
  1392. }
  1393. }
  1394. StringWriter sw = new StringWriter ();
  1395. sw.WriteLine ("HTTP/1.1 500 Too Lazy");
  1396. sw.WriteLine ("Content-Length: 0");
  1397. sw.WriteLine ();
  1398. sw.Flush ();
  1399. return Encoding.UTF8.GetBytes (sw.ToString ());
  1400. }
  1401. static byte [] NoContentLengthHandler (Socket socket)
  1402. {
  1403. StringWriter sw = new StringWriter ();
  1404. sw.WriteLine ("HTTP/1.1 500 Too Lazy");
  1405. sw.WriteLine ();
  1406. sw.Flush ();
  1407. return Encoding.UTF8.GetBytes (sw.ToString ());
  1408. }
  1409. static byte [] NotModifiedSinceHandler (Socket socket)
  1410. {
  1411. StringWriter sw = new StringWriter ();
  1412. sw.WriteLine ("HTTP/1.1 304 Not Modified");
  1413. sw.WriteLine ("Date: Fri, 06 Feb 2009 12:50:26 GMT");
  1414. sw.WriteLine ("Server: Apache/2.2.6 (Debian) PHP/5.2.6-2+b1 with Suhosin-Patch mod_ssl/2.2.6 OpenSSL/0.9.8g");
  1415. sw.WriteLine ("Not-Modified-Since: Sun, 08 Feb 2009 08:49:26 GMT");
  1416. sw.WriteLine ("ETag: 898bbr2347056cc2e096afc66e104653");
  1417. sw.WriteLine ("Connection: close");
  1418. sw.WriteLine ();
  1419. sw.Flush ();
  1420. return Encoding.UTF8.GetBytes (sw.ToString ());
  1421. }
  1422. static byte [] VerbEchoHandler (Socket socket)
  1423. {
  1424. MemoryStream ms = new MemoryStream ();
  1425. byte [] buffer = new byte [4096];
  1426. int bytesReceived = socket.Receive (buffer);
  1427. while (bytesReceived > 0) {
  1428. ms.Write (buffer, 0, bytesReceived);
  1429. // We don't check for Content-Length or anything else here, so we give the client a little time to write
  1430. // after sending the headers
  1431. Thread.Sleep (200);
  1432. if (socket.Available > 0) {
  1433. bytesReceived = socket.Receive (buffer);
  1434. } else {
  1435. bytesReceived = 0;
  1436. }
  1437. }
  1438. ms.Flush ();
  1439. ms.Position = 0;
  1440. string statusLine = null;
  1441. using (StreamReader sr = new StreamReader (ms, Encoding.UTF8)) {
  1442. statusLine = sr.ReadLine ();
  1443. }
  1444. string verb = "DEFAULT";
  1445. if (statusLine != null) {
  1446. string [] parts = statusLine.Split (' ');
  1447. if (parts.Length > 0)
  1448. verb = parts [0];
  1449. }
  1450. StringWriter sw = new StringWriter ();
  1451. sw.WriteLine ("HTTP/1.1 200 OK");
  1452. sw.WriteLine ("Content-Type: text/plain");
  1453. sw.WriteLine ("Content-Length: " + verb.Length);
  1454. sw.WriteLine ();
  1455. sw.Write (verb);
  1456. sw.Flush ();
  1457. return Encoding.UTF8.GetBytes (sw.ToString ());
  1458. }
  1459. static byte [] PostAnd401Handler (Socket socket)
  1460. {
  1461. MemoryStream ms = new MemoryStream ();
  1462. byte [] buffer = new byte [4096];
  1463. int bytesReceived = socket.Receive (buffer);
  1464. while (bytesReceived > 0) {
  1465. ms.Write (buffer, 0, bytesReceived);
  1466. // We don't check for Content-Length or anything else here, so we give the client a little time to write
  1467. // after sending the headers
  1468. Thread.Sleep (200);
  1469. if (socket.Available > 0) {
  1470. bytesReceived = socket.Receive (buffer);
  1471. } else {
  1472. bytesReceived = 0;
  1473. }
  1474. }
  1475. ms.Flush ();
  1476. ms.Position = 0;
  1477. string statusLine = null;
  1478. bool have_auth = false;
  1479. int cl = -1;
  1480. using (StreamReader sr = new StreamReader (ms, Encoding.UTF8)) {
  1481. string l;
  1482. while ((l = sr.ReadLine ()) != null) {
  1483. if (statusLine == null) {
  1484. statusLine = l;
  1485. } else if (l.StartsWith ("Authorization:")) {
  1486. have_auth = true;
  1487. } else if (l.StartsWith ("Content-Length:")) {
  1488. cl = Int32.Parse (l.Substring ("content-length: ".Length));
  1489. }
  1490. }
  1491. }
  1492. StringWriter sw = new StringWriter ();
  1493. if (!have_auth) {
  1494. sw.WriteLine ("HTTP/1.0 401 Invalid Credentials");
  1495. sw.WriteLine ("WWW-Authenticate: basic Yeah");
  1496. sw.WriteLine ();
  1497. sw.Flush ();
  1498. } else if (cl > 0 && statusLine.StartsWith ("POST ")) {
  1499. sw.WriteLine ("HTTP/1.0 200 OK");
  1500. sw.WriteLine ("Content-Type: text/plain");
  1501. sw.WriteLine ("Content-Length: 8");
  1502. sw.WriteLine ();
  1503. sw.Write ("LOOKS OK");
  1504. sw.Flush ();
  1505. } else {
  1506. sw.WriteLine ("HTTP/1.0 500 test failed");
  1507. sw.WriteLine ("Content-Length: 0");
  1508. sw.WriteLine ();
  1509. sw.Flush ();
  1510. }
  1511. return Encoding.UTF8.GetBytes (sw.ToString ());
  1512. }
  1513. [Test]
  1514. public void NtlmAuthentication ()
  1515. {
  1516. NtlmServer server = new NtlmServer ();
  1517. server.Start ();
  1518. string url = String.Format ("http://{0}:{1}/nothing.html", server.IPAddress, server.Port);
  1519. HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
  1520. request.Timeout = 5000;
  1521. request.Credentials = new NetworkCredential ("user", "password", "domain");
  1522. HttpWebResponse resp = (HttpWebResponse) request.GetResponse ();
  1523. string res = null;
  1524. using (StreamReader reader = new StreamReader (resp.GetResponseStream ())) {
  1525. res = reader.ReadToEnd ();
  1526. }
  1527. resp.Close ();
  1528. server.Stop ();
  1529. Assert.AreEqual ("OK", res);
  1530. }
  1531. class NtlmServer : HttpServer {
  1532. public string Where = "";
  1533. protected override void Run ()
  1534. {
  1535. Where = "before accept";
  1536. Socket client = sock.Accept ();
  1537. NetworkStream ns = new NetworkStream (client, false);
  1538. StreamReader reader = new StreamReader (ns, Encoding.ASCII);
  1539. string line;
  1540. Where = "first read";
  1541. while ((line = reader.ReadLine ()) != null) {
  1542. if (line.Trim () == String.Empty) {
  1543. break;
  1544. }
  1545. }
  1546. Where = "first write";
  1547. StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
  1548. writer.Write ( "HTTP/1.1 401 Unauthorized\r\n" +
  1549. "WWW-Authenticate: NTLM\r\n" +
  1550. "Content-Length: 5\r\n\r\nWRONG");
  1551. writer.Flush ();
  1552. Where = "second read";
  1553. while ((line = reader.ReadLine ()) != null) {
  1554. if (line.Trim () == String.Empty) {
  1555. break;
  1556. }
  1557. }
  1558. Where = "second write";
  1559. writer.Write ( "HTTP/1.1 401 Unauthorized\r\n" +
  1560. "WWW-Authenticate: NTLM TlRMTVNTUAACAAAAAAAAADgAAAABggAC8GDhqIONH3sAAAAAAAAAAAAAAAA4AAAABQLODgAAAA8=\r\n" +
  1561. "Content-Length: 5\r\n\r\nWRONG");
  1562. writer.Flush ();
  1563. Where = "third read";
  1564. while ((line = reader.ReadLine ()) != null) {
  1565. if (line.Trim () == String.Empty) {
  1566. break;
  1567. }
  1568. }
  1569. Where = "third write";
  1570. writer.Write ( "HTTP/1.1 200 OK\r\n" +
  1571. "Keep-Alive: true\r\n" +
  1572. "Content-Length: 2\r\n\r\nOK");
  1573. writer.Flush ();
  1574. Thread.Sleep (1000);
  1575. writer.Close ();
  1576. reader.Close ();
  1577. client.Close ();
  1578. }
  1579. }
  1580. class BadChunkedServer : HttpServer {
  1581. protected override void Run ()
  1582. {
  1583. Socket client = sock.Accept ();
  1584. NetworkStream ns = new NetworkStream (client, true);
  1585. StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
  1586. writer.Write ( "HTTP/1.1 200 OK\r\n" +
  1587. "Transfer-Encoding: chunked\r\n" +
  1588. "Connection: close\r\n" +
  1589. "Content-Type: text/plain; charset=UTF-8\r\n\r\n");
  1590. // This body lacks a 'last-chunk' (see RFC 2616)
  1591. writer.Write ("10\r\n1234567890123456\r\n");
  1592. writer.Flush ();
  1593. client.Shutdown (SocketShutdown.Send);
  1594. Thread.Sleep (1000);
  1595. writer.Close ();
  1596. }
  1597. }
  1598. class AcceptAllPolicy : ICertificatePolicy {
  1599. public bool CheckValidationResult (ServicePoint sp, X509Certificate certificate, WebRequest request, int error)
  1600. {
  1601. return true;
  1602. }
  1603. }
  1604. abstract class HttpServer
  1605. {
  1606. protected Socket sock;
  1607. protected Exception error;
  1608. protected ManualResetEvent evt;
  1609. public HttpServer ()
  1610. {
  1611. sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  1612. sock.Bind (new IPEndPoint (IPAddress.Loopback, 0));
  1613. sock.Listen (1);
  1614. }
  1615. public void Start ()
  1616. {
  1617. evt = new ManualResetEvent (false);
  1618. Thread th = new Thread (new ThreadStart (Run));
  1619. th.Start ();
  1620. }
  1621. public void Stop ()
  1622. {
  1623. evt.Set ();
  1624. sock.Close ();
  1625. }
  1626. public IPAddress IPAddress {
  1627. get { return ((IPEndPoint) sock.LocalEndPoint).Address; }
  1628. }
  1629. public int Port {
  1630. get { return ((IPEndPoint) sock.LocalEndPoint).Port; }
  1631. }
  1632. public Exception Error {
  1633. get { return error; }
  1634. }
  1635. protected abstract void Run ();
  1636. }
  1637. [Test]
  1638. public void BeginGetRequestStream ()
  1639. {
  1640. this.DoRequest (
  1641. (r, c) =>
  1642. {
  1643. r.Method = "POST";
  1644. r.ContentLength = 0;
  1645. r.BeginGetRequestStream ((a) =>
  1646. {
  1647. using (Stream s = r.EndGetRequestStream (a)) { };
  1648. c.Set();
  1649. },
  1650. null);
  1651. },
  1652. (c) => { });
  1653. }
  1654. [Test]
  1655. public void BeginGetRequestStreamNoClose ()
  1656. {
  1657. this.DoRequest (
  1658. (r, c) => {
  1659. r.Method = "POST";
  1660. r.ContentLength = 1;
  1661. r.BeginGetRequestStream ((a) =>
  1662. {
  1663. r.EndGetRequestStream (a);
  1664. c.Set ();
  1665. },
  1666. null);
  1667. },
  1668. (c) => {});
  1669. }
  1670. [Test]
  1671. public void BeginGetRequestStreamCancelIfNotAllBytesWritten ()
  1672. {
  1673. this.DoRequest (
  1674. (r, c) =>
  1675. {
  1676. r.Method = "POST";
  1677. r.ContentLength = 10;
  1678. r.BeginGetRequestStream ((a) =>
  1679. {
  1680. WebException ex = ExceptionAssert.Throws<WebException> (() =>
  1681. {
  1682. using (Stream s = r.EndGetRequestStream (a)) {
  1683. }
  1684. }
  1685. );
  1686. Assert.AreEqual (ex.Status, WebExceptionStatus.RequestCanceled);
  1687. c.Set();
  1688. },
  1689. null);
  1690. },
  1691. (c) => { });
  1692. }
  1693. [Test]
  1694. public void GetRequestStream2 ()
  1695. {
  1696. this.DoRequest (
  1697. (r, c) =>
  1698. {
  1699. r.Method = "POST";
  1700. r.ContentLength = data64KB.Length;
  1701. using (Stream s = r.GetRequestStream ()) {
  1702. s.Write (data64KB, 0, data64KB.Length);
  1703. }
  1704. c.Set ();
  1705. },
  1706. (c) => { });
  1707. }
  1708. [Test]
  1709. public void GetRequestStreamNotAllBytesWritten ()
  1710. {
  1711. this.DoRequest (
  1712. (r, c) =>
  1713. {
  1714. r.Method = "POST";
  1715. r.ContentLength = data64KB.Length;
  1716. WebException ex = ExceptionAssert.Throws<WebException> (() => r.GetRequestStream ().Close ());
  1717. Assert.AreEqual (ex.Status, WebExceptionStatus.RequestCanceled);
  1718. c.Set ();
  1719. },
  1720. (c) => {});
  1721. }
  1722. [Test]
  1723. public void GetRequestStreamTimeout ()
  1724. {
  1725. this.DoRequest (
  1726. (r, c) =>
  1727. {
  1728. r.Method = "POST";
  1729. r.ContentLength = data64KB.Length;
  1730. r.Timeout = 100;
  1731. WebException ex = ExceptionAssert.Throws<WebException> (() => r.GetRequestStream ());
  1732. Assert.IsTrue (ex.Status == WebExceptionStatus.Timeout || ex.Status == WebExceptionStatus.ConnectFailure);
  1733. c.Set();
  1734. });
  1735. }
  1736. [Test]
  1737. public void BeginWrite ()
  1738. {
  1739. byte[] received = new byte[data64KB.Length];
  1740. this.DoRequest (
  1741. (r, c) =>
  1742. {
  1743. r.Method = "POST";
  1744. r.ContentLength = data64KB.Length;
  1745. Stream s = r.GetRequestStream ();
  1746. s.BeginWrite (data64KB, 0, data64KB.Length,
  1747. (a) =>
  1748. {
  1749. s.EndWrite (a);
  1750. s.Close ();
  1751. r.GetResponse ().Close ();
  1752. c.Set();
  1753. },
  1754. null);
  1755. },
  1756. (c) =>
  1757. {
  1758. c.Request.InputStream.ReadAll (received, 0, received.Length);
  1759. c.Response.StatusCode = 204;
  1760. c.Response.Close ();
  1761. });
  1762. Assert.AreEqual (data64KB, received);
  1763. }
  1764. [Test]
  1765. public void BeginWriteAfterAbort ()
  1766. {
  1767. byte [] received = new byte [data64KB.Length];
  1768. this.DoRequest (
  1769. (r, c) =>
  1770. {
  1771. r.Method = "POST";
  1772. r.ContentLength = data64KB.Length;
  1773. Stream s = r.GetRequestStream ();
  1774. r.Abort();
  1775. WebException ex = ExceptionAssert.Throws<WebException> (() => s.BeginWrite (data64KB, 0, data64KB.Length, null, null));
  1776. Assert.AreEqual (ex.Status, WebExceptionStatus.RequestCanceled);
  1777. c.Set();
  1778. },
  1779. (c) =>
  1780. {
  1781. //c.Request.InputStream.ReadAll (received, 0, received.Length);
  1782. //c.Response.StatusCode = 204;
  1783. //c.Response.Close();
  1784. });
  1785. }
  1786. [Test]
  1787. public void PrematureStreamCloseAborts ()
  1788. {
  1789. byte [] received = new byte [data64KB.Length];
  1790. this.DoRequest (
  1791. (r, c) =>
  1792. {
  1793. r.Method = "POST";
  1794. r.ContentLength = data64KB.Length * 2;
  1795. Stream s = r.GetRequestStream ();
  1796. s.Write (data64KB, 0, data64KB.Length);
  1797. WebException ex = ExceptionAssert.Throws<WebException>(() => s.Close());
  1798. Assert.AreEqual(ex.Status, WebExceptionStatus.RequestCanceled);
  1799. c.Set();
  1800. },
  1801. (c) =>
  1802. {
  1803. c.Request.InputStream.ReadAll (received, 0, received.Length);
  1804. // c.Response.StatusCode = 204;
  1805. // c.Response.Close ();
  1806. });
  1807. }
  1808. [Test]
  1809. public void Write ()
  1810. {
  1811. byte [] received = new byte [data64KB.Length];
  1812. this.DoRequest (
  1813. (r, c) =>
  1814. {
  1815. r.Method = "POST";
  1816. r.ContentLength = data64KB.Length;
  1817. using (Stream s = r.GetRequestStream ()) {
  1818. s.Write (data64KB, 0, data64KB.Length);
  1819. }
  1820. r.GetResponse ().Close ();
  1821. c.Set ();
  1822. },
  1823. (c) =>
  1824. {
  1825. c.Request.InputStream.ReadAll (received, 0, received.Length);
  1826. c.Response.StatusCode = 204;
  1827. c.Response.Close ();
  1828. });
  1829. Assert.AreEqual(data64KB, received);
  1830. }
  1831. /*
  1832. Invalid test: it does not work on linux.
  1833. [pid 30973] send(9, "POST / HTTP/1.1\r\nContent-Length:"..., 89, 0) = 89
  1834. Abort set
  1835. [pid 30970] send(16, "HTTP/1.1 200 OK\r\nServer: Mono-HT"..., 133, 0) = 133
  1836. Calling abort
  1837. [pid 30970] close(16) = 0
  1838. Closing!!!
  1839. [pid 30980] send(9, "\213t\326\350\312u\36n\234\351\225L\r\243a\200\226\371\350F\271~oZ\32\270\24\226z4\211\345"..., 65536, 0) = 65536
  1840. Writing...
  1841. [pid 30966] close(4) = 0
  1842. OK
  1843. *
  1844. The server sideis closed (FD 16) and the send on the client side (FD 9) succeeds.
  1845. [Test]
  1846. [Category("NotWorking")]
  1847. public void WriteServerAborts ()
  1848. {
  1849. ManualResetEvent abort = new ManualResetEvent (false);
  1850. byte [] received = new byte [data64KB.Length];
  1851. this.DoRequest (
  1852. (r, c) =>
  1853. {
  1854. r.Method = "POST";
  1855. r.ContentLength = data64KB.Length;
  1856. using (Stream s = r.GetRequestStream()) {
  1857. abort.Set();
  1858. Thread.Sleep(100);
  1859. IOException ex = ExceptionAssert.Throws<IOException> (() => s.Write(data64KB, 0, data64KB.Length));
  1860. }
  1861. c.Set();
  1862. },
  1863. (c) =>
  1864. {
  1865. abort.WaitOne();
  1866. c.Response.Abort();
  1867. });
  1868. }
  1869. **/
  1870. [Test]
  1871. public void Read ()
  1872. {
  1873. byte [] received = new byte [data64KB.Length];
  1874. this.DoRequest (
  1875. (r, c) =>
  1876. {
  1877. using (HttpWebResponse x = (HttpWebResponse) r.GetResponse ())
  1878. using (Stream s = x.GetResponseStream()) {
  1879. s.ReadAll (received, 0, received.Length);
  1880. }
  1881. c.Set ();
  1882. },
  1883. (c) =>
  1884. {
  1885. c.Response.StatusCode = 200;
  1886. c.Response.ContentLength64 = data64KB.Length;
  1887. c.Response.OutputStream.Write (data64KB, 0, data64KB.Length);
  1888. c.Response.OutputStream.Close ();
  1889. c.Response.Close ();
  1890. });
  1891. Assert.AreEqual (data64KB, received);
  1892. }
  1893. [Test]
  1894. public void ReadTimeout2 ()
  1895. {
  1896. byte [] received = new byte [data64KB.Length];
  1897. this.DoRequest (
  1898. (r, c) =>
  1899. {
  1900. r.ReadWriteTimeout = 10;
  1901. using (HttpWebResponse x = (HttpWebResponse) r.GetResponse ())
  1902. using (Stream s = x.GetResponseStream ()) {
  1903. WebException ex = ExceptionAssert.Throws<WebException> (() => s.ReadAll (received, 0, received.Length));
  1904. Assert.AreEqual (ex.Status, WebExceptionStatus.Timeout);
  1905. }
  1906. c.Set();
  1907. },
  1908. (c) =>
  1909. {
  1910. c.Response.StatusCode = 200;
  1911. c.Response.ContentLength64 = data64KB.Length;
  1912. c.Response.OutputStream.Write (data64KB, 0, data64KB.Length / 2);
  1913. Thread.Sleep (1000);
  1914. // c.Response.OutputStream.Write (data64KB, data64KB.Length / 2, data64KB.Length / 2);
  1915. c.Response.OutputStream.Close ();
  1916. c.Response.Close ();
  1917. });
  1918. }
  1919. [Test]
  1920. public void ReadServerAborted ()
  1921. {
  1922. byte [] received = new byte [data64KB.Length];
  1923. this.DoRequest (
  1924. (r, c) =>
  1925. {
  1926. using (HttpWebResponse x = (HttpWebResponse) r.GetResponse ())
  1927. using (Stream s = x.GetResponseStream ()) {
  1928. Assert.AreEqual (1, s.ReadAll (received, 0, received.Length));
  1929. }
  1930. c.Set();
  1931. },
  1932. (c) =>
  1933. {
  1934. c.Response.StatusCode = 200;
  1935. c.Response.ContentLength64 = data64KB.Length;
  1936. c.Response.OutputStream.Write (data64KB, 0, 1);
  1937. c.Response.Abort ();
  1938. });
  1939. }
  1940. [Test]
  1941. public void BeginGetResponse2 ()
  1942. {
  1943. byte [] received = new byte [data64KB.Length];
  1944. this.DoRequest (
  1945. (r, c) =>
  1946. {
  1947. r.BeginGetResponse ((a) =>
  1948. {
  1949. using (HttpWebResponse x = (HttpWebResponse) r.EndGetResponse (a))
  1950. using (Stream s = x.GetResponseStream ()) {
  1951. s.ReadAll (received, 0, received.Length);
  1952. }
  1953. c.Set();
  1954. }, null);
  1955. },
  1956. (c) =>
  1957. {
  1958. c.Response.StatusCode = 200;
  1959. c.Response.ContentLength64 = data64KB.Length;
  1960. c.Response.OutputStream.Write (data64KB, 0, data64KB.Length);
  1961. c.Response.OutputStream.Close ();
  1962. c.Response.Close ();
  1963. });
  1964. Assert.AreEqual (data64KB, received);
  1965. }
  1966. [Test]
  1967. public void BeginGetResponseAborts ()
  1968. {
  1969. ManualResetEvent aborted = new ManualResetEvent(false);
  1970. this.DoRequest (
  1971. (r, c) =>
  1972. {
  1973. r.BeginGetResponse((a) =>
  1974. {
  1975. WebException ex = ExceptionAssert.Throws<WebException> (() => r.EndGetResponse (a));
  1976. Assert.AreEqual (ex.Status, WebExceptionStatus.RequestCanceled);
  1977. c.Set ();
  1978. }, null);
  1979. aborted.WaitOne ();
  1980. r.Abort ();
  1981. },
  1982. (c) =>
  1983. {
  1984. aborted.Set ();
  1985. // Thread.Sleep (100);
  1986. // c.Response.StatusCode = 200;
  1987. // c.Response.ContentLength64 = 0;
  1988. // c.Response.Close ();
  1989. });
  1990. return;
  1991. }
  1992. void DoRequest (Action<HttpWebRequest, EventWaitHandle> request)
  1993. {
  1994. int port = NetworkHelpers.FindFreePort ();
  1995. ManualResetEvent completed = new ManualResetEvent (false);
  1996. Uri address = new Uri (string.Format ("http://localhost:{0}", port));
  1997. HttpWebRequest client = (HttpWebRequest) WebRequest.Create (address);
  1998. request (client, completed);
  1999. if (!completed.WaitOne (10000))
  2000. Assert.Fail ("Test hung");
  2001. }
  2002. void DoRequest (Action<HttpWebRequest, EventWaitHandle> request, Action<HttpListenerContext> processor)
  2003. {
  2004. int port = NetworkHelpers.FindFreePort ();
  2005. ManualResetEvent [] completed = new ManualResetEvent [2];
  2006. completed [0] = new ManualResetEvent (false);
  2007. completed [1] = new ManualResetEvent (false);
  2008. using (ListenerScope scope = new ListenerScope (processor, port, completed [0])) {
  2009. ManualResetEvent clientCompleted = new ManualResetEvent (false);
  2010. Uri address = new Uri (string.Format ("http://localhost:{0}", port));
  2011. HttpWebRequest client = (HttpWebRequest) WebRequest.Create (address);
  2012. ThreadPool.QueueUserWorkItem ((o) => request (client, completed [1]));
  2013. if (!WaitHandle.WaitAll (completed, 10000))
  2014. Assert.Fail ("Test hung.");
  2015. }
  2016. }
  2017. #if NET_4_0
  2018. [Test]
  2019. [ExpectedException (typeof (ArgumentNullException))]
  2020. public void NullHost ()
  2021. {
  2022. HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
  2023. req.Host = null;
  2024. }
  2025. [Test]
  2026. public void NoHost ()
  2027. {
  2028. HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
  2029. Assert.AreEqual (req.Host, "go-mono.com");
  2030. }
  2031. [Test]
  2032. [ExpectedException (typeof (ArgumentException))]
  2033. public void EmptyHost ()
  2034. {
  2035. HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
  2036. req.Host = "";
  2037. }
  2038. [Test]
  2039. public void HostAndPort ()
  2040. {
  2041. HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com:80");
  2042. Assert.AreEqual ("go-mono.com", req.Host, "#01");
  2043. req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com:9000");
  2044. Assert.AreEqual ("go-mono.com:9000", req.Host, "#02");
  2045. }
  2046. [Test]
  2047. public void PortRange ()
  2048. {
  2049. for (int i = 0; i < 65536; i++) {
  2050. if (i == 80)
  2051. continue;
  2052. string s = i.ToString ();
  2053. HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com:" + s);
  2054. Assert.AreEqual ("go-mono.com:" + s, req.Host, "#" + s);
  2055. }
  2056. }
  2057. [Test]
  2058. [ExpectedException (typeof (ArgumentException))]
  2059. public void PortBelow ()
  2060. {
  2061. HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
  2062. req.Host = "go-mono.com:-1";
  2063. }
  2064. [Test]
  2065. [ExpectedException (typeof (ArgumentException))]
  2066. public void PortAbove ()
  2067. {
  2068. HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
  2069. req.Host = "go-mono.com:65536";
  2070. }
  2071. [Test]
  2072. [ExpectedException (typeof (ArgumentException))]
  2073. public void HostTooLong ()
  2074. {
  2075. HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
  2076. string s = new string ('a', 100);
  2077. req.Host = s + "." + s + "." + s + "." + s + "." + s + "." + s; // Over 255 bytes
  2078. }
  2079. [Test]
  2080. [Category ("NotWorking")] // #5490
  2081. public void InvalidNamesThatWork ()
  2082. {
  2083. HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
  2084. req.Host = "-";
  2085. req.Host = "-.-";
  2086. req.Host = "á";
  2087. req.Host = new string ('a', 64); // Should fail. Max. is 63.
  2088. }
  2089. [Test]
  2090. public void NoDate ()
  2091. {
  2092. HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
  2093. Assert.AreEqual (DateTime.MinValue, req.Date);
  2094. }
  2095. [Test]
  2096. public void UtcDate ()
  2097. {
  2098. HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
  2099. req.Date = DateTime.UtcNow;
  2100. DateTime date = req.Date;
  2101. Assert.AreEqual (DateTimeKind.Local, date.Kind);
  2102. }
  2103. [Test]
  2104. public void AddAndRemoveDate ()
  2105. {
  2106. // Neil Armstrong set his foot on Moon
  2107. var landing = new DateTime (1969, 7, 21, 2, 56, 0, DateTimeKind.Utc);
  2108. Assert.AreEqual (621214377600000000, landing.Ticks);
  2109. var unspecified = new DateTime (1969, 7, 21, 2, 56, 0);
  2110. var local = landing.ToLocalTime ();
  2111. var req = (HttpWebRequest)WebRequest.Create ("http://www.mono-project.com/");
  2112. req.Date = landing;
  2113. Assert.AreEqual (DateTimeKind.Local, req.Date.Kind);
  2114. Assert.AreEqual (local.Ticks, req.Date.Ticks);
  2115. Assert.AreEqual (local, req.Date);
  2116. req.Date = unspecified;
  2117. Assert.AreEqual (DateTimeKind.Local, req.Date.Kind);
  2118. Assert.AreEqual (unspecified.Ticks, req.Date.Ticks);
  2119. Assert.AreEqual (unspecified, req.Date);
  2120. req.Date = local;
  2121. Assert.AreEqual (DateTimeKind.Local, req.Date.Kind);
  2122. Assert.AreEqual (local.Ticks, req.Date.Ticks);
  2123. Assert.AreEqual (local, req.Date);
  2124. req.Date = DateTime.MinValue;
  2125. Assert.AreEqual (DateTimeKind.Unspecified, DateTime.MinValue.Kind);
  2126. Assert.AreEqual (DateTimeKind.Unspecified, req.Date.Kind);
  2127. Assert.AreEqual (0, req.Date.Ticks);
  2128. Assert.AreEqual (null, req.Headers.Get ("Date"));
  2129. }
  2130. [Test]
  2131. // Bug #12393
  2132. public void TestIPv6Host ()
  2133. {
  2134. var address = "2001:0000:0000:0001:0001:0001:0157:0000";
  2135. var address2 = '[' + address + ']';
  2136. var uri = new Uri (string.Format ("http://{0}/test.css", address2));
  2137. var hwr = (HttpWebRequest)WebRequest.Create (uri);
  2138. hwr.Host = address2;
  2139. Assert.AreEqual (address2, hwr.Host, "#1");
  2140. }
  2141. [Test]
  2142. // Bug #12393
  2143. [Category ("NotWorking")]
  2144. public void TestIPv6Host2 ()
  2145. {
  2146. var address = "2001:0000:0000:0001:0001:0001:0157:0000";
  2147. var address2 = '[' + address + ']';
  2148. var uri = new Uri (string.Format ("http://{0}/test.css", address2));
  2149. var hwr = (HttpWebRequest)WebRequest.Create (uri);
  2150. try {
  2151. hwr.Host = address;
  2152. Assert.Fail ("#1");
  2153. } catch (ArgumentException) {
  2154. ;
  2155. }
  2156. }
  2157. #endif
  2158. #if NET_4_5
  2159. [Test]
  2160. public void AllowReadStreamBuffering ()
  2161. {
  2162. var hr = WebRequest.CreateHttp ("http://www.google.com");
  2163. Assert.IsFalse (hr.AllowReadStreamBuffering, "#1");
  2164. try {
  2165. hr.AllowReadStreamBuffering = true;
  2166. Assert.Fail ("#2");
  2167. } catch (InvalidOperationException) {
  2168. }
  2169. }
  2170. #endif
  2171. class ListenerScope : IDisposable {
  2172. EventWaitHandle completed;
  2173. public HttpListener listener;
  2174. Action<HttpListenerContext> processor;
  2175. public ListenerScope (Action<HttpListenerContext> processor, int port, EventWaitHandle completed)
  2176. {
  2177. this.processor = processor;
  2178. this.completed = completed;
  2179. this.listener = new HttpListener ();
  2180. this.listener.Prefixes.Add (string.Format ("http://localhost:{0}/", port));
  2181. this.listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
  2182. this.listener.Start ();
  2183. this.listener.BeginGetContext (this.RequestHandler, null);
  2184. }
  2185. void RequestHandler (IAsyncResult result)
  2186. {
  2187. HttpListenerContext context = null;
  2188. try {
  2189. context = this.listener.EndGetContext (result);
  2190. } catch (HttpListenerException ex) {
  2191. // check if the thread has been aborted as in the case when we are shutting down.
  2192. if (ex.ErrorCode == 995)
  2193. return;
  2194. } catch (ObjectDisposedException) {
  2195. return;
  2196. }
  2197. ThreadPool.QueueUserWorkItem ((o) =>
  2198. {
  2199. try {
  2200. this.processor (context);
  2201. } catch (HttpListenerException) {
  2202. }
  2203. });
  2204. this.completed.Set ();
  2205. }
  2206. public void Dispose ()
  2207. {
  2208. this.listener.Stop ();
  2209. }
  2210. }
  2211. #if !MOBILE
  2212. class SslHttpServer : HttpServer {
  2213. X509Certificate _certificate;
  2214. protected override void Run ()
  2215. {
  2216. try {
  2217. Socket client = sock.Accept ();
  2218. NetworkStream ns = new NetworkStream (client, true);
  2219. SslServerStream s = new SslServerStream (ns, Certificate, false, false);
  2220. s.PrivateKeyCertSelectionDelegate += new PrivateKeySelectionCallback (GetPrivateKey);
  2221. StreamReader reader = new StreamReader (s);
  2222. StreamWriter writer = new StreamWriter (s, Encoding.ASCII);
  2223. string line;
  2224. string hello = "<html><body><h1>Hello World!</h1></body></html>";
  2225. string answer = "HTTP/1.0 200\r\n" +
  2226. "Connection: close\r\n" +
  2227. "Content-Type: text/html\r\n" +
  2228. "Content-Encoding: " + Encoding.ASCII.WebName + "\r\n" +
  2229. "Content-Length: " + hello.Length + "\r\n" +
  2230. "\r\n" + hello;
  2231. // Read the headers
  2232. do {
  2233. line = reader.ReadLine ();
  2234. } while (line != "" && line != null && line.Length > 0);
  2235. // Now the content. We know it's 100 bytes.
  2236. // This makes BeginRead in sslclientstream block.
  2237. char [] cs = new char [100];
  2238. reader.Read (cs, 0, 100);
  2239. writer.Write (answer);
  2240. writer.Flush ();
  2241. if (evt.WaitOne (5000, false))
  2242. error = new Exception ("Timeout when stopping the server");
  2243. } catch (Exception e) {
  2244. error = e;
  2245. }
  2246. }
  2247. X509Certificate Certificate {
  2248. get {
  2249. if (_certificate == null)
  2250. _certificate = new X509Certificate (CertData.Certificate);
  2251. return _certificate;
  2252. }
  2253. }
  2254. AsymmetricAlgorithm GetPrivateKey (X509Certificate certificate, string targetHost)
  2255. {
  2256. PrivateKey key = new PrivateKey (CertData.PrivateKey, null);
  2257. return key.RSA;
  2258. }
  2259. }
  2260. class CertData {
  2261. public readonly static byte [] Certificate = {
  2262. 48, 130, 1, 191, 48, 130, 1, 40, 160, 3, 2, 1, 2, 2, 16, 36,
  2263. 14, 97, 190, 146, 132, 208, 71, 175, 6, 87, 168, 185, 175, 55, 43, 48,
  2264. 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 4, 5, 0, 48, 18,
  2265. 49, 16, 48, 14, 6, 3, 85, 4, 3, 19, 7, 103, 111, 110, 122, 97,
  2266. 108, 111, 48, 30, 23, 13, 48, 53, 48, 54, 50, 50, 49, 57, 51, 48,
  2267. 52, 54, 90, 23, 13, 51, 57, 49, 50, 51, 49, 50, 51, 53, 57, 53,
  2268. 57, 90, 48, 18, 49, 16, 48, 14, 6, 3, 85, 4, 3, 19, 7, 103,
  2269. 111, 110, 122, 97, 108, 111, 48, 129, 158, 48, 13, 6, 9, 42, 134, 72,
  2270. 134, 247, 13, 1, 1, 1, 5, 0, 3, 129, 140, 0, 48, 129, 136, 2,
  2271. 129, 129, 0, 138, 9, 38, 25, 166, 252, 59, 26, 39, 184, 128, 216, 38,
  2272. 73, 41, 86, 30, 228, 160, 205, 41, 135, 115, 223, 44, 62, 42, 198, 178,
  2273. 190, 81, 11, 25, 21, 216, 49, 179, 130, 246, 52, 97, 175, 212, 94, 157,
  2274. 231, 162, 66, 161, 103, 63, 204, 83, 141, 172, 119, 97, 225, 206, 98, 101,
  2275. 210, 106, 2, 206, 81, 90, 173, 47, 41, 199, 209, 241, 177, 177, 96, 207,
  2276. 254, 220, 190, 66, 180, 153, 0, 209, 14, 178, 69, 194, 3, 37, 116, 239,
  2277. 49, 23, 185, 245, 255, 126, 35, 85, 246, 56, 244, 107, 117, 24, 14, 57,
  2278. 9, 111, 147, 189, 220, 142, 57, 104, 153, 193, 205, 19, 14, 22, 157, 16,
  2279. 24, 80, 201, 2, 2, 0, 17, 163, 23, 48, 21, 48, 19, 6, 3, 85,
  2280. 29, 37, 4, 12, 48, 10, 6, 8, 43, 6, 1, 5, 5, 7, 3, 1,
  2281. 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 4, 5, 0, 3,
  2282. 129, 129, 0, 64, 49, 57, 253, 218, 198, 229, 51, 189, 12, 154, 225, 183,
  2283. 160, 147, 90, 113, 172, 69, 122, 28, 77, 97, 215, 231, 194, 150, 29, 196,
  2284. 65, 95, 218, 99, 142, 111, 79, 205, 109, 76, 32, 92, 220, 76, 88, 53,
  2285. 237, 80, 11, 85, 44, 91, 21, 210, 12, 34, 223, 234, 18, 187, 136, 62,
  2286. 26, 240, 103, 180, 12, 226, 221, 250, 247, 129, 51, 23, 129, 165, 56, 67,
  2287. 43, 83, 244, 110, 207, 24, 253, 195, 16, 46, 80, 113, 80, 18, 2, 254,
  2288. 120, 147, 151, 164, 23, 210, 230, 100, 19, 197, 179, 28, 194, 48, 106, 159,
  2289. 155, 144, 37, 82, 44, 160, 40, 52, 146, 174, 77, 188, 160, 230, 75, 172,
  2290. 123, 3, 254,
  2291. };
  2292. public readonly static byte [] PrivateKey = {
  2293. 30, 241, 181, 176, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0,
  2294. 0, 0, 0, 0, 84, 2, 0, 0, 7, 2, 0, 0, 0, 36, 0, 0,
  2295. 82, 83, 65, 50, 0, 4, 0, 0, 17, 0, 0, 0, 201, 80, 24, 16,
  2296. 157, 22, 14, 19, 205, 193, 153, 104, 57, 142, 220, 189, 147, 111, 9, 57,
  2297. 14, 24, 117, 107, 244, 56, 246, 85, 35, 126, 255, 245, 185, 23, 49, 239,
  2298. 116, 37, 3, 194, 69, 178, 14, 209, 0, 153, 180, 66, 190, 220, 254, 207,
  2299. 96, 177, 177, 241, 209, 199, 41, 47, 173, 90, 81, 206, 2, 106, 210, 101,
  2300. 98, 206, 225, 97, 119, 172, 141, 83, 204, 63, 103, 161, 66, 162, 231, 157,
  2301. 94, 212, 175, 97, 52, 246, 130, 179, 49, 216, 21, 25, 11, 81, 190, 178,
  2302. 198, 42, 62, 44, 223, 115, 135, 41, 205, 160, 228, 30, 86, 41, 73, 38,
  2303. 216, 128, 184, 39, 26, 59, 252, 166, 25, 38, 9, 138, 175, 88, 190, 223,
  2304. 27, 24, 224, 123, 190, 69, 164, 234, 129, 59, 108, 229, 248, 62, 187, 15,
  2305. 235, 147, 162, 83, 47, 123, 170, 190, 224, 31, 215, 110, 143, 31, 227, 216,
  2306. 85, 88, 154, 83, 207, 229, 41, 28, 237, 116, 181, 17, 37, 141, 224, 185,
  2307. 164, 144, 141, 233, 164, 138, 177, 241, 115, 181, 230, 150, 7, 92, 139, 141,
  2308. 113, 95, 57, 191, 211, 165, 217, 250, 197, 68, 164, 184, 168, 43, 48, 65,
  2309. 177, 237, 173, 144, 148, 221, 62, 189, 147, 63, 216, 188, 206, 103, 226, 171,
  2310. 32, 20, 230, 116, 144, 192, 1, 39, 202, 87, 74, 250, 6, 142, 188, 23,
  2311. 45, 4, 112, 191, 253, 67, 69, 70, 128, 143, 44, 234, 41, 96, 195, 82,
  2312. 202, 35, 158, 149, 240, 151, 23, 25, 166, 179, 85, 144, 58, 120, 149, 229,
  2313. 205, 34, 8, 110, 86, 119, 130, 210, 37, 173, 65, 71, 169, 67, 8, 51,
  2314. 20, 96, 51, 155, 3, 39, 85, 187, 40, 193, 57, 19, 99, 78, 173, 28,
  2315. 129, 154, 108, 175, 8, 138, 237, 71, 27, 148, 129, 35, 47, 57, 101, 237,
  2316. 168, 178, 227, 221, 212, 63, 124, 254, 253, 215, 183, 159, 49, 103, 74, 49,
  2317. 67, 160, 171, 72, 194, 215, 108, 251, 178, 18, 184, 100, 211, 105, 21, 186,
  2318. 39, 66, 218, 154, 72, 222, 90, 237, 179, 251, 51, 224, 212, 56, 251, 6,
  2319. 209, 151, 198, 176, 89, 110, 35, 141, 248, 237, 223, 68, 135, 206, 207, 169,
  2320. 254, 219, 243, 130, 71, 11, 94, 113, 233, 92, 63, 156, 169, 72, 215, 110,
  2321. 95, 94, 191, 50, 59, 89, 187, 59, 183, 99, 161, 146, 233, 245, 219, 80,
  2322. 87, 113, 251, 50, 144, 195, 158, 46, 189, 232, 119, 91, 75, 22, 6, 176,
  2323. 39, 206, 25, 196, 213, 195, 219, 24, 28, 103, 104, 36, 137, 128, 4, 119,
  2324. 163, 40, 126, 87, 18, 86, 128, 243, 213, 101, 2, 237, 78, 64, 160, 55,
  2325. 199, 93, 90, 126, 175, 199, 55, 89, 234, 190, 5, 16, 196, 88, 28, 208,
  2326. 28, 92, 32, 115, 204, 9, 202, 101, 15, 123, 43, 75, 90, 144, 95, 179,
  2327. 102, 249, 57, 150, 204, 99, 147, 203, 16, 63, 81, 244, 226, 237, 82, 204,
  2328. 20, 200, 140, 65, 83, 217, 161, 23, 123, 37, 115, 12, 100, 73, 70, 190,
  2329. 32, 235, 174, 140, 148, 157, 47, 238, 40, 208, 228, 80, 54, 187, 156, 252,
  2330. 253, 230, 231, 156, 138, 125, 96, 79, 3, 27, 143, 55, 146, 169, 165, 61,
  2331. 238, 60, 227, 77, 217, 93, 117, 122, 111, 46, 173, 113,
  2332. };
  2333. }
  2334. #endif
  2335. [Test]
  2336. public void CookieContainerTest ()
  2337. {
  2338. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  2339. string url = "http://" + ep.ToString ();
  2340. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (CookieRequestHandler))) {
  2341. responder.Start ();
  2342. CookieContainer container = new CookieContainer ();
  2343. container.Add(new Uri (url), new Cookie ("foo", "bar"));
  2344. HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
  2345. request.CookieContainer = container;
  2346. WebHeaderCollection headers = request.Headers;
  2347. headers.Add("Cookie", "foo=baz");
  2348. HttpWebResponse response = (HttpWebResponse) request.GetResponse ();
  2349. string responseString = null;
  2350. using (StreamReader reader = new StreamReader (response.GetResponseStream ())) {
  2351. responseString = reader.ReadToEnd ();
  2352. }
  2353. response.Close ();
  2354. Assert.AreEqual (1, response.Cookies.Count, "#01");
  2355. Assert.AreEqual ("foo=bar", response.Headers.Get("Set-Cookie"), "#02");
  2356. }
  2357. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (CookieRequestHandler))) {
  2358. responder.Start ();
  2359. CookieContainer container = new CookieContainer ();
  2360. HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
  2361. request.CookieContainer = container;
  2362. WebHeaderCollection headers = request.Headers;
  2363. headers.Add("Cookie", "foo=baz");
  2364. HttpWebResponse response = (HttpWebResponse) request.GetResponse ();
  2365. string responseString = null;
  2366. using (StreamReader reader = new StreamReader (response.GetResponseStream ())) {
  2367. responseString = reader.ReadToEnd ();
  2368. }
  2369. response.Close ();
  2370. Assert.AreEqual (0, response.Cookies.Count, "#03");
  2371. Assert.AreEqual ("", response.Headers.Get("Set-Cookie"), "#04");
  2372. }
  2373. }
  2374. internal static byte[] CookieRequestHandler (Socket socket)
  2375. {
  2376. MemoryStream ms = new MemoryStream ();
  2377. byte[] buffer = new byte[4096];
  2378. int bytesReceived = socket.Receive (buffer);
  2379. while (bytesReceived > 0) {
  2380. ms.Write(buffer, 0, bytesReceived);
  2381. // We don't check for Content-Length or anything else here, so we give the client a little time to write
  2382. // after sending the headers
  2383. Thread.Sleep(200);
  2384. if (socket.Available > 0) {
  2385. bytesReceived = socket.Receive (buffer);
  2386. } else {
  2387. bytesReceived = 0;
  2388. }
  2389. }
  2390. ms.Flush();
  2391. ms.Position = 0;
  2392. string cookies = string.Empty;
  2393. using (StreamReader sr = new StreamReader (ms, Encoding.UTF8)) {
  2394. string line;
  2395. while ((line = sr.ReadLine ()) != null) {
  2396. if (line.StartsWith ("Cookie:")) {
  2397. cookies = line.Substring ("cookie: ".Length);
  2398. }
  2399. }
  2400. }
  2401. StringWriter sw = new StringWriter ();
  2402. sw.WriteLine ("HTTP/1.1 200 OK");
  2403. sw.WriteLine ("Content-Type: text/xml");
  2404. sw.WriteLine ("Set-Cookie: " + cookies);
  2405. sw.WriteLine ("Content-Length: " + cookies.Length.ToString (CultureInfo.InvariantCulture));
  2406. sw.WriteLine ();
  2407. sw.Write (cookies);
  2408. sw.Flush ();
  2409. return Encoding.UTF8.GetBytes (sw.ToString ());
  2410. }
  2411. }
  2412. [TestFixture]
  2413. public class HttpRequestStreamTest
  2414. {
  2415. [Test]
  2416. public void BeginRead ()
  2417. {
  2418. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  2419. string url = "http://" + ep.ToString () + "/test/";
  2420. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
  2421. responder.Start ();
  2422. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  2423. req.Method = "POST";
  2424. using (Stream rs = req.GetRequestStream ()) {
  2425. byte [] buffer = new byte [10];
  2426. try {
  2427. rs.BeginRead (buffer, 0, buffer.Length, null, null);
  2428. Assert.Fail ("#1");
  2429. } catch (NotSupportedException ex) {
  2430. // The stream does not support reading
  2431. Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
  2432. Assert.IsNull (ex.InnerException, "#3");
  2433. Assert.IsNotNull (ex.Message, "#4");
  2434. } finally {
  2435. req.Abort ();
  2436. }
  2437. }
  2438. }
  2439. }
  2440. [Test]
  2441. [Category("MobileNotWorking")]
  2442. public void BeginWrite_Request_Aborted ()
  2443. {
  2444. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  2445. string url = "http://" + ep.ToString () + "/test/";
  2446. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
  2447. responder.Start ();
  2448. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  2449. req.Method = "POST";
  2450. using (Stream rs = req.GetRequestStream ()) {
  2451. req.Abort ();
  2452. try {
  2453. rs.BeginWrite (new byte [] { 0x2a, 0x2f }, 0, 2, null, null);
  2454. Assert.Fail ("#1");
  2455. } catch (WebException ex) {
  2456. // The request was aborted: The request was canceled
  2457. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  2458. Assert.IsNull (ex.InnerException, "#3");
  2459. Assert.IsNotNull (ex.Message, "#4");
  2460. Assert.IsNull (ex.Response, "#5");
  2461. Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
  2462. }
  2463. }
  2464. }
  2465. }
  2466. [Test]
  2467. public void CanRead ()
  2468. {
  2469. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  2470. string url = "http://" + ep.ToString () + "/test/";
  2471. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
  2472. responder.Start ();
  2473. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  2474. req.Method = "POST";
  2475. Stream rs = req.GetRequestStream ();
  2476. try {
  2477. Assert.IsFalse (rs.CanRead, "#1");
  2478. rs.Close ();
  2479. Assert.IsFalse (rs.CanRead, "#2");
  2480. } finally {
  2481. rs.Close ();
  2482. req.Abort ();
  2483. }
  2484. }
  2485. }
  2486. [Test]
  2487. public void CanSeek ()
  2488. {
  2489. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  2490. string url = "http://" + ep.ToString () + "/test/";
  2491. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
  2492. responder.Start ();
  2493. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  2494. req.Method = "POST";
  2495. Stream rs = req.GetRequestStream ();
  2496. try {
  2497. Assert.IsFalse (rs.CanSeek, "#1");
  2498. rs.Close ();
  2499. Assert.IsFalse (rs.CanSeek, "#2");
  2500. } finally {
  2501. rs.Close ();
  2502. req.Abort ();
  2503. }
  2504. }
  2505. }
  2506. [Test] // bug #324182
  2507. public void CanTimeout ()
  2508. {
  2509. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  2510. string url = "http://" + ep.ToString () + "/test/";
  2511. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
  2512. responder.Start ();
  2513. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  2514. req.Method = "POST";
  2515. Stream rs = req.GetRequestStream ();
  2516. try {
  2517. Assert.IsTrue (rs.CanTimeout, "#1");
  2518. rs.Close ();
  2519. Assert.IsTrue (rs.CanTimeout, "#2");
  2520. } finally {
  2521. rs.Close ();
  2522. req.Abort ();
  2523. }
  2524. }
  2525. }
  2526. [Test]
  2527. public void CanWrite ()
  2528. {
  2529. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  2530. string url = "http://" + ep.ToString () + "/test/";
  2531. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
  2532. responder.Start ();
  2533. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  2534. req.Method = "POST";
  2535. Stream rs = req.GetRequestStream ();
  2536. try {
  2537. Assert.IsTrue (rs.CanWrite, "#1");
  2538. rs.Close ();
  2539. Assert.IsFalse (rs.CanWrite, "#2");
  2540. } finally {
  2541. rs.Close ();
  2542. req.Abort ();
  2543. }
  2544. }
  2545. }
  2546. [Test]
  2547. public void Read ()
  2548. {
  2549. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  2550. string url = "http://" + ep.ToString () + "/test/";
  2551. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
  2552. responder.Start ();
  2553. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  2554. req.Method = "POST";
  2555. using (Stream rs = req.GetRequestStream ()) {
  2556. byte [] buffer = new byte [10];
  2557. try {
  2558. rs.Read (buffer, 0, buffer.Length);
  2559. Assert.Fail ("#1");
  2560. } catch (NotSupportedException ex) {
  2561. // The stream does not support reading
  2562. Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
  2563. Assert.IsNull (ex.InnerException, "#3");
  2564. Assert.IsNotNull (ex.Message, "#4");
  2565. } finally {
  2566. req.Abort ();
  2567. }
  2568. }
  2569. }
  2570. }
  2571. [Test]
  2572. public void ReadByte ()
  2573. {
  2574. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  2575. string url = "http://" + ep.ToString () + "/test/";
  2576. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
  2577. responder.Start ();
  2578. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  2579. req.Method = "POST";
  2580. using (Stream rs = req.GetRequestStream ()) {
  2581. try {
  2582. rs.ReadByte ();
  2583. Assert.Fail ("#1");
  2584. } catch (NotSupportedException ex) {
  2585. // The stream does not support reading
  2586. Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
  2587. Assert.IsNull (ex.InnerException, "#3");
  2588. Assert.IsNotNull (ex.Message, "#4");
  2589. } finally {
  2590. req.Abort ();
  2591. }
  2592. }
  2593. }
  2594. }
  2595. [Test]
  2596. public void ReadTimeout ()
  2597. {
  2598. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  2599. string url = "http://" + ep.ToString () + "/test/";
  2600. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
  2601. responder.Start ();
  2602. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  2603. req.Method = "POST";
  2604. Stream rs = req.GetRequestStream ();
  2605. try {
  2606. Assert.AreEqual (300000, rs.ReadTimeout, "#1");
  2607. rs.Close ();
  2608. Assert.AreEqual (300000, rs.ReadTimeout, "#2");
  2609. } finally {
  2610. rs.Close ();
  2611. req.Abort ();
  2612. }
  2613. }
  2614. }
  2615. [Test]
  2616. public void Seek ()
  2617. {
  2618. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  2619. string url = "http://" + ep.ToString () + "/test/";
  2620. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
  2621. responder.Start ();
  2622. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  2623. req.Method = "POST";
  2624. using (Stream rs = req.GetRequestStream ()) {
  2625. try {
  2626. rs.Seek (0, SeekOrigin.Current);
  2627. Assert.Fail ("#1");
  2628. } catch (NotSupportedException ex) {
  2629. // This stream does not support seek operations
  2630. Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
  2631. Assert.IsNull (ex.InnerException, "#3");
  2632. Assert.IsNotNull (ex.Message, "#4");
  2633. } finally {
  2634. req.Abort ();
  2635. }
  2636. }
  2637. }
  2638. }
  2639. [Test]
  2640. public void Write_Buffer_Null ()
  2641. {
  2642. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  2643. string url = "http://" + ep.ToString () + "/test/";
  2644. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
  2645. responder.Start ();
  2646. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  2647. req.Method = "POST";
  2648. using (Stream rs = req.GetRequestStream ()) {
  2649. try {
  2650. rs.Write ((byte []) null, -1, -1);
  2651. Assert.Fail ("#1");
  2652. } catch (ArgumentNullException ex) {
  2653. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2654. Assert.IsNull (ex.InnerException, "#3");
  2655. Assert.IsNotNull (ex.Message, "#4");
  2656. Assert.AreEqual ("buffer", ex.ParamName, "#5");
  2657. }
  2658. }
  2659. req.Abort ();
  2660. }
  2661. }
  2662. [Test]
  2663. public void Write_Count_Negative ()
  2664. {
  2665. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  2666. string url = "http://" + ep.ToString () + "/test/";
  2667. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
  2668. responder.Start ();
  2669. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  2670. req.Method = "POST";
  2671. using (Stream rs = req.GetRequestStream ()) {
  2672. byte [] buffer = new byte [] { 0x2a, 0x2c, 0x1d, 0x00, 0x0f };
  2673. try {
  2674. rs.Write (buffer, 1, -1);
  2675. Assert.Fail ("#1");
  2676. } catch (ArgumentOutOfRangeException ex) {
  2677. // Specified argument was out of the range of valid values
  2678. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  2679. Assert.IsNull (ex.InnerException, "#A3");
  2680. Assert.IsNotNull (ex.Message, "#A4");
  2681. Assert.AreEqual ("size", ex.ParamName, "#A5");
  2682. }
  2683. }
  2684. req.Abort ();
  2685. }
  2686. }
  2687. [Test]
  2688. public void Write_Count_Overflow ()
  2689. {
  2690. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  2691. string url = "http://" + ep.ToString () + "/test/";
  2692. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
  2693. responder.Start ();
  2694. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  2695. req.Method = "POST";
  2696. using (Stream rs = req.GetRequestStream ()) {
  2697. byte [] buffer = new byte [] { 0x2a, 0x2c, 0x1d, 0x00, 0x0f };
  2698. try {
  2699. rs.Write (buffer, buffer.Length - 2, 3);
  2700. Assert.Fail ("#1");
  2701. } catch (ArgumentOutOfRangeException ex) {
  2702. // Specified argument was out of the range of valid values
  2703. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
  2704. Assert.IsNull (ex.InnerException, "#3");
  2705. Assert.IsNotNull (ex.Message, "#4");
  2706. Assert.AreEqual ("size", ex.ParamName, "#5");
  2707. }
  2708. }
  2709. req.Abort ();
  2710. }
  2711. }
  2712. [Test]
  2713. public void Write_Offset_Negative ()
  2714. {
  2715. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  2716. string url = "http://" + ep.ToString () + "/test/";
  2717. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
  2718. responder.Start ();
  2719. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  2720. req.Method = "POST";
  2721. using (Stream rs = req.GetRequestStream ()) {
  2722. byte [] buffer = new byte [] { 0x2a, 0x2c, 0x1d, 0x00, 0x0f };
  2723. try {
  2724. rs.Write (buffer, -1, 0);
  2725. Assert.Fail ("#1");
  2726. } catch (ArgumentOutOfRangeException ex) {
  2727. // Specified argument was out of the range of valid values
  2728. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
  2729. Assert.IsNull (ex.InnerException, "#3");
  2730. Assert.IsNotNull (ex.Message, "#4");
  2731. Assert.AreEqual ("offset", ex.ParamName, "#5");
  2732. }
  2733. }
  2734. req.Abort ();
  2735. }
  2736. }
  2737. [Test]
  2738. public void Write_Offset_Overflow ()
  2739. {
  2740. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  2741. string url = "http://" + ep.ToString () + "/test/";
  2742. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
  2743. responder.Start ();
  2744. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  2745. req.Method = "POST";
  2746. using (Stream rs = req.GetRequestStream ()) {
  2747. byte [] buffer = new byte [] { 0x2a, 0x2c, 0x1d, 0x00, 0x0f };
  2748. try {
  2749. rs.Write (buffer, buffer.Length + 1, 0);
  2750. Assert.Fail ("#1");
  2751. } catch (ArgumentOutOfRangeException ex) {
  2752. // Specified argument was out of the range of valid values
  2753. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
  2754. Assert.IsNull (ex.InnerException, "#3");
  2755. Assert.IsNotNull (ex.Message, "#4");
  2756. Assert.AreEqual ("offset", ex.ParamName, "#5");
  2757. }
  2758. }
  2759. req.Abort ();
  2760. }
  2761. }
  2762. [Test]
  2763. public void Write_Request_Aborted ()
  2764. {
  2765. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  2766. string url = "http://" + ep.ToString () + "/test/";
  2767. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
  2768. responder.Start ();
  2769. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  2770. req.Method = "POST";
  2771. using (Stream rs = req.GetRequestStream ()) {
  2772. req.Abort ();
  2773. try {
  2774. rs.Write (new byte [0], 0, 0);
  2775. Assert.Fail ("#1");
  2776. } catch (WebException ex) {
  2777. // The request was aborted: The request was canceled
  2778. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  2779. Assert.IsNull (ex.InnerException, "#3");
  2780. Assert.IsNotNull (ex.Message, "#4");
  2781. Assert.IsNull (ex.Response, "#5");
  2782. Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
  2783. }
  2784. }
  2785. }
  2786. }
  2787. [Test]
  2788. [Category ("NotWorking")]
  2789. public void Write_Stream_Closed ()
  2790. {
  2791. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  2792. string url = "http://" + ep.ToString () + "/test/";
  2793. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
  2794. responder.Start ();
  2795. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  2796. req.Method = "POST";
  2797. using (Stream rs = req.GetRequestStream ()) {
  2798. rs.Close ();
  2799. try {
  2800. rs.Write (new byte [0], 0, 0);
  2801. Assert.Fail ("#1");
  2802. } catch (WebException ex) {
  2803. // The request was aborted: The connection was closed unexpectedly
  2804. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  2805. Assert.IsNull (ex.InnerException, "#3");
  2806. Assert.IsNotNull (ex.Message, "#4");
  2807. Assert.IsNull (ex.Response, "#5");
  2808. Assert.AreEqual (WebExceptionStatus.ConnectionClosed, ex.Status, "#6");
  2809. }
  2810. }
  2811. }
  2812. }
  2813. [Test]
  2814. public void WriteByte_Request_Aborted ()
  2815. {
  2816. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  2817. string url = "http://" + ep.ToString () + "/test/";
  2818. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
  2819. responder.Start ();
  2820. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  2821. req.Method = "POST";
  2822. using (Stream rs = req.GetRequestStream ()) {
  2823. req.Abort ();
  2824. try {
  2825. rs.WriteByte (0x2a);
  2826. Assert.Fail ("#1");
  2827. } catch (WebException ex) {
  2828. // The request was aborted: The request was canceled
  2829. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  2830. Assert.IsNull (ex.InnerException, "#3");
  2831. Assert.IsNotNull (ex.Message, "#4");
  2832. Assert.IsNull (ex.Response, "#5");
  2833. Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
  2834. }
  2835. }
  2836. }
  2837. }
  2838. [Test]
  2839. public void WriteTimeout ()
  2840. {
  2841. IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
  2842. string url = "http://" + ep.ToString () + "/test/";
  2843. using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
  2844. responder.Start ();
  2845. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  2846. req.Method = "POST";
  2847. Stream rs = req.GetRequestStream ();
  2848. try {
  2849. Assert.AreEqual (300000, rs.WriteTimeout, "#1");
  2850. rs.Close ();
  2851. Assert.AreEqual (300000, rs.WriteTimeout, "#2");
  2852. } finally {
  2853. rs.Close ();
  2854. req.Abort ();
  2855. }
  2856. }
  2857. }
  2858. #if NET_4_0
  2859. [Test]
  2860. // Bug6737
  2861. // This test is supposed to fail prior to .NET 4.0
  2862. public void Post_EmptyRequestStream ()
  2863. {
  2864. var wr = HttpWebRequest.Create ("http://google.com");
  2865. wr.Method = "POST";
  2866. wr.GetRequestStream ();
  2867. var gr = wr.BeginGetResponse (delegate { }, null);
  2868. Assert.AreEqual (true, gr.AsyncWaitHandle.WaitOne (5000), "#1");
  2869. }
  2870. #endif
  2871. }
  2872. static class StreamExtensions {
  2873. public static int ReadAll(this Stream stream, byte[] buffer, int offset, int count)
  2874. {
  2875. int totalRead = 0;
  2876. while (totalRead < count) {
  2877. int bytesRead = stream.Read (buffer, offset + totalRead, count - totalRead);
  2878. if (bytesRead == 0)
  2879. break;
  2880. totalRead += bytesRead;
  2881. }
  2882. return totalRead;
  2883. }
  2884. }
  2885. static class ExceptionAssert {
  2886. /// <summary>
  2887. /// Asserts that the function throws an exception.
  2888. /// </summary>
  2889. /// <param name="f">A function execute that is expected to raise an exception.</param>
  2890. /// <typeparam name="T">The type of exception that is expected.</typeparam>
  2891. /// <returns>The exception thrown.</returns>
  2892. /// <exception cref="AssertFailedException">If the function does not throw an exception
  2893. /// or throws a different exception.</exception>
  2894. /// <example><![CDATA[
  2895. /// ExceptionAssert.Throws(typeof(ArgumentNullException), delegate {
  2896. /// myObject.myFunction(null); });
  2897. /// ]]></example>
  2898. public static T Throws<T> (Action f) where T : Exception {
  2899. Exception actualException = null;
  2900. try {
  2901. f ();
  2902. } catch (Exception ex) {
  2903. actualException = ex;
  2904. }
  2905. if (actualException == null)
  2906. throw new AssertionException (string.Format (
  2907. "No exception thrown. Expected '{0}'",
  2908. typeof (T).FullName));
  2909. else if (typeof(T) != actualException.GetType())
  2910. throw new AssertionException (string.Format (
  2911. "Caught exception of type '{0}'. Expected '{1}':{2}",
  2912. actualException.GetType().FullName,
  2913. typeof (T).FullName,
  2914. Environment.NewLine + actualException));
  2915. return (T) actualException;
  2916. }
  2917. }
  2918. }