SoapHttpClientProtocolTest.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. //
  2. // System.Web.Services.Protocols.SoapHttpClientProtocolTest.cs
  3. //
  4. // Author:
  5. // Gert Driesen <[email protected]>
  6. //
  7. // (C) 2007 Gert Driesen
  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.IO;
  30. using System.Net;
  31. using System.Net.Sockets;
  32. using System.Text;
  33. using System.Web.Services;
  34. using System.Web.Services.Description;
  35. using System.Web.Services.Protocols;
  36. using System.Xml;
  37. using System.Xml.Serialization;
  38. using NUnit.Framework;
  39. namespace MonoTests.System.Web.Services.Protocols
  40. {
  41. [TestFixture]
  42. public class SoapHttpClientProtocolTest
  43. {
  44. [Ignore ("this kind of connection oriented tests got non-working after some Windows updates in .NET 2.0 (1.1 still works).")]
  45. [Test] // bug #79988
  46. public void OutParametersTest ()
  47. {
  48. IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 5000);
  49. using (SocketResponder sr = new SocketResponder (localEP, new SocketRequestHandler (OutParametersResponse))) {
  50. sr.Start ();
  51. FooService service = new FooService ();
  52. service.Url = "http://" + IPAddress.Loopback.ToString () + ":5000/";
  53. int a;
  54. bool b;
  55. Elem [] e = service.Req ("x", out a, out b);
  56. Assert.IsNull (e, "#A1");
  57. Assert.AreEqual (0, a, "#A2");
  58. Assert.IsFalse (b, "#A3");
  59. service.Dispose ();
  60. sr.Stop ();
  61. }
  62. }
  63. [Ignore ("this kind of connection oriented tests got non-working after some Windows updates in .NET 2.0 (1.1 still works).")]
  64. [Test] // bug #81886
  65. public void FaultTest ()
  66. {
  67. IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 5000);
  68. using (SocketResponder sr = new SocketResponder (localEP, new SocketRequestHandler (FaultResponse_Qualified))) {
  69. sr.Start ();
  70. FooService service = new FooService ();
  71. service.Url = "http://" + IPAddress.Loopback.ToString () + ":5000/";
  72. try {
  73. service.Run ();
  74. Assert.Fail ("#A1");
  75. } catch (SoapException ex) {
  76. Assert.AreEqual ("Mono Web Service", ex.Actor, "#A2");
  77. Assert.AreEqual (SoapException.ServerFaultCode, ex.Code, "#A3");
  78. Assert.IsNotNull (ex.Detail, "#A4");
  79. Assert.AreEqual ("detail", ex.Detail.LocalName, "#A5");
  80. Assert.AreEqual ("http://schemas.xmlsoap.org/soap/envelope/", ex.Detail.NamespaceURI, "#A6");
  81. XmlNamespaceManager nsMgr = new XmlNamespaceManager (ex.Detail.OwnerDocument.NameTable);
  82. nsMgr.AddNamespace ("se", "http://www.mono-project/System");
  83. XmlElement systemError = (XmlElement) ex.Detail.SelectSingleNode (
  84. "se:systemerror", nsMgr);
  85. Assert.IsNotNull (systemError, "#A7");
  86. Assert.IsNull (ex.InnerException, "#A8");
  87. Assert.AreEqual ("Failure processing request.", ex.Message, "#A9");
  88. }
  89. service.Dispose ();
  90. sr.Stop ();
  91. }
  92. using (SocketResponder sr = new SocketResponder (localEP, new SocketRequestHandler (FaultResponse_Unqualified))) {
  93. sr.Start ();
  94. FooService service = new FooService ();
  95. service.Url = "http://" + IPAddress.Loopback.ToString () + ":5000/";
  96. try {
  97. service.Run ();
  98. Assert.Fail ("#B1");
  99. } catch (SoapException ex) {
  100. Assert.AreEqual ("Mono Web Service", ex.Actor, "#B2");
  101. Assert.AreEqual (SoapException.ServerFaultCode, ex.Code, "#B3");
  102. Assert.IsNotNull (ex.Detail, "#B4");
  103. Assert.AreEqual ("detail", ex.Detail.LocalName, "#B5");
  104. Assert.AreEqual (string.Empty, ex.Detail.NamespaceURI, "#B6");
  105. XmlNamespaceManager nsMgr = new XmlNamespaceManager (ex.Detail.OwnerDocument.NameTable);
  106. nsMgr.AddNamespace ("se", "http://www.mono-project/System");
  107. XmlElement systemError = (XmlElement) ex.Detail.SelectSingleNode (
  108. "se:systemerror", nsMgr);
  109. Assert.IsNotNull (systemError, "#B7");
  110. Assert.IsNull (ex.InnerException, "#B8");
  111. Assert.AreEqual ("Failure processing request.", ex.Message, "#B9");
  112. }
  113. service.Dispose ();
  114. sr.Stop ();
  115. }
  116. }
  117. static byte [] FaultResponse_Qualified (Socket socket)
  118. {
  119. string responseContent =
  120. "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
  121. " <soap:Body>" +
  122. " <soap:Fault>" +
  123. " <soap:faultcode>soap:Server</soap:faultcode>" +
  124. " <soap:faultstring>Failure processing request.</soap:faultstring>" +
  125. " <soap:faultactor>Mono Web Service</soap:faultactor>" +
  126. " <soap:detail>" +
  127. " <se:systemerror xmlns:se=\"http://www.mono-project/System\">" +
  128. " <se:code>5000</se:code>" +
  129. " <se:description>Invalid credentials.</se:description>" +
  130. " </se:systemerror>" +
  131. " </soap:detail>" +
  132. " </soap:Fault>" +
  133. " </soap:Body>" +
  134. "</soap:Envelope>";
  135. StringWriter sw = new StringWriter ();
  136. sw.WriteLine ("HTTP/1.1 200 OK");
  137. sw.WriteLine ("Content-Type: text/xml");
  138. sw.WriteLine ("Content-Length: " + responseContent.Length);
  139. sw.WriteLine ();
  140. sw.Write (responseContent);
  141. sw.Flush ();
  142. return Encoding.UTF8.GetBytes (sw.ToString ());
  143. }
  144. static byte [] FaultResponse_Unqualified (Socket socket)
  145. {
  146. string responseContent =
  147. "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
  148. " <soap:Body>" +
  149. " <soap:Fault>" +
  150. " <faultcode>soap:Server</faultcode>" +
  151. " <faultstring>Failure processing request.</faultstring>" +
  152. " <faultactor>Mono Web Service</faultactor>" +
  153. " <detail>" +
  154. " <se:systemerror xmlns:se=\"http://www.mono-project/System\">" +
  155. " <se:code>5000</se:code>" +
  156. " <se:description>Invalid credentials.</se:description>" +
  157. " </se:systemerror>" +
  158. " </detail>" +
  159. " </soap:Fault>" +
  160. " </soap:Body>" +
  161. "</soap:Envelope>";
  162. StringWriter sw = new StringWriter ();
  163. sw.WriteLine ("HTTP/1.1 200 OK");
  164. sw.WriteLine ("Content-Type: text/xml");
  165. sw.WriteLine ("Content-Length: " + responseContent.Length);
  166. sw.WriteLine ();
  167. sw.Write (responseContent);
  168. sw.Flush ();
  169. return Encoding.UTF8.GetBytes (sw.ToString ());
  170. }
  171. static byte [] OutParametersResponse (Socket socket)
  172. {
  173. string responseContent =
  174. "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
  175. " <soap:Body>" +
  176. " <ReqResponse2 xmlns=\"urn:foo\">" +
  177. " <Hits>ERERE</Hits>" +
  178. " </ReqResponse2>" +
  179. " </soap:Body>" +
  180. "</soap:Envelope>";
  181. StringWriter sw = new StringWriter ();
  182. sw.WriteLine ("HTTP/1.1 200 OK");
  183. sw.WriteLine ("Content-Type: text/xml");
  184. sw.WriteLine ("Content-Length: " + responseContent.Length);
  185. sw.WriteLine ();
  186. sw.Write (responseContent);
  187. sw.Flush ();
  188. return Encoding.UTF8.GetBytes (sw.ToString ());
  189. }
  190. [WebServiceBindingAttribute (Name = "Foo", Namespace = "urn:foo")]
  191. public class FooService : SoapHttpClientProtocol
  192. {
  193. [SoapDocumentMethodAttribute ("", RequestElementName = "Req", RequestNamespace = "urn:foo", ResponseNamespace = "urn:foo", Use = SoapBindingUse.Literal, ParameterStyle = SoapParameterStyle.Wrapped)]
  194. [return: XmlElementAttribute ("Hits")]
  195. public Elem [] Req ([XmlAttributeAttribute ()] string arg, [XmlAttributeAttribute ()] out int status, [XmlAttributeAttribute ()] [XmlIgnoreAttribute ()] out bool statusSpecified)
  196. {
  197. object [] results = this.Invoke ("Req", new object [] { arg });
  198. status = ((int) (results [1]));
  199. statusSpecified = ((bool) (results [2]));
  200. return ((Elem []) (results [0]));
  201. }
  202. [SoapDocumentMethodAttribute ("", RequestElementName = "Run", RequestNamespace = "urn:foo", ResponseNamespace = "urn:foo", Use = SoapBindingUse.Literal, ParameterStyle = SoapParameterStyle.Wrapped)]
  203. [return: XmlElementAttribute ("Hits")]
  204. public Elem Run ()
  205. {
  206. this.Invoke ("Run", new object [0]);
  207. return new Elem ();
  208. }
  209. }
  210. [SerializableAttribute ()]
  211. public class Elem
  212. {
  213. private string attrField;
  214. [XmlAttributeAttribute ()]
  215. public string attr
  216. {
  217. get
  218. {
  219. return this.attrField;
  220. }
  221. set
  222. {
  223. this.attrField = value;
  224. }
  225. }
  226. }
  227. }
  228. }