WebHttpBehaviorTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. using System;
  2. using System.IO;
  3. using System.Runtime.Serialization;
  4. using System.ServiceModel;
  5. using System.ServiceModel.Channels;
  6. using System.ServiceModel.Description;
  7. using System.ServiceModel.Dispatcher;
  8. using System.ServiceModel.Web;
  9. using System.Text;
  10. using NUnit.Framework;
  11. namespace MonoTests.System.ServiceModel.Description
  12. {
  13. public class WebHttpBehaviorExt : WebHttpBehavior
  14. {
  15. public IClientMessageFormatter DoGetReplyClientFormatter (OperationDescription operationDescription, ServiceEndpoint endpoint)
  16. {
  17. return GetReplyClientFormatter (operationDescription, endpoint);
  18. }
  19. public IClientMessageFormatter DoGetRequestClientFormatter (OperationDescription operationDescription, ServiceEndpoint endpoint)
  20. {
  21. return GetRequestClientFormatter (operationDescription, endpoint);
  22. }
  23. #if !MOBILE
  24. public IDispatchMessageFormatter DoGetReplyDispatchFormatter (OperationDescription operationDescription, ServiceEndpoint endpoint)
  25. {
  26. return GetReplyDispatchFormatter (operationDescription, endpoint);
  27. }
  28. public IDispatchMessageFormatter DoGetRequestDispatchFormatter (OperationDescription operationDescription, ServiceEndpoint endpoint)
  29. {
  30. return GetRequestDispatchFormatter (operationDescription, endpoint);
  31. }
  32. #endif
  33. public event Action<ServiceEndpoint, ClientRuntime> ApplyClientBehaviorInvoked;
  34. public override void ApplyClientBehavior (ServiceEndpoint endpoint, ClientRuntime client)
  35. {
  36. base.ApplyClientBehavior (endpoint, client);
  37. if (ApplyClientBehaviorInvoked != null)
  38. ApplyClientBehaviorInvoked (endpoint, client);
  39. }
  40. }
  41. [TestFixture]
  42. public class WebHttpBehaviorTest
  43. {
  44. ServiceEndpoint CreateEndpoint ()
  45. {
  46. return new ServiceEndpoint (ContractDescription.GetContract (typeof (IMyService)), new WebHttpBinding (),
  47. new EndpointAddress ("http://localhost:37564"));
  48. }
  49. [Test]
  50. public void DefaultValues ()
  51. {
  52. var b = new WebHttpBehavior ();
  53. Assert.AreEqual (WebMessageBodyStyle.Bare, b.DefaultBodyStyle, "#1");
  54. Assert.AreEqual (WebMessageFormat.Xml, b.DefaultOutgoingRequestFormat, "#2");
  55. Assert.AreEqual (WebMessageFormat.Xml, b.DefaultOutgoingResponseFormat, "#3");
  56. Assert.IsFalse (b.AutomaticFormatSelectionEnabled, "#11");
  57. Assert.IsFalse (b.FaultExceptionEnabled, "#12");
  58. Assert.IsFalse (b.HelpEnabled, "#13");
  59. }
  60. [Test]
  61. public void AddBiningParameters ()
  62. {
  63. var se = CreateEndpoint ();
  64. var b = new WebHttpBehavior ();
  65. var pl = new BindingParameterCollection ();
  66. b.AddBindingParameters (se, pl);
  67. Assert.AreEqual (0, pl.Count, "#1");
  68. }
  69. #if !MOBILE
  70. [Test]
  71. public void ApplyDispatchBehavior ()
  72. {
  73. var se = CreateEndpoint ();
  74. var od = se.Contract.Operations [0];
  75. // in .NET 3.5 it adds "OperationSelectorBehavior"
  76. int initCB = ContractDescription.GetContract (typeof (IMyService)).Behaviors.Count;
  77. // in .NET 3.5 it adds
  78. // - OperationInvokeBehavior,
  79. // - OperationBehaviorAttribute,
  80. // - DataContractSerializerOperationBehavior and
  81. // - DataContractSerializerOperationGenerator
  82. int initOB = od.Behaviors.Count;
  83. // Assert.AreEqual (1, initCB, "#0-1");
  84. // Assert.AreEqual (4, initOB, "#0-2");
  85. var b = new WebHttpBehavior ();
  86. se.Behaviors.Add (b);
  87. var ed = new EndpointDispatcher (se.Address, se.Contract.Name, se.Contract.Namespace);
  88. IChannelListener l = new WebHttpBinding ().BuildChannelListener<IReplyChannel> (new BindingParameterCollection ());
  89. var cd = new ChannelDispatcher (l);
  90. cd.Endpoints.Add (ed); // without it this test results in NRE (it blindly adds IErrorHandler).
  91. Assert.AreEqual (0, cd.ErrorHandlers.Count, "#1-1");
  92. Assert.IsNull (ed.DispatchRuntime.OperationSelector, "#1-2");
  93. Assert.AreEqual (1, se.Behaviors.Count, "#1-3-1");
  94. Assert.AreEqual (initCB, se.Contract.Behaviors.Count, "#1-3-2");
  95. Assert.AreEqual (initOB, od.Behaviors.Count, "#1-3-3");
  96. Assert.IsTrue (ed.AddressFilter is EndpointAddressMessageFilter, "#1-4");
  97. b.ApplyDispatchBehavior (se, ed);
  98. // FIXME: implement and enable it later
  99. //Assert.AreEqual (1, cd.ErrorHandlers.Count, "#2-1");
  100. Assert.AreEqual (typeof (WebHttpDispatchOperationSelector),
  101. ed.DispatchRuntime.OperationSelector.GetType (), "#2-2");
  102. Assert.AreEqual (1, se.Behaviors.Count, "#3-1");
  103. Assert.AreEqual (initCB, se.Contract.Behaviors.Count, "#3-2");
  104. Assert.AreEqual (initOB, od.Behaviors.Count, "#3-3");
  105. // ... i.e. nothing is added.
  106. Assert.IsTrue (ed.AddressFilter is PrefixEndpointAddressMessageFilter, "#3-4");
  107. Assert.AreEqual (0, ed.DispatchRuntime.Operations.Count, "#4-0"); // hmm... really?
  108. }
  109. #endif
  110. [Test]
  111. public void GetMessageFormatters ()
  112. {
  113. var se = CreateEndpoint ();
  114. var od = se.Contract.Operations [0];
  115. var b = new WebHttpBehaviorExt ();
  116. Assert.IsNotNull (b.DoGetRequestClientFormatter (od, se), "#1");
  117. Assert.IsNotNull (b.DoGetReplyClientFormatter (od, se), "#2");
  118. #if !MOBILE
  119. Assert.IsNotNull (b.DoGetRequestDispatchFormatter (od, se), "#3");
  120. Assert.IsNotNull (b.DoGetReplyDispatchFormatter (od, se), "#4");
  121. #endif
  122. }
  123. [Test]
  124. public void RequestClientFormatter ()
  125. {
  126. var se = CreateEndpoint ();
  127. var od = se.Contract.Operations [0];
  128. var b = new WebHttpBehaviorExt ();
  129. var rcf = b.DoGetRequestClientFormatter (od, se);
  130. var msg = rcf.SerializeRequest (MessageVersion.None, new object [] {"foo"});
  131. var hp = msg.Properties [HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
  132. Assert.IsNotNull (hp, "#1");
  133. Assert.IsTrue (msg.IsEmpty, "#2");
  134. Assert.AreEqual (String.Empty, hp.QueryString, "#3");
  135. var mb = msg.CreateBufferedCopy (1000);
  136. try {
  137. rcf.DeserializeReply (mb.CreateMessage (), new object [0]);
  138. Assert.Fail ("It should not support reply deserialization");
  139. } catch (NotSupportedException) {
  140. }
  141. }
  142. #if !MOBILE
  143. [Test]
  144. public void RequestClientFormatter2 ()
  145. {
  146. var se = CreateEndpoint ();
  147. var od = se.Contract.Operations [0];
  148. var b = new WebHttpBehaviorExt ();
  149. IClientMessageFormatter rcf = null;
  150. b.ApplyClientBehaviorInvoked += delegate (ServiceEndpoint e, ClientRuntime cr) {
  151. rcf = cr.Operations [0].Formatter;
  152. };
  153. se.Behaviors.Add (b);
  154. var ch = new WebChannelFactory<IMyServiceClient> (se).CreateChannel ();
  155. var msg = rcf.SerializeRequest (MessageVersion.None, new object [] {"foo"});
  156. var hp = msg.Properties [HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
  157. Assert.IsNotNull (hp, "#1");
  158. Assert.IsTrue (msg.IsEmpty, "#2");
  159. Assert.AreEqual (String.Empty, hp.QueryString, "#3");
  160. //var mb = msg.CreateBufferedCopy (1000);
  161. // TODO: test DeserializeReply too (it is supported unlike above).
  162. }
  163. #endif
  164. [ServiceContract]
  165. public interface IMyService
  166. {
  167. [OperationContract]
  168. [WebGet]
  169. string Echo (string input);
  170. }
  171. public interface IMyServiceClient : IMyService, IClientChannel
  172. {
  173. }
  174. public class MyService: IMyService
  175. {
  176. #if !MOBILE
  177. [OperationBehavior]
  178. #endif
  179. public string Echo (string input)
  180. {
  181. return input;
  182. }
  183. }
  184. [Test]
  185. public void TestWebGetExists()
  186. {
  187. ContractDescription cd = ContractDescription.GetContract (typeof(IMyService), typeof (MyService));
  188. OperationDescription od = cd.Operations[0];
  189. Assert.IsTrue (od.Behaviors.Contains (typeof (WebGetAttribute)), "Operation is recognized as WebGet");
  190. }
  191. #if !MOBILE
  192. [Test]
  193. public void MessageFormatterSupportsRaw ()
  194. {
  195. // serializing reply
  196. var ms = new MemoryStream ();
  197. var bytes = new byte [] {0, 1, 2, 0xFF};
  198. ms.Write (bytes, 0, bytes.Length);
  199. ms.Position = 0;
  200. var cd = ContractDescription.GetContract (typeof (ITestService));
  201. var od = cd.Operations [0];
  202. var se = new ServiceEndpoint (cd, new WebHttpBinding (), new EndpointAddress ("http://localhost:37564/"));
  203. var formatter = new WebHttpBehaviorExt ().DoGetReplyDispatchFormatter (od, se);
  204. var msg = formatter.SerializeReply (MessageVersion.None, null, ms);
  205. var wp = msg.Properties ["WebBodyFormatMessageProperty"] as WebBodyFormatMessageProperty;
  206. Assert.IsNotNull (wp, "#1");
  207. Assert.AreEqual (WebContentFormat.Raw, wp.Format, "#2");
  208. var wmebe = new WebMessageEncodingBindingElement ();
  209. var wme = wmebe.CreateMessageEncoderFactory ().Encoder;
  210. var ms2 = new MemoryStream ();
  211. wme.WriteMessage (msg, ms2);
  212. Assert.AreEqual (bytes, ms2.ToArray (), "#3");
  213. }
  214. [Test]
  215. public void MessageFormatterSupportsRaw2 ()
  216. {
  217. // deserializing request
  218. var ms = new MemoryStream ();
  219. ms.Write (new byte [] {0, 1, 2, 0xFF}, 0, 4);
  220. ms.Position = 0;
  221. var cd = ContractDescription.GetContract (typeof (ITestService));
  222. var od = cd.Operations [0];
  223. var se = new ServiceEndpoint (cd, new WebHttpBinding (), new EndpointAddress ("http://localhost:8080/"));
  224. var wmebe = new WebMessageEncodingBindingElement ();
  225. var wme = wmebe.CreateMessageEncoderFactory ().Encoder;
  226. var msg = wme.ReadMessage (ms, 100, null); // "application/xml" causes error.
  227. var formatter = new WebHttpBehaviorExt ().DoGetRequestDispatchFormatter (od, se);
  228. object [] pars = new object [1];
  229. formatter.DeserializeRequest (msg, pars);
  230. Assert.IsTrue (pars [0] is Stream, "ret");
  231. }
  232. #endif
  233. [ServiceContract]
  234. public interface IMultipleParametersGet
  235. {
  236. [OperationContract]
  237. [WebGet (UriTemplate = "get")]
  238. void Get (string p1, string p2);
  239. }
  240. [ServiceContract]
  241. public interface IMultipleParameters
  242. {
  243. [OperationContract]
  244. [WebInvoke (UriTemplate = "posturi?p1={p1}&p2={p2}")]
  245. string PostUri (string p1, string p2);
  246. [OperationContract]
  247. [WebInvoke (UriTemplate = "postone?p1={p1}")]
  248. string PostOne (string p1, string p2);
  249. [OperationContract]
  250. [WebInvoke (UriTemplate = "postwrapped", BodyStyle=WebMessageBodyStyle.WrappedRequest)]
  251. string PostWrapped (string p1, string p2);
  252. [OperationContract]
  253. [WebInvoke (UriTemplate = "out?p1={p1}&p2={p2}", BodyStyle=WebMessageBodyStyle.WrappedResponse)]
  254. void PostOut (string p1, string p2, out string ret);
  255. }
  256. [Test]
  257. public void MultipleParameters ()
  258. {
  259. var behavior = new WebHttpBehaviorExt ();
  260. var cd = ContractDescription.GetContract (typeof (IMultipleParameters));
  261. var se = new ServiceEndpoint (cd, new WebHttpBinding (), new EndpointAddress ("http://localhost:8080/"));
  262. behavior.Validate (se);
  263. foreach (var od in cd.Operations)
  264. behavior.DoGetRequestClientFormatter (od, se);
  265. }
  266. [Test]
  267. [Category ("NotWorking")]
  268. public void MultipleParameters2 ()
  269. {
  270. var behavior = new WebHttpBehaviorExt ();
  271. var cd = ContractDescription.GetContract (typeof (IMultipleParametersGet));
  272. var se = new ServiceEndpoint (cd, new WebHttpBinding (), new EndpointAddress ("http://localhost:8080/"));
  273. behavior.Validate (se);
  274. try {
  275. foreach (var od in cd.Operations)
  276. behavior.DoGetRequestClientFormatter (od, se);
  277. Assert.Fail ("Should result in invalid operation");
  278. } catch (InvalidOperationException) {
  279. }
  280. }
  281. }
  282. [ServiceContract]
  283. public interface ITestService
  284. {
  285. [OperationContract]
  286. Stream Receive (Stream input);
  287. }
  288. }