#region Disclaimer / License // Copyright (C) 2010, Jackie Ng // http://trac.osgeo.org/mapguide/wiki/maestro, jumpinjackie@gmail.com // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // #endregion using System; using System.Collections.Generic; using System.Text; using NUnit.Framework; using OSGeo.MapGuide.MaestroAPI; using OSGeo.MapGuide.MaestroAPI.Services; using System.IO; using OSGeo.MapGuide.ObjectModels.Common; using OSGeo.MapGuide.ObjectModels; namespace MaestroAPITests { [TestFixture(Ignore = TestControl.IgnoreHttpConnectionTests)] public class HttpConnectionTests { //[Test] public void TestEncryptedFeatureSourceCredentials() { //Sensitive data redacted, nevertheless you can test and verify this by filling in the //blanks here and uncommenting the above [Test] attribute. If the test passes, credential encryption is working string server = ""; string database = ""; string actualUser = ""; string actualPass = ""; string bogusUser = "foo"; string bogusPass = "bar"; var conn = ConnectionUtil.CreateTestHttpConnection(); string fsId = "Library://UnitTests/EncryptedCredentials.FeatureSource"; var fs = ObjectFactory.CreateFeatureSource(conn, "OSGeo.SQLServerSpatial"); fs.SetConnectionProperty("Username", "%MG_USERNAME%"); fs.SetConnectionProperty("Password", "%MG_PASSWORD%"); fs.SetConnectionProperty("Service", server); fs.SetConnectionProperty("DataStore", database); fs.ResourceID = fsId; conn.ResourceService.SaveResource(fs); using (var ms = CredentialWriter.Write(actualUser, actualPass)) { conn.ResourceService.SetResourceData(fsId, "MG_USER_CREDENTIALS", ResourceDataType.String, ms); } string result = conn.FeatureService.TestConnection(fsId); Assert.AreEqual("TRUE", result.ToUpper()); //Test convenience method fsId = "Library://UnitTests/EncryptedCredentials2.FeatureSource"; fs = ObjectFactory.CreateFeatureSource(conn, "OSGeo.SQLServerSpatial"); fs.SetConnectionProperty("Username", "%MG_USERNAME%"); fs.SetConnectionProperty("Password", "%MG_PASSWORD%"); fs.SetConnectionProperty("Service", server); fs.SetConnectionProperty("DataStore", database); fs.ResourceID = fsId; conn.ResourceService.SaveResource(fs); fs.SetEncryptedCredentials(actualUser, actualPass); result = conn.FeatureService.TestConnection(fsId); Assert.AreEqual("TRUE", result.ToUpper()); Assert.AreEqual(actualUser, fs.GetEncryptedUsername()); //Do not set encrypted credentials fsId = "Library://UnitTests/EncryptedCredentials3.FeatureSource"; fs = ObjectFactory.CreateFeatureSource(conn, "OSGeo.SQLServerSpatial"); fs.SetConnectionProperty("Username", "%MG_USERNAME%"); fs.SetConnectionProperty("Password", "%MG_PASSWORD%"); fs.SetConnectionProperty("Service", server); fs.SetConnectionProperty("DataStore", database); fs.ResourceID = fsId; conn.ResourceService.SaveResource(fs); try { result = conn.FeatureService.TestConnection(fsId); Assert.AreEqual("FALSE", result.ToUpper()); } catch //Exception or false I can't remember, as long as the result is not "true" { } //Encrypt credentials, but use bogus username/password fsId = "Library://UnitTests/EncryptedCredentials4.FeatureSource"; fs = ObjectFactory.CreateFeatureSource(conn, "OSGeo.SQLServerSpatial"); fs.SetConnectionProperty("Username", "%MG_USERNAME%"); fs.SetConnectionProperty("Password", "%MG_PASSWORD%"); fs.SetConnectionProperty("Service", server); fs.SetConnectionProperty("DataStore", database); fs.ResourceID = fsId; conn.ResourceService.SaveResource(fs); fs.SetEncryptedCredentials(bogusUser, bogusPass); try { result = conn.FeatureService.TestConnection(fsId); Assert.AreEqual("FALSE", result.ToUpper()); } catch { //Exception or false I can't remember, as long as the result is not "true" } Assert.AreEqual(bogusUser, fs.GetEncryptedUsername()); } [Test] public void TestConnectionString() { System.Data.Common.DbConnectionStringBuilder builder = new System.Data.Common.DbConnectionStringBuilder(); builder["Foo"] = "sdfjkg"; builder["Bar"] = "skgjuksdf"; builder["Snafu"] = "asjdgjh;sdgj"; //Note the ; in the value builder["Whatever"] = "asjd=gjh;sdgj"; //Note the ; and = in the value var values = ConnectionProviderRegistry.ParseConnectionString(builder.ToString()); Assert.AreEqual(values.Count, 4); Assert.AreEqual(builder["Foo"].ToString(), values["Foo"]); Assert.AreEqual(builder["Bar"].ToString(), values["Bar"]); Assert.AreEqual(builder["Snafu"].ToString(), values["Snafu"]); Assert.AreEqual(builder["Whatever"].ToString(), values["Whatever"]); } [Test] public void TestCustomProperties() { var builder = new RequestBuilder(new Uri("http://tempuri.org"), "en"); var conn = new HttpServerConnection(builder); //Work through the interface IServerConnection isvc = (IServerConnection)conn; //UserAgent is exposed as a custom property var props = isvc.GetCustomPropertyNames(); Assert.IsNotNull(props); Assert.AreEqual(props.Length, 2); Assert.IsTrue(Array.IndexOf(props, HttpServerConnection.PROP_USER_AGENT) >= 0); Assert.IsTrue(Array.IndexOf(props, HttpServerConnection.PROP_BASE_URL) >= 0); //It is of type string var type = isvc.GetCustomPropertyType(HttpServerConnection.PROP_USER_AGENT); Assert.AreEqual(type, typeof(string)); type = isvc.GetCustomPropertyType(HttpServerConnection.PROP_BASE_URL); Assert.AreEqual(type, typeof(string)); //We can set and get it isvc.SetCustomProperty(HttpServerConnection.PROP_USER_AGENT, "MapGuide Maestro API Unit Test Fixture"); var agent = (string)isvc.GetCustomProperty(HttpServerConnection.PROP_USER_AGENT); Assert.AreEqual(agent, "MapGuide Maestro API Unit Test Fixture"); //BaseUrl is read-only try { isvc.SetCustomProperty(HttpServerConnection.PROP_BASE_URL, "http://mylocalhost/mapguide"); Assert.Fail("Should've thrown exception"); } catch { } } [Test] public void TestServiceCapabilities() { var builder = new RequestBuilder(new Uri("http://tempuri.org"), "en"); var conn = new HttpServerConnection(builder); conn.SetSiteVersion(new Version(1, 2, 0)); //Work through the interface IServerConnection isvc = (IServerConnection)conn; int[] stypes = isvc.Capabilities.SupportedServices; foreach (int st in stypes) { try { IService svc = isvc.GetService(st); } catch { Assert.Fail("Supported service type mismatch"); } } conn.SetSiteVersion(new Version(2, 0, 0)); stypes = isvc.Capabilities.SupportedServices; foreach (int st in stypes) { try { IService svc = isvc.GetService(st); } catch { Assert.Fail("Supported service type mismatch"); } } } [Test] public void TestTouch() { var conn = ConnectionUtil.CreateTestHttpConnection(); var resSvc = conn.ResourceService; if (!resSvc.ResourceExists("Library://UnitTests/Data/HydrographicPolygons.FeatureSource")) resSvc.SetResourceXmlData("Library://UnitTests/Data/HydrographicPolygons.FeatureSource", File.OpenRead("TestData/MappingService/UT_HydrographicPolygons.fs")); resSvc.Touch("Library://UnitTests/Data/HydrographicPolygons.FeatureSource"); } [Test] public void TestAnyStreamInput() { string source = "Library://UnitTests/Data/HydrographicPolygons.FeatureSource"; string target = "Library://UnitTests/Data/TestAnyStreamInput.FeatureSource"; var conn = ConnectionUtil.CreateTestHttpConnection(); var resSvc = conn.ResourceService; if (!resSvc.ResourceExists(source)) resSvc.SetResourceXmlData(source, File.OpenRead("TestData/MappingService/UT_HydrographicPolygons.fs")); resSvc.SetResourceXmlData(target, resSvc.GetResourceXmlData(source)); string dataName = "UT_HydrographicPolygons.sdf"; var resDataList = resSvc.EnumerateResourceData(source); if (resDataList.ResourceData.Count == 1) dataName = resDataList.ResourceData[0].Name; else resSvc.SetResourceData(source, dataName, ResourceDataType.File, File.OpenRead("TestData/MappingService/UT_HydrographicPolygons.sdf")); resSvc.SetResourceData(target, dataName, ResourceDataType.File, resSvc.GetResourceData(source, dataName)); } } }