GetAssetStreamHandlerTestHelpers.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.IO;
  30. using System.Net;
  31. using System.Text;
  32. using System.Xml;
  33. using System.Xml.Serialization;
  34. using NUnit.Framework;
  35. using OpenMetaverse;
  36. using OpenSim.Framework;
  37. using OpenSim.Framework.Servers;
  38. using OpenSim.Framework.Servers.HttpServer;
  39. using OpenSim.Server.Base;
  40. using OpenSim.Tests.Common.Mock;
  41. namespace OpenSim.Tests.Common.Setup
  42. {
  43. public class GetAssetStreamHandlerTestHelpers
  44. {
  45. private const string EXPECTED_CONTENT_TYPE = "application/x-metaverse-callingcard";
  46. public static void BaseFetchExistingAssetXmlTest(AssetBase asset, BaseGetAssetStreamHandler handler, OSHttpResponse response)
  47. {
  48. byte[] expected = BaseGetAssetStreamHandler.GetXml(asset);
  49. byte[] actual = handler.Handle("/assets/" + asset.ID , null, null, response);
  50. Assert.Greater(actual.Length, 10, "Too short xml on fetching xml without trailing slash.");
  51. Assert.AreEqual(expected, actual, "Failed on fetching xml without trailing slash.");
  52. // Assert.AreEqual((int)HttpStatusCode.OK, response.StatusCode, "Wrong http response code on first fetch.");
  53. actual = handler.Handle("/assets/" + asset.ID + "/", null, null, response);
  54. Assert.Greater(actual.Length, 10, "Too short xml on fetching xml with trailing slash.");
  55. Assert.AreEqual(expected, actual, "Failed on fetching xml with trailing slash.");
  56. // Assert.AreEqual((int)HttpStatusCode.OK, response.StatusCode, "Wrong http response code on second fetch.");
  57. actual = handler.Handle("/assets/" + asset.ID + "/badData", null, null, response);
  58. Assert.Greater(actual.Length, 10, "Too short xml on fetching xml with bad trailing data.");
  59. Assert.AreEqual(expected, actual, "Failed on fetching xml with bad trailing trailing slash.");
  60. // Assert.AreEqual((int)HttpStatusCode.OK, response.StatusCode, "Wrong http response code on second fetch.");
  61. }
  62. public static void BaseFetchExistingAssetDataTest(AssetBase asset, BaseGetAssetStreamHandler handler, OSHttpResponse response)
  63. {
  64. Assert.AreEqual(asset.Data, handler.Handle("/assets/" + asset.ID + "/data", null, null, response), "Failed on fetching data without trailing slash.");
  65. Assert.AreEqual((int)HttpStatusCode.OK, response.StatusCode, "Wrong http response code on first fetch.");
  66. Assert.AreEqual(EXPECTED_CONTENT_TYPE, response.ContentType, "Wrong http content type on first fetch.");
  67. Assert.AreEqual(asset.Data, handler.Handle("/assets/" + asset.ID + "/data/", null, null, response), "Failed on fetching data with trailing slash.");
  68. Assert.AreEqual((int)HttpStatusCode.OK, response.StatusCode, "Wrong http response code on second fetch.");
  69. Assert.AreEqual(EXPECTED_CONTENT_TYPE, response.ContentType, "Wrong http content type on second fetch.");
  70. }
  71. public static void BaseFetchExistingAssetMetaDataTest(AssetBase asset, BaseGetAssetStreamHandler handler, OSHttpResponse response)
  72. {
  73. XmlSerializer xs = new XmlSerializer(typeof(AssetMetadata));
  74. byte[] expected = ServerUtils.SerializeResult(xs, asset.Metadata);
  75. Assert.AreEqual(expected, handler.Handle("/assets/" + asset.ID + "/metadata", null, null, response), "Failed on fetching data without trailing slash.");
  76. Assert.AreEqual((int)HttpStatusCode.OK, response.StatusCode, "Wrong http response code on first fetch.");
  77. Assert.AreEqual(EXPECTED_CONTENT_TYPE, response.ContentType, "Wrong http content type on first fetch.");
  78. Assert.AreEqual(expected, handler.Handle("/assets/" + asset.ID + "/metadata/", null, null, response), "Failed on fetching data with trailing slash.");
  79. Assert.AreEqual((int)HttpStatusCode.OK, response.StatusCode, "Wrong http response code on second fetch.");
  80. Assert.AreEqual(EXPECTED_CONTENT_TYPE, response.ContentType, "Wrong http content type on second fetch.");
  81. }
  82. public static AssetBase CreateCommonTestResources(out OSHttpResponse response)
  83. {
  84. AssetBase asset = CreateTestAsset();
  85. response = new TestOSHttpResponse();
  86. return asset;
  87. }
  88. public static AssetBase CreateTestAsset()
  89. {
  90. byte[] expected = new byte[] { 1,2,3 };
  91. AssetBase asset = new AssetBase();
  92. asset.ID = Guid.NewGuid().ToString();
  93. asset.Data = expected;
  94. asset.Type = 2;
  95. return asset;
  96. }
  97. public static void BaseFetchMissingAsset(BaseGetAssetStreamHandler handler, OSHttpResponse response)
  98. {
  99. Assert.AreEqual(
  100. BaseRequestHandlerTestHelper.EmptyByteArray,
  101. handler.Handle("/assets/" + Guid.NewGuid(), null, null, response), "Failed on bad guid.");
  102. Assert.AreEqual((int)HttpStatusCode.NotFound, response.StatusCode, "Response code wrong in BaseFetchMissingAsset");
  103. }
  104. }
  105. }