// // HttpWebRequestTest.cs - NUnit Test Cases for System.Net.HttpWebRequest // // Authors: // Lawrence Pit (loz@cable.a2000.nl) // Martin Willemoes Hansen (mwh@sysrq.dk) // Gonzalo Paniagua Javier (gonzalo@ximian.com) // // (C) 2003 Martin Willemoes Hansen // Copyright (c) 2005 Novell, Inc. (http://www.novell.com // using NUnit.Framework; using System; using System.Collections; using System.IO; using System.Net; using System.Net.Sockets; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading; using Mono.Security.Authenticode; using Mono.Security.Protocol.Tls; namespace MonoTests.System.Net { [TestFixture] public class HttpWebRequestTest { [Test] [Category("InetAccess")] public void Sync () { HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com"); Assertion.AssertNotNull ("req:If Modified Since: ", req.IfModifiedSince); req.UserAgent = "MonoClient v1.0"; Assertion.AssertEquals ("req Header 1", "User-Agent", req.Headers.GetKey (0)); Assertion.AssertEquals ("req Header 2", "MonoClient v1.0", req.Headers.Get (0)); HttpWebResponse res = (HttpWebResponse) req.GetResponse (); Assertion.AssertEquals ("res:HttpStatusCode: ", "OK", res.StatusCode.ToString ()); Assertion.AssertEquals ("res:HttpStatusDescription: ", "OK", res.StatusDescription); Assertion.AssertEquals ("res Header 1", "text/html", res.Headers.Get ("Content-Type")); Assertion.AssertNotNull ("Last Modified: ", res.LastModified); Assertion.AssertEquals ("res:", 0, res.Cookies.Count); res.Close (); } [Test] public void AddRange () { HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com"); req.AddRange (10); req.AddRange (50, 90); req.AddRange ("bytes", 100); req.AddRange ("bytes", 100, 120); Assertion.AssertEquals ("#1", "bytes=10-,50-90,100-,100-120", req.Headers ["Range"]); try { req.AddRange ("bits", 2000); Assertion.Fail ("#2"); } catch (InvalidOperationException) {} } [Test] [Category("InetAccess")] public void Cookies1 () { // The purpose of this test is to ensure that the cookies we get from a request // are stored in both, the CookieCollection in HttpWebResponse and the CookieContainer // in HttpWebRequest. // If this URL stops sending *one* and only one cookie, replace it. string url = "http://www.elmundo.es"; CookieContainer cookies = new CookieContainer (); HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url); req.KeepAlive = false; req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv; 1.7.6) Gecko/20050317 Firefox/1.0.2"; req.CookieContainer = cookies; Assertion.AssertEquals ("#01", 0, cookies.Count); using (HttpWebResponse res = (HttpWebResponse) req.GetResponse()) { CookieCollection coll = req.CookieContainer.GetCookies (new Uri (url)); Assertion.AssertEquals ("#02", 1, coll.Count); Assertion.AssertEquals ("#03", 1, res.Cookies.Count); Cookie one = coll [0]; Cookie two = res.Cookies [0]; Assertion.AssertEquals ("#04", true, object.ReferenceEquals (one, two)); } } [Test] public void SslClientBlock () { // This tests that the write request/initread/write body sequence does not hang // when using SSL. // If there's a regression for this, the test will hang. ServicePointManager.CertificatePolicy = new AcceptAllPolicy (); try { SslHttpServer server = new SslHttpServer (); server.Start (); string url = String.Format ("https://{0}:{1}/nothing.html", server.IPAddress, server.Port); HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url); request.Method = "POST"; Stream stream = request.GetRequestStream (); byte [] bytes = new byte [100]; stream.Write (bytes, 0, bytes.Length); stream.Close (); HttpWebResponse resp = (HttpWebResponse) request.GetResponse (); Assertion.AssertEquals ("StatusCode", 200, (int) resp.StatusCode); StreamReader sr = new StreamReader (resp.GetResponseStream (), Encoding.UTF8); string x = sr.ReadToEnd (); sr.Close (); resp.Close (); server.Stop (); if (server.Error != null) throw server.Error; } finally { ServicePointManager.CertificatePolicy = null; } } [Test] public void Missing_ContentEncoding () { ServicePointManager.CertificatePolicy = new AcceptAllPolicy (); try { BadChunkedServer server = new BadChunkedServer (); server.Start (); string url = String.Format ("http://{0}:{1}/nothing.html", server.IPAddress, server.Port); HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url); request.Method = "GET"; HttpWebResponse resp = (HttpWebResponse) request.GetResponse (); Assert.AreEqual ("", resp.ContentEncoding); resp.Close (); server.Stop (); if (server.Error != null) throw server.Error; } finally { ServicePointManager.CertificatePolicy = null; } } [Test] public void BadServer_ChunkedClose () { // The server will send a chunked response without a 'last-chunked' mark // and then shutdown the socket for sending. BadChunkedServer server = new BadChunkedServer (); server.Start (); string url = String.Format ("http://{0}:{1}/nothing.html", server.IPAddress, server.Port); HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url); HttpWebResponse resp = (HttpWebResponse) request.GetResponse (); string x = null; try { byte [] bytes = new byte [32]; // Using StreamReader+UTF8Encoding here fails on MS runtime Stream stream = resp.GetResponseStream (); int nread = stream.Read (bytes, 0, 32); Assertion.AssertEquals ("#01", 16, nread); x = Encoding.ASCII.GetString (bytes, 0, 16); } finally { resp.Close (); server.Stop (); } if (server.Error != null) throw server.Error; Assertion.AssertEquals ("1234567890123456", x); } class BadChunkedServer : HttpServer { protected override void Run () { Socket client = sock.Accept (); NetworkStream ns = new NetworkStream (client, true); StreamWriter writer = new StreamWriter (ns, Encoding.ASCII); writer.Write ( "HTTP/1.1 200 OK\r\n" + "Transfer-Encoding: chunked\r\n" + "Connection: close\r\n" + "Content-Type: text/plain; charset=UTF-8\r\n\r\n"); // This body lacks a 'last-chunk' (see RFC 2616) writer.Write ("10\r\n1234567890123456\r\n"); writer.Flush (); client.Shutdown (SocketShutdown.Send); Thread.Sleep (1000); writer.Close (); } } class AcceptAllPolicy : ICertificatePolicy { public bool CheckValidationResult (ServicePoint sp, X509Certificate certificate, WebRequest request, int error) { return true; } } abstract class HttpServer { protected Socket sock; protected Exception error; protected ManualResetEvent evt; public HttpServer () { sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); sock.Bind (new IPEndPoint (IPAddress.Loopback, 0)); sock.Listen (1); } public void Start () { evt = new ManualResetEvent (false); Thread th = new Thread (new ThreadStart (Run)); th.Start (); } public void Stop () { evt.Set (); sock.Close (); } public IPAddress IPAddress { get { return ((IPEndPoint) sock.LocalEndPoint).Address; } } public int Port { get { return ((IPEndPoint) sock.LocalEndPoint).Port; } } public Exception Error { get { return error; } } protected abstract void Run (); } class SslHttpServer : HttpServer { X509Certificate _certificate; protected override void Run () { try { Socket client = sock.Accept (); NetworkStream ns = new NetworkStream (client, true); SslServerStream s = new SslServerStream (ns, Certificate, false, false); s.PrivateKeyCertSelectionDelegate += new PrivateKeySelectionCallback (GetPrivateKey); StreamReader reader = new StreamReader (s); StreamWriter writer = new StreamWriter (s, Encoding.ASCII); string line; string hello = "