From 40e95cab02b125a6c9ec7e90cab94c94b201f021 Mon Sep 17 00:00:00 2001 From: lbsa71 Date: Wed, 13 May 2009 17:11:53 +0000 Subject: * Added some more tests to the GetAssetStreamHandlers --- .../Framework/Servers/BaseGetAssetStreamHandler.cs | 43 +++++++++++-------- .../Framework/Servers/HttpServer/OSHttpResponse.cs | 4 +- .../Tests/CachedGetAssetStreamHandlerTests.cs | 37 ++++++++++++---- .../Servers/Tests/GetAssetStreamHandlerTests.cs | 49 ++++++++++++++++++---- 4 files changed, 99 insertions(+), 34 deletions(-) (limited to 'OpenSim/Framework') diff --git a/OpenSim/Framework/Servers/BaseGetAssetStreamHandler.cs b/OpenSim/Framework/Servers/BaseGetAssetStreamHandler.cs index fc4eed7..9eacf24 100644 --- a/OpenSim/Framework/Servers/BaseGetAssetStreamHandler.cs +++ b/OpenSim/Framework/Servers/BaseGetAssetStreamHandler.cs @@ -60,7 +60,7 @@ namespace OpenSim.Framework.Servers if (p.Length > 0) { - UUID assetID = UUID.Zero; + UUID assetID; if (!UUID.TryParse(p[0], out assetID)) { @@ -70,16 +70,14 @@ namespace OpenSim.Framework.Servers } if (StatsManager.AssetStats != null) + { StatsManager.AssetStats.AddRequest(); + } AssetBase asset = GetAsset(assetID); if (asset != null) { -// if (asset.ContainsReferences) -// { -// asset.Data = ProcessOutgoingAssetData(asset.Data); -// } if (p.Length > 1 && p[1] == "data") { httpResponse.StatusCode = (int)HttpStatusCode.OK; @@ -88,25 +86,15 @@ namespace OpenSim.Framework.Servers } else { - XmlSerializer xs = new XmlSerializer(typeof(AssetBase)); - MemoryStream ms = new MemoryStream(); - XmlTextWriter xw = new XmlTextWriter(ms, Encoding.UTF8); - xw.Formatting = Formatting.Indented; - xs.Serialize(xw, asset); - xw.Flush(); - - ms.Seek(0, SeekOrigin.Begin); - //StreamReader sr = new StreamReader(ms); - - result = ms.GetBuffer(); - - Array.Resize(ref result, (int)ms.Length); + result = GetXml(asset); } } else { if (StatsManager.AssetStats != null) + { StatsManager.AssetStats.AddNotFoundRequest(); + } m_log.InfoFormat("[REST]: GET:/asset failed to find {0}", assetID); } @@ -115,6 +103,25 @@ namespace OpenSim.Framework.Servers return result; } + public static byte[] GetXml(AssetBase asset) + { + byte[] result; + XmlSerializer xs = new XmlSerializer(typeof(AssetBase)); + MemoryStream ms = new MemoryStream(); + XmlTextWriter xw = new XmlTextWriter(ms, Encoding.UTF8); + xw.Formatting = Formatting.Indented; + xs.Serialize(xw, asset); + xw.Flush(); + + ms.Seek(0, SeekOrigin.Begin); + //StreamReader sr = new StreamReader(ms); + + result = ms.GetBuffer(); + + Array.Resize(ref result, (int)ms.Length); + return result; + } + public string ProcessAssetDataString(string data) { Regex regex = new Regex("(creator_id|owner_id)\\s+(\\S+)"); diff --git a/OpenSim/Framework/Servers/HttpServer/OSHttpResponse.cs b/OpenSim/Framework/Servers/HttpServer/OSHttpResponse.cs index 210d122..6ea95b1 100644 --- a/OpenSim/Framework/Servers/HttpServer/OSHttpResponse.cs +++ b/OpenSim/Framework/Servers/HttpServer/OSHttpResponse.cs @@ -45,7 +45,7 @@ namespace OpenSim.Framework.Servers.HttpServer /// Setting this property will also set IsContentTypeSet to /// true. /// - public string ContentType + public virtual string ContentType { get { @@ -226,7 +226,7 @@ namespace OpenSim.Framework.Servers.HttpServer /// /// HTTP status code. /// - public int StatusCode + public virtual int StatusCode { get { diff --git a/OpenSim/Framework/Servers/Tests/CachedGetAssetStreamHandlerTests.cs b/OpenSim/Framework/Servers/Tests/CachedGetAssetStreamHandlerTests.cs index dbb877d..b3cccfd 100644 --- a/OpenSim/Framework/Servers/Tests/CachedGetAssetStreamHandlerTests.cs +++ b/OpenSim/Framework/Servers/Tests/CachedGetAssetStreamHandlerTests.cs @@ -4,7 +4,10 @@ using System.Text; using NUnit.Framework; using OpenSim.Data; using OpenSim.Framework.Servers.HttpServer; +using OpenSim.Services.Interfaces; using OpenSim.Tests.Common; +using OpenSim.Tests.Common.Mock; +using OpenSim.Tests.Common.Setup; namespace OpenSim.Framework.Servers.Tests { @@ -59,14 +62,34 @@ namespace OpenSim.Framework.Servers.Tests BaseRequestHandlerTestHelper.BaseTestHandleMalformedGuid(handler, ASSETS_PATH); } - //[Test] - //public void TestHandleFetchMissingAsset() - //{ + [Test] + public void TestHandleFetchMissingAsset() + { + IAssetCache assetCache = new TestAssetCache(); + CachedGetAssetStreamHandler handler = new CachedGetAssetStreamHandler(assetCache); + + GetAssetStreamHandlerTestHelpers.BaseFetchMissingAsset(handler); + } + + [Test] + public void TestHandleFetchExistingAssetData() + { + CachedGetAssetStreamHandler handler; + OSHttpResponse response; + AssetBase asset = CreateTestEnvironment(out handler, out response); + + GetAssetStreamHandlerTestHelpers.BaseFetchExistingAssetDataTest(asset, handler, response); + } - // byte[] emptyResult = new byte[] { }; - // CachedGetAssetStreamHandler handler = new CachedGetAssetStreamHandler(null); + private static AssetBase CreateTestEnvironment(out CachedGetAssetStreamHandler handler, out OSHttpResponse response) + { + AssetBase asset = GetAssetStreamHandlerTestHelpers.CreateCommonTestResources(out response); + + IAssetCache assetDataPlugin = new TestAssetCache(); + handler = new CachedGetAssetStreamHandler(assetDataPlugin); - // Assert.AreEqual(new string[] { }, handler.Handle("/assets/badGuid", null, null, null), "Failed on bad guid."); - //} + assetDataPlugin.AddAsset(asset); + return asset; + } } } diff --git a/OpenSim/Framework/Servers/Tests/GetAssetStreamHandlerTests.cs b/OpenSim/Framework/Servers/Tests/GetAssetStreamHandlerTests.cs index 091a944..51dd79d 100644 --- a/OpenSim/Framework/Servers/Tests/GetAssetStreamHandlerTests.cs +++ b/OpenSim/Framework/Servers/Tests/GetAssetStreamHandlerTests.cs @@ -1,10 +1,14 @@ using System; using System.Collections.Generic; +using System.Net; using System.Text; +using HttpServer; using NUnit.Framework; using OpenSim.Data; using OpenSim.Framework.Servers.HttpServer; using OpenSim.Tests.Common; +using OpenSim.Tests.Common.Mock; +using OpenSim.Tests.Common.Setup; namespace OpenSim.Framework.Servers.Tests { @@ -59,13 +63,44 @@ namespace OpenSim.Framework.Servers.Tests BaseRequestHandlerTestHelper.BaseTestHandleMalformedGuid(handler, ASSETS_PATH); } - //[Test] - //public void TestHandleFetchMissingAsset() - //{ - // byte[] emptyResult = new byte[] { }; - // GetAssetStreamHandler handler = new GetAssetStreamHandler(null); + [Test] + public void TestHandleFetchMissingAsset() + { + IAssetDataPlugin assetDataPlugin = new TestAssetDataPlugin(); + GetAssetStreamHandler handler = new GetAssetStreamHandler(assetDataPlugin); + + GetAssetStreamHandlerTestHelpers.BaseFetchMissingAsset(handler); + } + + [Test] + public void TestHandleFetchExistingAssetData() + { + GetAssetStreamHandler handler; + OSHttpResponse response; + AssetBase asset = CreateTestEnvironment(out handler, out response); + + GetAssetStreamHandlerTestHelpers.BaseFetchExistingAssetDataTest(asset, handler, response); + } - // Assert.AreEqual(new string[] { }, handler.Handle("/assets/badGuid", null, null, null), "Failed on bad guid."); - //} + [Test] + public void TestHandleFetchExistingAssetXml() + { + GetAssetStreamHandler handler; + OSHttpResponse response; + AssetBase asset = CreateTestEnvironment(out handler, out response); + + GetAssetStreamHandlerTestHelpers.BaseFetchExistingAssetXmlTest(asset, handler, response); + } + + private static AssetBase CreateTestEnvironment(out GetAssetStreamHandler handler, out OSHttpResponse response) + { + AssetBase asset = GetAssetStreamHandlerTestHelpers.CreateCommonTestResources(out response); + + IAssetDataPlugin assetDataPlugin = new TestAssetDataPlugin(); + handler = new GetAssetStreamHandler(assetDataPlugin); + + assetDataPlugin.CreateAsset(asset); + return asset; + } } } -- cgit v1.1