HttpClientHandlerTest.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. // HttpClientHandlerTest.cs
  3. //
  4. // Authors:
  5. // Marek Safar <[email protected]>
  6. //
  7. // Copyright (C) 2011 Xamarin Inc (http://www.xamarin.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using NUnit.Framework;
  32. using System.Net.Http;
  33. using System.Net;
  34. namespace MonoTests.System.Net.Http
  35. {
  36. [TestFixture]
  37. public class HttpClientHandlerTest
  38. {
  39. class Proxy : IWebProxy
  40. {
  41. public ICredentials Credentials {
  42. get {
  43. throw new NotImplementedException ();
  44. }
  45. set {
  46. throw new NotImplementedException ();
  47. }
  48. }
  49. public Uri GetProxy (Uri destination)
  50. {
  51. throw new NotImplementedException ();
  52. }
  53. public bool IsBypassed (Uri host)
  54. {
  55. throw new NotImplementedException ();
  56. }
  57. }
  58. [Test]
  59. #if FEATURE_NO_BSD_SOCKETS
  60. [ExpectedException (typeof (PlatformNotSupportedException))]
  61. #endif
  62. public void Properties_Defaults ()
  63. {
  64. var h = HttpClientTestHelpers.CreateHttpClientHandler ();
  65. Assert.IsTrue (h.AllowAutoRedirect, "#1");
  66. Assert.AreEqual (DecompressionMethods.None, h.AutomaticDecompression, "#2");
  67. Assert.AreEqual (0, h.CookieContainer.Count, "#3");
  68. Assert.AreEqual (4096, h.CookieContainer.MaxCookieSize, "#3b");
  69. Assert.AreEqual (null, h.Credentials, "#4");
  70. Assert.AreEqual (50, h.MaxAutomaticRedirections, "#5");
  71. Assert.IsFalse (h.PreAuthenticate, "#7");
  72. Assert.IsNull (h.Proxy, "#8");
  73. Assert.IsTrue (h.SupportsAutomaticDecompression, "#9");
  74. Assert.IsTrue (h.SupportsProxy, "#10");
  75. Assert.IsTrue (h.SupportsRedirectConfiguration, "#11");
  76. Assert.IsTrue (h.UseCookies, "#12");
  77. Assert.IsFalse (h.UseDefaultCredentials, "#13");
  78. Assert.IsTrue (h.UseProxy, "#14");
  79. Assert.AreEqual (ClientCertificateOption.Manual, h.ClientCertificateOptions, "#15");
  80. }
  81. [Test]
  82. #if FEATURE_NO_BSD_SOCKETS
  83. [ExpectedException (typeof (PlatformNotSupportedException))]
  84. #endif
  85. public void Properties_Invalid ()
  86. {
  87. var h = HttpClientTestHelpers.CreateHttpClientHandler ();
  88. try {
  89. h.MaxAutomaticRedirections = 0;
  90. Assert.Fail ("#1");
  91. } catch (ArgumentOutOfRangeException) {
  92. }
  93. try {
  94. h.MaxRequestContentBufferSize = -1;
  95. Assert.Fail ("#2");
  96. } catch (ArgumentOutOfRangeException) {
  97. }
  98. if (HttpClientTestHelpers.UsingSocketsHandler)
  99. Assert.Ignore ();
  100. h.UseProxy = false;
  101. try {
  102. h.Proxy = new Proxy ();
  103. Assert.Fail ("#3");
  104. } catch (InvalidOperationException) {
  105. }
  106. }
  107. [Test]
  108. #if FEATURE_NO_BSD_SOCKETS
  109. [ExpectedException (typeof (PlatformNotSupportedException))]
  110. #endif
  111. public void Properties_AfterClientCreation ()
  112. {
  113. var h = HttpClientTestHelpers.CreateHttpClientHandler ();
  114. h.AllowAutoRedirect = true;
  115. // We may modify properties after creating the HttpClient.
  116. using (var c = new HttpClient (h, true)) {
  117. h.AllowAutoRedirect = false;
  118. }
  119. }
  120. [Test]
  121. #if FEATURE_NO_BSD_SOCKETS
  122. [ExpectedException (typeof (PlatformNotSupportedException))]
  123. #endif
  124. public void Disposed ()
  125. {
  126. var h = HttpClientTestHelpers.CreateHttpClientHandler ();
  127. h.Dispose ();
  128. var c = new HttpClient (h);
  129. try {
  130. c.GetAsync ("http://google.com").Wait ();
  131. Assert.Fail ("#1");
  132. } catch (AggregateException e) {
  133. Assert.IsTrue (e.InnerException is ObjectDisposedException, "#2");
  134. Assert.IsFalse (HttpClientTestHelpers.IsSocketsHandler (h), "#3");
  135. } catch (ObjectDisposedException) {
  136. Assert.IsTrue (HttpClientTestHelpers.IsSocketsHandler (h), "#4");
  137. }
  138. }
  139. }
  140. }