| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- //
- // MetadataResolverTest.cs
- //
- // Author:
- // Ankit Jain <[email protected]>
- //
- // Copyright (C) 2006 Novell, Inc. http://www.novell.com
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Runtime.Serialization;
- using System.ServiceModel.Description;
- using System.ServiceModel;
- using NUnit.Framework;
- using MonoTests.Helpers;
- namespace MonoTests.System.ServiceModel.Description
- {
- [TestFixture]
- public class MetadataResolverTest
- {
- string url;
- //string url = "http://192.168.0.1:8080/echo/mex";
- static HttpListener listener;
- IAsyncResult current_request;
- int remaining, port;
- static readonly string mex = File.ReadAllText ("Test/System.ServiceModel.Description/dump.xml");
- [SetUp]
- public void StartupServer ()
- {
- if (listener != null)
- listener.Stop ();
- listener = new HttpListener ();
- port = NetworkHelpers.FindFreePort ();
- url = "http://localhost:" + port + "/echo/mex";
- listener.Prefixes.Add ("http://*:" + port + "/echo/");
- listener.Start ();
- current_request = listener.BeginGetContext (OnReceivedRequest, null);
- remaining = 1;
- }
-
- void OnReceivedRequest (IAsyncResult result)
- {
- try {
- var ctx = listener.EndGetContext (result);
- current_request = null;
- ctx.Response.ContentType = "application/soap+xml";
- ctx.Response.ContentLength64 = mex.Length;
- using (var sw = new StreamWriter (ctx.Response.OutputStream))
- sw.Write (mex);
- ctx.Response.Close ();
- if (--remaining > 0)
- current_request = listener.BeginGetContext (OnReceivedRequest, null);
- } catch (Exception ex) {
- // ignore server errors in this test.
- }
- }
-
- [TearDown]
- public void ShutdownServer ()
- {
- listener.Stop ();
- listener = null;
- }
- [Test]
- [Category ("NotWorking")]
- public void ResolveNoEndpoint ()
- {
- ServiceEndpointCollection endpoints = MetadataResolver.Resolve (
- typeof (NonExistantContract),
- new EndpointAddress (url));
- Assert.IsNotNull (endpoints);
- Assert.AreEqual (0, endpoints.Count);
- }
- [Test]
- [Category ("NotWorking")]
- public void Resolve1 ()
- {
- ServiceEndpointCollection endpoints = MetadataResolver.Resolve (
- typeof (IEchoService), new EndpointAddress (url));
- CheckIEchoServiceEndpoint (endpoints);
- }
- [Test]
- [Category ("NotWorking")]
- public void Resolve2 ()
- {
- ServiceEndpointCollection endpoints = MetadataResolver.Resolve (
- typeof (IEchoService),
- new Uri (url),
- MetadataExchangeClientMode.MetadataExchange);
- CheckIEchoServiceEndpoint (endpoints);
- }
- [Test]
- [Category ("NotWorking")]
- public void Resolve3 ()
- {
- ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
- List<ContractDescription> contracts = new List<ContractDescription> ();
- contracts.Add (contract);
- ServiceEndpointCollection endpoints = MetadataResolver.Resolve (
- contracts,
- new Uri (url),
- MetadataExchangeClientMode.MetadataExchange);
- CheckIEchoServiceEndpoint (endpoints);
- }
- [Test]
- [Category ("NotWorking")]
- public void Resolve4 ()
- {
- ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
- List<ContractDescription> contracts = new List<ContractDescription> ();
- contracts.Add (contract);
- contracts.Add (ContractDescription.GetContract (typeof (NonExistantContract)));
- ServiceEndpointCollection endpoints = MetadataResolver.Resolve (
- contracts,
- new Uri (url),
- MetadataExchangeClientMode.MetadataExchange);
- CheckIEchoServiceEndpoint (endpoints);
- }
- [Test]
- [Category ("NotWorking")]
- public void Resolve5 ()
- {
- ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
- List<ContractDescription> contracts = new List<ContractDescription> ();
- contracts.Add (contract);
- contracts.Add (ContractDescription.GetContract (typeof (NonExistantContract)));
- //FIXME: What is the 'client' param used for?
- //TODO: Write test cases for the related overloads of Resolve
- MetadataResolver.Resolve (
- contracts,
- new EndpointAddress (url),
- new MetadataExchangeClient (new EndpointAddress ("http://localhost")));
- }
- [Test]
- [Category ("NotWorking")]
- public void Resolve6 ()
- {
- ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
- List<ContractDescription> contracts = new List<ContractDescription> ();
- contracts.Add (contract);
- contracts.Add (ContractDescription.GetContract (typeof (NonExistantContract)));
- //FIXME: What is the 'client' param used for?
- //TODO: Write test cases for the related overloads of Resolve
- MetadataResolver.Resolve (
- contracts,
- new Uri (url),
- MetadataExchangeClientMode.MetadataExchange,
- new MetadataExchangeClient (new EndpointAddress ("http://localhost")));
- }
- //Negative tests
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void ErrResolve1 ()
- {
- MetadataResolver.Resolve (
- typeof (IEchoService),
- null,
- MetadataExchangeClientMode.MetadataExchange);
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- [Ignore ("does not fail on .NET either")]
- public void ErrResolve2 ()
- {
- //Mex cannot be fetched with HttpGet from the given url
- MetadataResolver.Resolve (
- typeof (IEchoService),
- new Uri (url),
- MetadataExchangeClientMode.HttpGet);
- }
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void ErrResolve3 ()
- {
- ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
- List<ContractDescription> contracts = new List<ContractDescription> ();
- contracts.Add (contract);
- contracts.Add (ContractDescription.GetContract (typeof (NonExistantContract)));
- MetadataResolver.Resolve (contracts, new EndpointAddress (url),
- (MetadataExchangeClient) null);
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void ErrResolve4 ()
- {
- ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
- List<ContractDescription> contracts = new List<ContractDescription> ();
- contracts.Add (contract);
- contracts.Add (ContractDescription.GetContract (typeof (NonExistantContract)));
- //FIXME: What is the 'client' param used for?
- //TODO: Write test cases for the related overloads of Resolve
- MetadataResolver.Resolve (
- contracts,
- new EndpointAddress ("http://localhost"),
- new MetadataExchangeClient (new EndpointAddress (url)));
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- [Ignore ("does not fail on .NET either")]
- public void ErrResolve5 ()
- {
- ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
- List<ContractDescription> contracts = new List<ContractDescription> ();
- contracts.Add (contract);
- contracts.Add (ContractDescription.GetContract (typeof (NonExistantContract)));
- //FIXME: What is the 'client' param used for?
- //TODO: Write test cases for the related overloads of Resolve
- MetadataResolver.Resolve (
- contracts,
- new Uri (url),
- MetadataExchangeClientMode.HttpGet,
- new MetadataExchangeClient (new EndpointAddress ("http://localhost")));
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void ErrResolve6 ()
- {
- ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
- List<ContractDescription> contracts = new List<ContractDescription> ();
- //FIXME: What is the 'client' param used for?
- //TODO: Write test cases for the related overloads of Resolve
- MetadataResolver.Resolve (
- contracts,
- new Uri (url),
- MetadataExchangeClientMode.HttpGet,
- new MetadataExchangeClient (new EndpointAddress ("http://localhost")));
- }
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void ErrResolve7 ()
- {
- MetadataResolver.Resolve (
- null,
- new Uri (url),
- MetadataExchangeClientMode.HttpGet,
- new MetadataExchangeClient (new EndpointAddress ("http://localhost")));
- }
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void ErrResolve8 ()
- {
- ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
- List<ContractDescription> contracts = new List<ContractDescription> ();
- contracts.Add (contract);
- MetadataResolver.Resolve (contracts, null);
- }
- /* Test for bad endpoint address */
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void ErrResolve9 ()
- {
- ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
- List<ContractDescription> contracts = new List<ContractDescription> ();
- contracts.Add (contract);
- MetadataResolver.Resolve (contracts, new EndpointAddress ("http://localhost"));
- }
- private void CheckIEchoServiceEndpoint (ServiceEndpointCollection endpoints)
- {
- Assert.IsNotNull (endpoints);
- Assert.AreEqual (1, endpoints.Count);
- ServiceEndpoint ep = endpoints [0];
- //URI Dependent
- //Assert.AreEqual ("http://localhost:8080/echo/svc", ep.Address.Uri.AbsoluteUri, "#R1");
- Assert.AreEqual ("IEchoService", ep.Contract.Name, "#R3");
- Assert.AreEqual ("http://myns/echo", ep.Contract.Namespace, "#R4");
- Assert.AreEqual ("BasicHttpBinding_IEchoService", ep.Name, "#R5");
- Assert.AreEqual (typeof (BasicHttpBinding), ep.Binding.GetType (), "#R2");
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void ResolveNonContract ()
- {
- MetadataResolver.Resolve (
- typeof (Int32), new EndpointAddress (url));
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void ResolveBadUri ()
- {
- MetadataResolver.Resolve (
- typeof (IEchoService), new EndpointAddress ("http://localhost"));
- }
- [DataContract]
- public class dc
- {
- [DataMember]
- string field;
- }
- [ServiceContract (Namespace = "http://myns/echo")]
- public interface IEchoService
- {
- [OperationContract]
- string Echo (string msg, int num, dc d);
- [OperationContract]
- string DoubleIt (int it, string prefix);
- }
- [ServiceContract]
- public class NonExistantContract
- {
- }
- }
- }
|