Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@ -0,0 +1,35 @@
2010-05-24 Atsushi Enomoto <atsushi@ximian.com>
* JsonQueryStringConverterTest.cs : add more tests for CanConvert()
and reorganized some classes to make tests rational.
2009-10-08 Atsushi Enomoto <atsushi@ximian.com>
* JsonQueryStringConverterTest.cs : added some string deserialization
test. It just proved .NET is too lame.
2009-09-02 Atsushi Enomoto <atsushi@ximian.com>
* JsonQueryStringConverterTest.cs : comment out one cosmetic case
that is not working under .NET.
2008-02-18 Atsushi Enomoto <atsushi@ximian.com>
* JsonQueryStringConverterTest.cs : new test.
2008-02-15 Atsushi Enomoto <atsushi@ximian.com>
* WebHttpDispatchOperationSelectorTest.cs : added couple more tests
with different contract contract case.
2008-02-15 Atsushi Enomoto <atsushi@ximian.com>
* WebHttpDispatchOperationSelectorTest.cs : new test.
2008-02-13 Atsushi Enomoto <atsushi@ximian.com>
* QueryStringConverterTest.cs : enable null-to-char conversion test.
2008-02-13 Atsushi Enomoto <atsushi@ximian.com>
* QueryStringConverterTest.cs : new test.

View File

@ -0,0 +1,327 @@
//
// JsonQueryStringConverterTest.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
// Copyright 2014 Xamarin Inc. (http://www.xamarin.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.ComponentModel;
using System.Globalization;
using System.Runtime.Serialization;
using System.ServiceModel.Dispatcher;
using System.Text;
using System.Threading;
using System.Xml;
using NUnit.Framework;
using CategoryAttribute = NUnit.Framework.CategoryAttribute;
namespace MonoTests.System.ServiceModel.Description
{
[TestFixture]
public class JsonQueryStringConverterTest
{
JsonQueryStringConverter c;
[SetUp]
public void Setup ()
{
c = new JsonQueryStringConverter ();
}
[Test]
public void CanConvert ()
{
Assert.IsTrue (c.CanConvert (typeof (bool)), "#1");
Assert.IsTrue (c.CanConvert (typeof (char)), "#2");
Assert.IsTrue (c.CanConvert (typeof (double)), "#3");
Assert.IsTrue (c.CanConvert (typeof (decimal)), "#4");
Assert.IsTrue (c.CanConvert (typeof (float)), "#5");
Assert.IsTrue (c.CanConvert (typeof (string)), "#6");
Assert.IsTrue (c.CanConvert (typeof (int)), "#7");
Assert.IsTrue (c.CanConvert (typeof (byte)), "#8");
Assert.IsTrue (c.CanConvert (typeof (sbyte)), "#9");
Assert.IsTrue (c.CanConvert (typeof (long)), "#10");
Assert.IsTrue (c.CanConvert (typeof (ulong)), "#11");
Assert.IsTrue (c.CanConvert (typeof (DateTime)), "#12");
Assert.IsTrue (c.CanConvert (typeof (DateTimeOffset)), "#13");
Assert.IsTrue (c.CanConvert (typeof (TimeSpan)), "#14");
Assert.IsTrue (c.CanConvert (typeof (Guid)), "#15");
Assert.IsTrue (c.CanConvert (typeof (XmlQualifiedName)), "#16");
Assert.IsTrue (c.CanConvert (typeof (object)), "#17");
Assert.IsTrue (c.CanConvert (typeof (QueryStringConverter)), "#18");
// TypeConverterAttribute does not help it.
Assert.IsFalse (c.CanConvert (typeof (MyConvertible)), "#19");
Assert.IsTrue (c.CanConvert (typeof (MyPublicClass)), "#20");
Assert.IsTrue (c.CanConvert (typeof (MyNestedPublicClass)), "#21");
Assert.IsTrue (c.CanConvert (typeof (MyNestedPrivateClass)), "#22");
Assert.IsTrue (c.CanConvert (typeof (List<int>)), "#23");
Assert.IsTrue (c.CanConvert (typeof (List<MyPublicClass>)), "#24");
// FIXME: enable it
//Assert.IsFalse (c.CanConvert (typeof (List<MyInternalClass>)), "#25");
}
// ConvertValueToString
[Test]
public void ConvertValueToStringValidCast ()
{
Assert.AreEqual ("\"ABC\"", c.ConvertValueToString ("ABC", typeof (char)));
}
[Test]
[Ignore ("huh? .NET converts to 123, not \"123\"")]
public void ConvertValueToStringValidCast2 ()
{
Assert.AreEqual ("\"123\"", c.ConvertValueToString (123, typeof (string)));
}
[Test]
[Ignore ("huh? .NET converts to \"123\", not 123")]
public void ConvertValueToStringValidCast3 ()
{
Assert.AreEqual ("123", c.ConvertValueToString ("123", typeof (int)));
}
[Test]
public void ConvertValueToStringValidCast4 ()
{
Assert.AreEqual ("123.45", c.ConvertValueToString (123.45, typeof (int)));
}
[Test]
public void ConvertValueToStringValidCast4_ptBR ()
{
var ci = CultureInfo.CurrentCulture;
try {
Thread.CurrentThread.CurrentCulture = new CultureInfo ("pt-BR");
Assert.AreEqual ("123.45", c.ConvertValueToString (123.45, typeof (int)));
}
finally {
Thread.CurrentThread.CurrentCulture = ci;
}
}
[Test]
public void ConvertValueToStringValidCast5 ()
{
Assert.AreEqual ("123", c.ConvertValueToString (123, typeof (double)));
}
[Test]
public void ConvertValueToStringValidCast6 ()
{
// umm... should be out of range
Assert.AreEqual ("12345", c.ConvertValueToString (12345, typeof (byte)));
}
[Test]
public void ConvertValueToStringNullToValueType ()
{
Assert.AreEqual (null, c.ConvertValueToString (null, typeof (char)));
}
[Test]
public void ConvertValueToStringDbNull ()
{
Assert.AreEqual ("{}", c.ConvertValueToString (DBNull.Value, typeof (DBNull)));
}
[Test]
public void ConvertValueToStringDbNull2 ()
{
Assert.AreEqual ("\"\"", c.ConvertValueToString ("", typeof (DBNull)));
}
[Test]
public void ConvertValueToStringDbNull3 ()
{
Assert.AreEqual (null, c.ConvertValueToString (null, typeof (DBNull)));
}
[Test]
public void ConvertValueToStringDbNull4 ()
{
// ... so, DBNull is just converted to String
Assert.AreEqual ("\"ABC\"", c.ConvertValueToString ("ABC", typeof (DBNull)));
}
[Test]
public void ConvertValueToStringQName ()
{
Assert.AreEqual ("\"foo:\"", c.ConvertValueToString (new XmlQualifiedName ("foo"), typeof (XmlQualifiedName)), "#1");
Assert.AreEqual ("\"foo:urn:bar\"", c.ConvertValueToString (new XmlQualifiedName ("foo", "urn:bar"), typeof (XmlQualifiedName)), "#2");
}
[Test]
public void ConvertValueToStringNullToString ()
{
Assert.IsNull (c.ConvertValueToString (null, typeof (string)));
}
[Test]
public void ConvertValueToString ()
{
Assert.AreEqual ("\"A\"", c.ConvertValueToString ('A', typeof (char)), "#1");
Assert.AreEqual ("\"}}}\"", c.ConvertValueToString ("}}}", typeof (string)), "#2");
Assert.AreEqual ("123", c.ConvertValueToString (123.0, typeof (double)), "#3");
}
// ConvertStringToValue
[Test]
[ExpectedException (typeof (FormatException))]
public void ConvertStringToValueInvalidCast ()
{
c.ConvertStringToValue ("ABC", typeof (char));
}
[Test]
[ExpectedException (typeof (FormatException))]
[Category ("NotWorking")]
public void ConvertStringToValueInvalidCast2 ()
{
c.ConvertStringToValue ("-123", typeof (uint));
}
[Test]
[ExpectedException (typeof (FormatException))]
public void ConvertStringToValueInvalidCast4 ()
{
c.ConvertStringToValue ("123.45", typeof (int));
}
[Test]
public void ConvertStringToValueString1 ()
{
// hmm ...
Assert.AreEqual ("-123.45", c.ConvertStringToValue ("-123.45", typeof (string)));
}
[Test]
public void ConvertStringToValueString2 ()
{
Assert.AreEqual ("ABC", c.ConvertStringToValue ("\"ABC\"", typeof (string)));
}
[Test]
[ExpectedException (typeof (SerializationException))]
public void ConvertStringToValueString3 ()
{
// missing closing '"'
Assert.AreEqual ("\"ABC", c.ConvertStringToValue ("\"ABC", typeof (string)));
// this test exposes that .NET uses DataContractJsonSerializer internally.
}
[Test]
public void ConvertStringToValueNullToValueType ()
{
// hmm, it passes.
Assert.AreEqual (default (char), c.ConvertStringToValue (null, typeof (char)));
}
[Test]
public void ConvertStringToValueDbNull ()
{
Assert.AreEqual (null, c.ConvertStringToValue (null, typeof (DBNull)));
}
[Test]
[ExpectedException (typeof (SerializationException))]
public void ConvertStringToValueDbNull2 ()
{
c.ConvertStringToValue ("", typeof (DBNull));
}
[Test]
[ExpectedException (typeof (SerializationException))]
public void ConvertStringToValueDbNull3 ()
{
c.ConvertStringToValue ("ABC", typeof (DBNull));
}
[Test]
public void ConvertStringToValueDbNull4 ()
{
Assert.AreEqual (DBNull.Value, c.ConvertStringToValue ("{}", typeof (DBNull)));
}
[Test]
public void ConvertStringToValueNullToString ()
{
Assert.IsNull (c.ConvertStringToValue (null, typeof (string)));
}
[Test]
public void ConvertStringToValue ()
{
Assert.AreEqual ('A', c.ConvertStringToValue ("A", typeof (char)), "#1");
// likely .NET bug: it should fail to deserialize as it is not a valid JSON string.
//Assert.AreEqual ("}}}", c.ConvertStringToValue ("}}}", typeof (string)), "#2");
Assert.AreEqual (123.0, c.ConvertStringToValue ("123.0", typeof (double)), "#3");
Assert.AreEqual (123.0, c.ConvertStringToValue ("123", typeof (double)), "#4");
Assert.AreEqual ("A", c.ConvertStringToValue ("\"A\"", typeof (string)), "#5");
// LAMESPEC: it should either always expect or preserve double-quotes. This behavior is just inconsistent.
Assert.AreEqual ("A", c.ConvertStringToValue ("A", typeof (string)), "#6");
Assert.AreEqual ("A%22B", c.ConvertStringToValue ("A%22B", typeof (string)), "#7"); // it's not either covered by url escaping.
}
// Types
public class MyNestedPublicClass
{
}
public class MyNestedPrivateClass
{
}
}
// non-nested types
public class MyPublicClass
{
}
class MyInternalClass
{
}
[TypeConverter (typeof (MyTypeConverter))]
class MyConvertible
{
}
class MyTypeConverter : TypeConverter
{
public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof (string))
return "hogehoge";
throw new Exception ();
}
}
}

View File

@ -0,0 +1,235 @@
//
// QueryStringConverterTest.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2008 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.ComponentModel;
using System.Globalization;
using System.ServiceModel.Dispatcher;
using System.Text;
using System.Xml;
using NUnit.Framework;
using CategoryAttribute = NUnit.Framework.CategoryAttribute;
namespace MonoTests.System.ServiceModel.Description
{
[TestFixture]
public class QueryStringConverterTest
{
QueryStringConverter c;
[SetUp]
public void Setup ()
{
c = new QueryStringConverter ();
}
[Test]
public void CanConvert ()
{
Assert.IsTrue (c.CanConvert (typeof (bool)), "#1");
Assert.IsTrue (c.CanConvert (typeof (char)), "#2");
Assert.IsTrue (c.CanConvert (typeof (double)), "#3");
Assert.IsTrue (c.CanConvert (typeof (decimal)), "#4");
Assert.IsTrue (c.CanConvert (typeof (float)), "#5");
Assert.IsTrue (c.CanConvert (typeof (string)), "#6");
Assert.IsTrue (c.CanConvert (typeof (int)), "#7");
Assert.IsTrue (c.CanConvert (typeof (byte)), "#8");
Assert.IsTrue (c.CanConvert (typeof (sbyte)), "#9");
Assert.IsTrue (c.CanConvert (typeof (long)), "#10");
Assert.IsTrue (c.CanConvert (typeof (ulong)), "#11");
Assert.IsTrue (c.CanConvert (typeof (DateTime)), "#12");
Assert.IsTrue (c.CanConvert (typeof (DateTimeOffset)), "#13");
Assert.IsTrue (c.CanConvert (typeof (TimeSpan)), "#14");
Assert.IsTrue (c.CanConvert (typeof (Guid)), "#15");
Assert.IsFalse (c.CanConvert (typeof (XmlQualifiedName)), "#16");
Assert.IsTrue (c.CanConvert (typeof (object)), "#17");
Assert.IsFalse (c.CanConvert (typeof (QueryStringConverter)), "#18");
// TypeConverterAttribute does not help it.
Assert.IsFalse (c.CanConvert (typeof (MyConvertible)), "#19");
Assert.IsTrue (c.CanConvert (typeof (DemoEnum)), "#20");
}
// ConvertStringToValue
[Test]
[ExpectedException (typeof (InvalidCastException))]
public void ConvertValueToStringInvalidCast ()
{
c.ConvertValueToString ("ABC", typeof (char));
}
[Test]
[ExpectedException (typeof (InvalidCastException))]
public void ConvertValueToStringInvalidCast2 ()
{
c.ConvertValueToString (123, typeof (string));
}
[Test]
[ExpectedException (typeof (InvalidCastException))]
public void ConvertValueToStringInvalidCast3 ()
{
c.ConvertValueToString ("123", typeof (int));
}
[Test]
[ExpectedException (typeof (InvalidCastException))]
public void ConvertValueToStringInvalidCast4 ()
{
c.ConvertValueToString (123.45, typeof (int));
}
[Test]
[ExpectedException (typeof (InvalidCastException))]
public void ConvertValueToStringInvalidCast5 ()
{
// umm...
c.ConvertValueToString (123, typeof (double));
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ConvertValueToStringNullToValueType ()
{
c.ConvertValueToString (null, typeof (char));
}
[Test]
[ExpectedException (typeof (NotSupportedException))]
public void ConvertValueToStringDbNull ()
{
c.ConvertValueToString (DBNull.Value, typeof (DBNull));
}
[Test]
public void ConvertValueToStringNullToString ()
{
Assert.IsNull (c.ConvertValueToString (null, typeof (string)));
}
[Test]
public void ConvertValueToString ()
{
Assert.AreEqual ("A", c.ConvertValueToString ('A', typeof (char)), "#1");
Assert.AreEqual ("}}}", c.ConvertValueToString ("}}}", typeof (string)), "#2");
Assert.AreEqual ("123", c.ConvertValueToString (123.0, typeof (double)), "#3");
}
[Test]
public void ConvertValueToStringEnum ()
{
string stringValue = c.ConvertValueToString (DemoEnum.Value2, typeof (DemoEnum));
Assert.AreEqual ("Value2", stringValue);
}
// ConvertStringToValue
[Test]
public void ConvertStringToValueEnum ()
{
Assert.AreEqual (DemoEnum.Value3, (DemoEnum)c.ConvertStringToValue ("Value3", typeof(DemoEnum)));
Assert.AreEqual (DemoEnum.Value2, (DemoEnum)c.ConvertStringToValue ("value2", typeof(DemoEnum)));
}
[Test]
[ExpectedException (typeof (FormatException))]
public void ConvertStringToValueInvalidCast ()
{
c.ConvertStringToValue ("ABC", typeof (char));
}
[Test]
[ExpectedException (typeof (FormatException))]
[Category ("NotWorking")]
public void ConvertStringToValueInvalidCast2 ()
{
c.ConvertStringToValue ("-123", typeof (uint));
}
[Test]
[ExpectedException (typeof (FormatException))]
public void ConvertStringToValueInvalidCast4 ()
{
c.ConvertStringToValue ("123.45", typeof (int));
}
[Test]
public void ConvertStringToValueNullToValueType ()
{
// hmm, it passes.
Assert.AreEqual (default (char), c.ConvertStringToValue (null, typeof (char)));
}
[Test]
[ExpectedException (typeof (NotSupportedException))]
public void ConvertStringToValueDbNull ()
{
c.ConvertStringToValue (null, typeof (DBNull));
}
[Test]
public void ConvertStringToValueNullToString ()
{
Assert.IsNull (c.ConvertStringToValue (null, typeof (string)));
}
[Test]
public void ConvertStringToValue ()
{
Assert.AreEqual ('A', c.ConvertStringToValue ("A", typeof (char)), "#1");
Assert.AreEqual ("}}}", c.ConvertStringToValue ("}}}", typeof (string)), "#2");
Assert.AreEqual (123.0, c.ConvertStringToValue ("123.0", typeof (double)), "#3");
Assert.AreEqual (123.0, c.ConvertStringToValue ("123", typeof (double)), "#4");
}
// Types
public enum DemoEnum
{
Value1,
Value2,
Value3,
Value4,
}
[TypeConverter (typeof (MyTypeConverter))]
class MyConvertible
{
}
class MyTypeConverter : TypeConverter
{
public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof (string))
return "hogehoge";
throw new Exception ();
}
}
}
}

View File

@ -0,0 +1,280 @@
//
// WebHttpDispatchOperationSelectorTest.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
// Copyright 2011 Xamarin Inc (http://www.xamarin.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.
//
#if !MOBILE
using System;
using System.Globalization;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Web;
using System.Text;
using System.Xml;
using NUnit.Framework;
namespace MonoTests.System.ServiceModel.Dispatcher
{
[TestFixture]
public class WebHttpDispatchOperationSelectorTest
{
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ConstructorNullEndpoint ()
{
new WebHttpDispatchOperationSelector (null);
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void ConstructorEndpointNullAddress ()
{
ServiceEndpoint se = new ServiceEndpoint (ContractDescription.GetContract (typeof (MyService)));
new WebHttpDispatchOperationSelector (se);
}
#region SelectOperation
[Test]
public void SelectOperation ()
{
SelectOperationCore (Create ());
}
[Test]
public void SelectOperation2 ()
{
SelectOperationCore (Create2 ());
}
[Test]
public void SelectOperation3 ()
{
ContractDescription cd = ContractDescription.GetContract (typeof (MyService2));
Assert.IsNotNull (cd.Operations [0].Behaviors.Find<WebGetAttribute> (), "#1");
cd = ContractDescription.GetContract (typeof (MyService));
Assert.IsNull (cd.Operations [0].Behaviors.Find<WebGetAttribute> (), "#2");
}
void SelectOperationCore (MySelector d)
{
string name;
var msg = Message.CreateMessage (MessageVersion.Soap12, "http://temuri.org/MyService/Echo"); // looks like Version is not checked
Assert.IsNull (msg.Headers.To, "#1-0");
Assert.IsFalse (d.SelectOperation (ref msg, out name), "#1");
Assert.AreEqual ("", name, "#1-2");
Assert.IsNull (msg.Headers.To, "#1-3"); // no overwrite
Assert.IsFalse (msg.Properties.ContainsKey ("UriMatched"), "#1-4");
msg = Message.CreateMessage (MessageVersion.None, "http://temuri.org/MyService/Echo");
Assert.IsFalse (d.SelectOperation (ref msg, out name), "#2");
Assert.IsFalse (msg.Properties.ContainsKey ("UriMatched"), "#2-2");
msg = Message.CreateMessage (MessageVersion.None, "http://foobar.org/MyService/NonExistent");
Assert.IsFalse (d.SelectOperation (ref msg, out name), "#3");
Assert.AreEqual ("", name, "#3-2");
Assert.IsFalse (msg.Properties.ContainsKey ("UriMatched"), "#3-3");
// version and action do not matter
msg = Message.CreateMessage (MessageVersion.Soap12, "http://nonexistent.org/");
var http = new HttpRequestMessageProperty ();
// this mismatch is allowed. Lack of this value is OK.
// http.Method = "POST";
// this mismatch is allowed. Lack of this value is OK.
// http.QueryString = "foo=bar";
// this mismatch is allowed. Lack of this value is OK.
// http.Headers.Add ("Content-Type", "application/json");
// so, the http property can be empty, but is required.
msg.Properties.Add (HttpRequestMessageProperty.Name, http);
msg.Headers.To = new Uri ("http://localhost:8080/Echo?input=hoge");
Assert.IsTrue (d.SelectOperation (ref msg, out name), "#4");
// FIXME: hmm... isn'y "Echo" expected?
// Assert.AreEqual ("", name, "#4-2");
}
[Test]
[Category ("NotWorking")]
public void SelectOperationOnlyMessage ()
{
// This test shows strange result ... it adds UriMatched property while it does not really match the URI.
var d = Create ();
var msg = Message.CreateMessage (MessageVersion.None, "http://temuri.org/MyService/Echo"); // looks like Version is not checked
Assert.AreEqual ("", d.SelectOperation (ref msg), "#1");
Assert.IsTrue (msg.Properties.ContainsKey ("UriMatched"), "#1-2");
msg = Message.CreateMessage (MessageVersion.None, "http://foobar.org/MyService/NonExistent");
Assert.AreEqual ("", d.SelectOperation (ref msg), "#2");
Assert.IsNotNull (msg.Properties ["UriMatched"], "#2-2");
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void SelectOperationCheckExistingProperty ()
{
var d = Create ();
var msg = Message.CreateMessage (MessageVersion.None, "http://temuri.org/MyService/Echo"); // heh, I mistyped it and turned to prove that Action does not matter.
msg.Headers.To = new Uri ("http://localhost:8080/Echo");
// LAMESPEC: .NET does returns String.Empty, while we return the name of the operation (as IOperationSelector.SelectOperation is expected to do!)
// Assert.AreEqual (String.Empty, d.SelectOperation (ref msg), "#1");
d.SelectOperation (ref msg);
// The second invocation should raise the expected exception
d.SelectOperation (ref msg);
}
// Types
class MySelector : WebHttpDispatchOperationSelector
{
public MySelector (ServiceEndpoint se)
: base (se)
{
}
public bool SelectOperation (ref Message msg, out string name)
{
bool ret;
name = SelectOperation (ref msg, out ret);
return ret;
}
}
class MyBehavior : WebHttpBehavior
{
public WebHttpDispatchOperationSelector GetPublicOperationSelector (ServiceEndpoint se)
{
return GetOperationSelector (se);
}
protected override WebHttpDispatchOperationSelector GetOperationSelector (ServiceEndpoint se)
{
return new MySelector (se);
}
}
[ServiceContract]
public interface MyService
{
[OperationContract]
// [WebGet (UriTemplate = "MyService/Echo?input={input}")]
// [WebGet] // UriTemplate = "Echo?input={input}"
string Echo (string input);
}
[ServiceContract (Name = "MyService")]
public interface MyService2
{
[OperationContract (Name = "Echo")]
[WebGet] // UriTemplate = "Echo?input={input}"
string Echo (string input);
}
MySelector Create ()
{
ServiceEndpoint se = new ServiceEndpoint (ContractDescription.GetContract (typeof (MyService)));
se.Address = new EndpointAddress ("http://localhost:8080");
se.Contract.Operations [0].Behaviors.Add (new WebGetAttribute ());
return new MySelector (se);
//var b = new MyBehavior ();
//se.Behaviors.Add (b);
//return (MySelector) b.GetPublicOperationSelector (se);
}
MySelector Create2 ()
{
ServiceEndpoint se = new ServiceEndpoint (ContractDescription.GetContract (typeof (MyService2)));
se.Address = new EndpointAddress ("http://localhost:8080");
//se.Contract.Operations [0].Behaviors.Add (new WebGetAttribute ());
return new MySelector (se);
}
#endregion
#region "bug #656020"
[DataContract]
public class User
{
[DataMember]
public string Name { get; set; }
}
[ServiceContract]
interface IHello
{
[WebGet(UriTemplate = "{name}?age={age}&blah={blah}")]
[OperationContract]
string SayHi (string name, int age, string blah);
[WebInvoke(UriTemplate = "blah")]
[OperationContract]
string SayHi2 (User user);
}
class Hello : IHello
{
public string SayHi (string name, int age, string blah)
{
return string.Format ("Hi {0}: {1}, {2}", name, age, blah == null);
}
public string SayHi2 (User user)
{
return string.Format ("Hi {0}.", user.Name);
}
}
[Test]
public void WebMessageFormats ()
{
var host = new WebServiceHost (typeof (Hello));
host.AddServiceEndpoint (typeof (IHello), new WebHttpBinding (), "http://localhost:37564/");
host.Description.Behaviors.Find<ServiceDebugBehavior> ().IncludeExceptionDetailInFaults = true;
host.Open ();
try {
// run client
using (ChannelFactory<IHello> factory = new ChannelFactory<IHello> (new WebHttpBinding (), "http://localhost:37564/"))
{
factory.Endpoint.Behaviors.Add (new WebHttpBehavior ());
IHello h = factory.CreateChannel ();
//Console.WriteLine(h.SayHi("Joe", 42, null));
Assert.AreEqual ("Hi Joe.", h.SayHi2 (new User { Name = "Joe" }), "#1");
}
} finally {
host.Close ();
}
}
#endregion
}
}
#endif