[LoongArch64] Part-5:add loongarch support in some files for LoongArch64. (#21769)
[mono-project.git] / mcs / class / System / Test / System.Net / WebRequestTest.cs
blob891c2d22ff43cc42348ff728ab406ddf1fa46208
1 //
2 // WebRequestTest.cs - NUnit Test Cases for System.Net.WebRequest
3 //
4 // Authors:
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Martin Willemoes Hansen (mwh@sysrq.dk)
7 // Sebastien Pouliot <sebastien@ximian.com>
8 //
9 // (C) 2003 Martin Willemoes Hansen
10 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
13 using NUnit.Framework;
14 using MonoTests.Helpers;
15 using System;
16 using System.Net;
17 using System.Threading;
18 using System.Collections;
19 using System.Runtime.Serialization;
20 using Socks = System.Net.Sockets;
23 namespace MonoTests.System.Net {
25 // WebRequest is abstract
27 public class NonAbstractWebRequest : WebRequest
30 public NonAbstractWebRequest ()
34 public NonAbstractWebRequest (SerializationInfo si, StreamingContext sc)
35 : base (si, sc)
40 [TestFixture]
41 public class WebRequestTest {
43 private void Callback (IAsyncResult ar)
45 Assert.Fail ("Callback");
48 [Test]
49 public void SerializationConstructor ()
51 NonAbstractWebRequest w = new NonAbstractWebRequest (null, new StreamingContext ());
52 Assert.IsNotNull (w);
55 // properties (only test 'get'ter)
57 [Test]
58 [ExpectedException (typeof (NotImplementedException))]
59 public void ConnectionGroupName ()
61 NonAbstractWebRequest w = new NonAbstractWebRequest ();
62 Assert.IsNull (w.ConnectionGroupName);
65 [Test]
66 [ExpectedException (typeof (NotImplementedException))]
67 public void ContentLength ()
69 NonAbstractWebRequest w = new NonAbstractWebRequest ();
70 Assert.IsNull (w.ContentLength);
73 [Test]
74 [ExpectedException (typeof (NotImplementedException))]
75 public void ContentType ()
77 NonAbstractWebRequest w = new NonAbstractWebRequest ();
78 Assert.IsNull (w.ContentType);
81 [Test]
82 [ExpectedException (typeof (NotImplementedException))]
83 public void Credentials ()
85 NonAbstractWebRequest w = new NonAbstractWebRequest ();
86 Assert.IsNull (w.Credentials);
89 [Test]
90 [ExpectedException (typeof (NotImplementedException))]
91 public void Headers ()
93 NonAbstractWebRequest w = new NonAbstractWebRequest ();
94 Assert.IsNull (w.Headers);
97 [Test]
98 [ExpectedException (typeof (NotImplementedException))]
99 public void Method ()
101 NonAbstractWebRequest w = new NonAbstractWebRequest ();
102 Assert.IsNull (w.Method);
105 [Test]
106 [ExpectedException (typeof (NotImplementedException))]
107 public void PreAuthenticate ()
109 NonAbstractWebRequest w = new NonAbstractWebRequest ();
110 Assert.IsTrue (w.PreAuthenticate);
113 [Test]
114 [ExpectedException (typeof (NotImplementedException))]
115 public void Proxy ()
117 NonAbstractWebRequest w = new NonAbstractWebRequest ();
118 Assert.IsNull (w.Proxy);
121 [Test]
122 [ExpectedException (typeof (NotImplementedException))]
123 public void RequestUri ()
125 NonAbstractWebRequest w = new NonAbstractWebRequest ();
126 Assert.IsNull (w.RequestUri);
129 [Test]
130 [ExpectedException (typeof (NotImplementedException))]
131 public void Timeout ()
133 NonAbstractWebRequest w = new NonAbstractWebRequest ();
134 Assert.IsNull (w.Timeout);
137 // methods
139 [Test]
140 [ExpectedException (typeof (NotImplementedException))]
141 public void Abort ()
143 NonAbstractWebRequest w = new NonAbstractWebRequest ();
144 w.Abort ();
147 [Test]
148 [ExpectedException (typeof (NotImplementedException))]
149 public void BeginGetRequestStream ()
151 NonAbstractWebRequest w = new NonAbstractWebRequest ();
152 IAsyncResult r = w.BeginGetRequestStream (new AsyncCallback (Callback), w);
155 [Test]
156 [ExpectedException (typeof (NotImplementedException))]
157 public void BeginGetResponse ()
159 NonAbstractWebRequest w = new NonAbstractWebRequest ();
160 IAsyncResult r = w.BeginGetResponse (new AsyncCallback (Callback), w);
163 [Test]
164 [ExpectedException (typeof (NotImplementedException))]
165 public void EndGetRequestStream ()
167 NonAbstractWebRequest w = new NonAbstractWebRequest ();
168 w.EndGetRequestStream (null);
171 [Test]
172 [ExpectedException (typeof (NotImplementedException))]
173 public void EndGetResponse ()
175 NonAbstractWebRequest w = new NonAbstractWebRequest ();
176 w.EndGetResponse (null);
179 [Test]
180 [ExpectedException (typeof (NotImplementedException))]
181 public void GetRequestStream ()
183 NonAbstractWebRequest w = new NonAbstractWebRequest ();
184 w.GetRequestStream ();
187 [Test]
188 [ExpectedException (typeof (NotImplementedException))]
189 public void GetResponse ()
191 NonAbstractWebRequest w = new NonAbstractWebRequest ();
192 w.GetResponse ();
195 [Test]
196 #if FEATURE_NO_BSD_SOCKETS
197 [ExpectedException (typeof (PlatformNotSupportedException))]
198 #endif
199 public void All ()
201 WebRequest req = WebRequest.Create ("http://www.contoso.com");
202 Assert.IsTrue (req is HttpWebRequest, "#1");
203 req = WebRequest.Create ("https://www.contoso.com");
204 Assert.IsTrue (req is HttpWebRequest, "#2");
205 req = WebRequest.Create ("file://www.contoso.com");
206 Assert.IsTrue (req is FileWebRequest, "#3");
207 req = WebRequest.Create ("ftp://www.contoso.com");
208 Assert.IsTrue (req is FtpWebRequest, "#4");
209 WebRequest.RegisterPrefix ("http://www.contoso.com", new TestWebRequestCreator ());
210 bool ret = WebRequest.RegisterPrefix ("http://WWW.contoso.com", new TestWebRequestCreator ());
211 Assert.AreEqual (false, ret, "#5a");
212 ret = WebRequest.RegisterPrefix ("http://www.contoso.com/foo/bar", new TestWebRequestCreator2 ());
213 Assert.AreEqual (true, ret, "#5b");
214 ret = WebRequest.RegisterPrefix ("http://www", new TestWebRequestCreator3 ());
215 Assert.AreEqual (true, ret, "#5c");
217 req = WebRequest.Create ("http://WWW.contoso.com");
218 Assert.IsTrue (req is TestWebRequest, "#6");
220 req = WebRequest.Create ("http://WWW.contoso.com/foo/bar/index.html");
221 Assert.IsTrue (req is TestWebRequest2, "#7");
223 req = WebRequest.Create ("http://WWW.x.com");
224 Assert.IsTrue (req is TestWebRequest3, "#8");
226 req = WebRequest.Create ("http://WWW.c");
227 Assert.IsTrue (req is TestWebRequest3, "#9");
229 req = WebRequest.CreateDefault (new Uri("http://WWW.contoso.com"));
230 Assert.IsTrue (req is HttpWebRequest, "#10");
232 try {
233 req = WebRequest.Create ("tcp://www.contoso.com");
234 Assert.Fail ("#11 should have failed with NotSupportedException");
235 } catch (NotSupportedException) {
239 [Test]
240 public void Create_RequestUriString_Null ()
242 try {
243 WebRequest.Create ((string) null);
244 Assert.Fail ("#1");
245 } catch (ArgumentNullException ex) {
246 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
247 Assert.IsNull (ex.InnerException, "#3");
248 Assert.IsNotNull (ex.Message, "#4");
249 Assert.IsNotNull (ex.ParamName, "#5");
250 Assert.AreEqual ("requestUriString", ex.ParamName, "#6");
254 [Test]
255 public void CreateDefault_RequestUri_Null ()
257 try {
258 WebRequest.CreateDefault ((Uri) null);
259 Assert.Fail ("#1");
260 } catch (ArgumentNullException ex) {
261 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
262 Assert.IsNull (ex.InnerException, "#3");
263 Assert.IsNotNull (ex.Message, "#4");
264 Assert.IsNotNull (ex.ParamName, "#5");
265 Assert.AreEqual ("requestUri", ex.ParamName, "#6");
269 [Test]
270 #if FEATURE_NO_BSD_SOCKETS && !WASM
271 [ExpectedException (typeof (PlatformNotSupportedException))]
272 #endif
273 public void DefaultWebProxy ()
275 WebProxy proxy = new WebProxy ("proxy.intern.com", 83);
277 WebRequest.DefaultWebProxy = proxy;
278 Assert.IsNotNull (WebRequest.DefaultWebProxy, "#A1");
279 Assert.AreSame (proxy, WebRequest.DefaultWebProxy, "#A2");
281 HttpWebRequest req = (HttpWebRequest) WebRequest.CreateDefault (
282 new Uri ("http://www.example.com"));
283 Assert.IsNotNull (req.Proxy, "#B1");
284 Assert.AreSame (proxy, req.Proxy, "#B2");
286 WebRequest.DefaultWebProxy = null;
287 Assert.IsNull (WebRequest.DefaultWebProxy, "#C1");
288 Assert.IsNotNull (req.Proxy, "#C2");
289 Assert.AreSame (proxy, req.Proxy, "#C3");
291 req = (HttpWebRequest) WebRequest.CreateDefault (
292 new Uri ("http://www.example.com"));
293 Assert.IsNull (req.Proxy, "#D");
296 [Test]
297 public void RegisterPrefix_Creator_Null ()
299 try {
300 WebRequest.RegisterPrefix ("http://www.example.com", (IWebRequestCreate) null);
301 Assert.Fail ("#1");
302 } catch (ArgumentNullException ex) {
303 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
304 Assert.IsNull (ex.InnerException, "#3");
305 Assert.IsNotNull (ex.Message, "#4");
306 Assert.IsNotNull (ex.ParamName, "#5");
307 Assert.AreEqual ("creator", ex.ParamName, "#6");
311 [Test]
312 public void RegisterPrefix_Prefix_Null ()
314 try {
315 WebRequest.RegisterPrefix ((string) null, new TestWebRequestCreator ());
316 Assert.Fail ("#1");
317 } catch (ArgumentNullException ex) {
318 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
319 Assert.IsNull (ex.InnerException, "#3");
320 Assert.IsNotNull (ex.Message, "#4");
321 Assert.IsNotNull (ex.ParamName, "#5");
322 Assert.AreEqual ("prefix", ex.ParamName, "#6");
326 [Test] //BNC#323452
327 // Throws exception with Status == Timeout. The same code behaves as the test expects when run from a regular app.
328 // Might be an issue with the test suite. To investigate.
329 [Category("AndroidNotWorking")]
330 [Category("MultiThreaded")]
331 public void TestFailedConnection ()
333 try {
334 WebRequest.Create ("http://127.0.0.1:0/non-existant.txt").GetResponse ();
335 Assert.Fail ("Should have raised an exception");
336 #if FEATURE_NO_BSD_SOCKETS
337 } catch (PlatformNotSupportedException) {
338 // Expected
339 #endif
340 } catch (Exception e) {
341 Assert.IsTrue (e is WebException, "Got " + e.GetType ().Name + ": " + e.Message);
342 //#if NET_2_0 e.Message == "Unable to connect to the remote server"
343 //#if NET_1_1 e.Message == "The underlying connection was closed: Unable to connect to the remote server."
345 Assert.AreEqual (WebExceptionStatus.ConnectFailure, ((WebException)e).Status);
347 //#if !NET_1_1 (this is not true in .NET 1.x)
348 Assert.IsNotNull (e.InnerException);
349 Assert.IsTrue (e.InnerException is Socks.SocketException, "InnerException should be SocketException");
350 //e.Message == "The requested address is not valid in its context 127.0.0.1:0"
351 //#endif
355 [Test] //BNC#323452
356 [Category ("AndroidNotWorking")] // Fails when ran as part of the entire BCL test suite. Works when only this fixture is ran
357 [Category ("MultiThreaded")]
358 public void TestFailedResolution ()
360 try {
361 var req = WebRequest.Create ("http://thisdomaindoesnotexist.monotestcase.x/non-existant.txt").GetResponse ();
363 * Work around broken t-online.de DNS Server.
365 * T-Online's DNS Server for DSL Customers resolves
366 * non-exisitng domain names to
367 * http://navigationshilfe1.t-online.de/dnserror?url=....
368 * instead of reporting an error.
370 if (req.ResponseUri.DnsSafeHost.Equals ("navigationshilfe1.t-online.de"))
371 Assert.Ignore ("Misbehaving DNS server.");
373 Assert.Fail ("Should have raised an exception");
374 #if FEATURE_NO_BSD_SOCKETS
375 } catch (PlatformNotSupportedException) {
376 // Expected
377 #endif
378 } catch (Exception e) {
379 Assert.IsTrue (e is WebException);
380 //#if NET_2_0 e.Message == "The underlying connection was closed: The remote name could not be resolved."
381 //#if NET_1_1 e.Message == "The remote name could not be resolved: 'thisdomaindoesnotexist.monotestcase.x'"
382 Assert.AreEqual (WebExceptionStatus.NameResolutionFailure, ((WebException)e).Status);
383 Assert.IsNull (e.InnerException);
387 internal class TestWebRequestCreator : IWebRequestCreate
389 internal TestWebRequestCreator () { }
391 public WebRequest Create (Uri uri)
393 return new TestWebRequest ();
397 internal class TestWebRequest : WebRequest
399 internal TestWebRequest () { }
402 internal class TestWebRequestCreator2 : IWebRequestCreate
404 internal TestWebRequestCreator2 () { }
406 public WebRequest Create (Uri uri)
408 return new TestWebRequest2 ();
412 internal class TestWebRequest2 : WebRequest
414 internal TestWebRequest2 () { }
417 internal class TestWebRequestCreator3 : IWebRequestCreate
419 internal TestWebRequestCreator3 () { }
421 public WebRequest Create (Uri uri)
423 return new TestWebRequest3 ();
427 internal class TestWebRequest3 : WebRequest
429 internal TestWebRequest3 () { }
432 [Test] // Covers #41477
433 #if FEATURE_NO_BSD_SOCKETS
434 [ExpectedException (typeof (PlatformNotSupportedException))]
435 #endif
436 public void TestReceiveCancelation ()
438 HttpListener listener = NetworkHelpers.CreateAndStartHttpListener ("http://localhost:", out var port, "/", out var uri);
440 try {
441 for (var i = 0; i < 10; i++) {
442 var request = WebRequest.CreateHttp (uri);
443 request.Method = "GET";
445 var tokenSource = new CancellationTokenSource ();
446 tokenSource.Token.Register(() => request.Abort ());
448 var responseTask = request.GetResponseAsync ();
450 var context = listener.GetContext ();
451 byte[] outBuffer = new byte[8 * 1024];
452 context.Response.OutputStream.WriteAsync (outBuffer, 0, outBuffer.Length);
454 Assert.IsTrue (responseTask.Wait (1000), "Timeout #1");
456 WebResponse response = responseTask.Result;
457 var stream = response.GetResponseStream ();
459 byte[] buffer = new byte[8 * 1024];
460 var taskRead = stream.ReadAsync (buffer, 0, buffer.Length, tokenSource.Token);
462 tokenSource.Cancel ();
464 Assert.IsTrue (taskRead.Wait (1000), "Timeout #2");
466 var byteRead = taskRead.Result;
468 } catch (AggregateException ex) {
469 var webEx = ex.InnerException as WebException;
470 Assert.IsNotNull(webEx, "Inner exception is not a WebException");
471 Assert.AreEqual (webEx.Status, WebExceptionStatus.RequestCanceled);
474 listener.Close ();