HttpTransportBindingElementTest.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. //
  2. // HttpTransportBindingElementTest.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc. http://www.novell.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.ObjectModel;
  30. using System.IO;
  31. using System.Net;
  32. using System.Net.Security;
  33. using System.ServiceModel;
  34. using System.ServiceModel.Channels;
  35. using System.ServiceModel.Description;
  36. using System.Threading;
  37. using System.Xml;
  38. using NUnit.Framework;
  39. namespace MonoTests.System.ServiceModel.Channels
  40. {
  41. [TestFixture]
  42. public class HttpTransportBindingElementTest
  43. {
  44. static BindingParameterCollection empty_params =
  45. new BindingParameterCollection ();
  46. [Test]
  47. public void DefaultValues ()
  48. {
  49. HttpTransportBindingElement be =
  50. new HttpTransportBindingElement ();
  51. Assert.AreEqual (false, be.AllowCookies, "#1");
  52. Assert.AreEqual (AuthenticationSchemes.Anonymous,
  53. be.AuthenticationScheme, "#2");
  54. Assert.AreEqual (false, be.BypassProxyOnLocal, "#3");
  55. Assert.AreEqual (default (HostNameComparisonMode),
  56. be.HostNameComparisonMode, "#4");
  57. Assert.AreEqual (0x10000, be.MaxBufferSize, "#6");
  58. Assert.IsNull (be.ProxyAddress, "#7");
  59. Assert.AreEqual (AuthenticationSchemes.Anonymous,
  60. be.ProxyAuthenticationScheme, "#8");
  61. Assert.AreEqual (String.Empty, be.Realm, "#9");
  62. Assert.AreEqual ("http", be.Scheme, "#10");
  63. Assert.AreEqual (default (TransferMode),
  64. be.TransferMode, "#11");
  65. Assert.AreEqual (false,
  66. be.UnsafeConnectionNtlmAuthentication, "#12");
  67. Assert.AreEqual (true, be.UseDefaultWebProxy, "#13");
  68. }
  69. [Test]
  70. public void CanBuildChannelFactory ()
  71. {
  72. HttpTransportBindingElement be =
  73. new HttpTransportBindingElement ();
  74. BindingContext ctx = new BindingContext (
  75. new CustomBinding (), empty_params);
  76. Assert.IsTrue (be.CanBuildChannelFactory<IRequestChannel> (ctx), "#1");
  77. Assert.IsFalse (be.CanBuildChannelFactory<IInputChannel> (ctx), "#2");
  78. Assert.IsFalse (be.CanBuildChannelFactory<IReplyChannel> (ctx), "#3");
  79. Assert.IsFalse (be.CanBuildChannelFactory<IOutputChannel> (ctx), "#4");
  80. // seems like it does not support session channels by itself ?
  81. Assert.IsFalse (be.CanBuildChannelFactory<IRequestSessionChannel> (ctx), "#5");
  82. Assert.IsFalse (be.CanBuildChannelFactory<IInputSessionChannel> (ctx), "#6");
  83. Assert.IsFalse (be.CanBuildChannelFactory<IReplySessionChannel> (ctx), "#7");
  84. Assert.IsFalse (be.CanBuildChannelFactory<IOutputSessionChannel> (ctx), "#8");
  85. // IServiceChannel is not supported
  86. Assert.IsFalse (be.CanBuildChannelListener<IServiceChannel> (ctx), "#9");
  87. }
  88. [Test]
  89. public void CanBuildChannelListener ()
  90. {
  91. HttpTransportBindingElement be =
  92. new HttpTransportBindingElement ();
  93. BindingContext ctx = new BindingContext (
  94. new CustomBinding (), empty_params);
  95. Assert.IsTrue (be.CanBuildChannelListener<IReplyChannel> (ctx), "#1");
  96. Assert.IsFalse (be.CanBuildChannelListener<IOutputChannel> (ctx), "#2");
  97. Assert.IsFalse (be.CanBuildChannelListener<IRequestChannel> (ctx), "#3");
  98. Assert.IsFalse (be.CanBuildChannelListener<IInputChannel> (ctx), "#4");
  99. // seems like it does not support session channels by itself ?
  100. Assert.IsFalse (be.CanBuildChannelListener<IReplySessionChannel> (ctx), "#5");
  101. Assert.IsFalse (be.CanBuildChannelListener<IOutputSessionChannel> (ctx), "#6");
  102. Assert.IsFalse (be.CanBuildChannelListener<IRequestSessionChannel> (ctx), "#7");
  103. Assert.IsFalse (be.CanBuildChannelListener<IInputSessionChannel> (ctx), "#8");
  104. // IServiceChannel is not supported
  105. Assert.IsFalse (be.CanBuildChannelListener<IServiceChannel> (ctx), "#9");
  106. }
  107. [Test]
  108. public void BuildChannelFactory ()
  109. {
  110. BindingContext ctx = new BindingContext (
  111. new CustomBinding (
  112. new HttpTransportBindingElement ()),
  113. empty_params);
  114. // returns HttpChannelFactory
  115. IChannelFactory<IRequestChannel> f =
  116. ctx.BuildInnerChannelFactory<IRequestChannel> ();
  117. f.Open (); // required
  118. IChannel c = f.CreateChannel (new EndpointAddress (
  119. "http://www.mono-project.com"));
  120. }
  121. [Test]
  122. [ExpectedException (typeof (InvalidOperationException))]
  123. public void CreateChannelWithoutOpen ()
  124. {
  125. BindingContext ctx = new BindingContext (
  126. new CustomBinding (
  127. new HttpTransportBindingElement ()),
  128. empty_params);
  129. // returns HttpChannelFactory
  130. IChannelFactory<IRequestChannel> f =
  131. ctx.BuildInnerChannelFactory<IRequestChannel> ();
  132. IChannel c = f.CreateChannel (new EndpointAddress (
  133. "http://www.mono-project.com"));
  134. }
  135. [Test]
  136. public void BuildChannelFactoryTwoHttp ()
  137. {
  138. BindingContext ctx = new BindingContext (
  139. new CustomBinding (
  140. new HttpTransportBindingElement (),
  141. new HttpTransportBindingElement ()),
  142. empty_params);
  143. ctx.BuildInnerChannelFactory<IRequestChannel> ();
  144. }
  145. [Test]
  146. public void BuildChannelFactoryHttpThenMessage ()
  147. {
  148. BindingContext ctx = new BindingContext (
  149. new CustomBinding (
  150. new HttpTransportBindingElement (),
  151. new BinaryMessageEncodingBindingElement ()),
  152. empty_params);
  153. IChannelFactory<IRequestChannel> cf =
  154. ctx.BuildInnerChannelFactory<IRequestChannel> ();
  155. cf.Open ();
  156. }
  157. [Test]
  158. // with July CTP it still works ...
  159. public void BuildChannelFactoryHttpNoMessage ()
  160. {
  161. BindingContext ctx = new BindingContext (
  162. new CustomBinding (
  163. new HttpTransportBindingElement ()),
  164. empty_params);
  165. IChannelFactory<IRequestChannel> cf =
  166. ctx.BuildInnerChannelFactory<IRequestChannel> ();
  167. cf.Open ();
  168. }
  169. [Test]
  170. public void BuildChannelFactoryIgnoresRemaining ()
  171. {
  172. BindingContext ctx = new BindingContext (
  173. new CustomBinding (
  174. new HttpTransportBindingElement (),
  175. new InvalidBindingElement ()),
  176. empty_params);
  177. ctx.BuildInnerChannelFactory<IRequestChannel> ();
  178. }
  179. [Test]
  180. [ExpectedException (typeof (ArgumentException))]
  181. public void CreateChannelInvalidScheme ()
  182. {
  183. IChannelFactory<IRequestChannel> f = new BasicHttpBinding ().BuildChannelFactory<IRequestChannel> (new BindingParameterCollection ());
  184. f.Open ();
  185. f.CreateChannel (new EndpointAddress ("stream:dummy"));
  186. }
  187. [Test]
  188. public void BuildChannelListenerWithoutListenUri ()
  189. {
  190. new BasicHttpBinding ().BuildChannelListener<IReplyChannel> (new BindingParameterCollection ());
  191. }
  192. #region connection test
  193. string svcret;
  194. [Test]
  195. [Ignore ("It somehow fails...")]
  196. // It is almost identical to http-low-level-binding
  197. public void LowLevelHttpConnection ()
  198. {
  199. HttpTransportBindingElement lel =
  200. new HttpTransportBindingElement ();
  201. // Service
  202. BindingContext lbc = new BindingContext (
  203. new CustomBinding (),
  204. new BindingParameterCollection (),
  205. new Uri ("http://localhost:37564"),
  206. String.Empty, ListenUriMode.Explicit);
  207. listener = lel.BuildChannelListener<IReplyChannel> (lbc);
  208. try {
  209. listener.Open ();
  210. svcret = "";
  211. Thread svc = new Thread (delegate () {
  212. try {
  213. svcret = LowLevelHttpConnection_SetupService ();
  214. } catch (Exception ex) {
  215. svcret = ex.ToString ();
  216. }
  217. });
  218. svc.Start ();
  219. // Client code goes here.
  220. HttpTransportBindingElement el =
  221. new HttpTransportBindingElement ();
  222. BindingContext ctx = new BindingContext (
  223. new CustomBinding (),
  224. new BindingParameterCollection ());
  225. IChannelFactory<IRequestChannel> factory =
  226. el.BuildChannelFactory<IRequestChannel> (ctx);
  227. factory.Open ();
  228. IRequestChannel request = factory.CreateChannel (
  229. new EndpointAddress ("http://localhost:37564"));
  230. request.Open ();
  231. try {
  232. try {
  233. Message reqmsg = Message.CreateMessage (
  234. MessageVersion.Default, "Echo");
  235. // sync version does not work here.
  236. Message msg = request.Request (reqmsg, TimeSpan.FromSeconds (5));
  237. using (XmlWriter w = XmlWriter.Create (TextWriter.Null)) {
  238. msg.WriteMessage (w);
  239. }
  240. if (svcret != null)
  241. Assert.Fail (svcret.Length > 0 ? svcret : "service code did not finish until this test expected.");
  242. } finally {
  243. if (request.State == CommunicationState.Opened)
  244. request.Close ();
  245. }
  246. } finally {
  247. if (factory.State == CommunicationState.Opened)
  248. factory.Close ();
  249. }
  250. } finally {
  251. if (listener.State == CommunicationState.Opened)
  252. listener.Close ();
  253. }
  254. }
  255. IChannelListener<IReplyChannel> listener;
  256. string LowLevelHttpConnection_SetupService ()
  257. {
  258. IReplyChannel reply = listener.AcceptChannel ();
  259. reply.Open ();
  260. if (!reply.WaitForRequest (TimeSpan.FromSeconds (10)))
  261. return "No request reached here.";
  262. svcret = "Receiving request ...";
  263. RequestContext ctx = reply.ReceiveRequest ();
  264. if (ctx == null)
  265. return "No request context returned.";
  266. svcret = "Starting reply ...";
  267. ctx.Reply (Message.CreateMessage (MessageVersion.Default, "Ack"));
  268. return null; // OK
  269. }
  270. #endregion
  271. }
  272. }