123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Reflection;
- using System.Text;
- using log4net;
- using Nini.Config;
- using NUnit.Framework;
- using OpenMetaverse;
- using OpenSim.Framework;
- using OpenSim.Framework.Servers;
- using OpenSim.Framework.Servers.HttpServer;
- using OpenSim.Region.CoreModules.Scripting.LSLHttp;
- using OpenSim.Region.Framework.Scenes;
- using OpenSim.Region.ScriptEngine.Shared;
- using OpenSim.Region.ScriptEngine.Shared.Api;
- using OpenSim.Region.ScriptEngine.Shared.ScriptBase;
- using OpenSim.Services.Interfaces;
- using OpenSim.Tests.Common;
- namespace OpenSim.Region.ScriptEngine.Shared.Tests
- {
-
-
-
- [TestFixture]
- public class LSL_ApiHttpTests : OpenSimTestCase
- {
- private Scene m_scene;
- private MockScriptEngine m_engine;
- private UrlModule m_urlModule;
- private TaskInventoryItem m_scriptItem;
- private LSL_Api m_lslApi;
- [TestFixtureSetUp]
- public void TestFixtureSetUp()
- {
-
- Util.FireAndForgetMethod = FireAndForgetMethod.RegressionTest;
- }
- [TestFixtureTearDown]
- public void TestFixureTearDown()
- {
-
-
-
- Util.FireAndForgetMethod = Util.DefaultFireAndForgetMethod;
- }
- [SetUp]
- public override void SetUp()
- {
- base.SetUp();
-
-
- uint port = 9999;
- MainServer.RemoveHttpServer(port);
- BaseHttpServer server = new BaseHttpServer(port, false, 0, "");
- MainServer.AddHttpServer(server);
- MainServer.Instance = server;
- server.Start();
- m_engine = new MockScriptEngine();
- m_urlModule = new UrlModule();
- m_scene = new SceneHelpers().SetupScene();
- SceneHelpers.SetupSceneModules(m_scene, new IniConfigSource(), m_engine, m_urlModule);
- SceneObjectGroup so = SceneHelpers.AddSceneObject(m_scene);
- m_scriptItem = TaskInventoryHelpers.AddScript(m_scene.AssetService, so.RootPart);
-
-
- m_lslApi = new LSL_Api();
- m_lslApi.Initialize(m_engine, so.RootPart, m_scriptItem, null);
- }
- [TearDown]
- public void TearDown()
- {
- MainServer.Instance.Stop();
- }
- [Test]
- public void TestLlReleaseUrl()
- {
- TestHelpers.InMethod();
- m_lslApi.llRequestURL();
- string returnedUri = m_engine.PostedEvents[m_scriptItem.ItemID][0].Params[2].ToString();
- {
-
- Assert.That(m_lslApi.llGetFreeURLs().value, Is.EqualTo(m_urlModule.TotalUrls - 1));
- }
- {
-
- m_lslApi.llReleaseURL("GARBAGE");
- Assert.That(m_lslApi.llGetFreeURLs().value, Is.EqualTo(m_urlModule.TotalUrls - 1));
- }
- {
-
- m_lslApi.llReleaseURL("http://example.com");
- Assert.That(m_lslApi.llGetFreeURLs().value, Is.EqualTo(m_urlModule.TotalUrls - 1));
- }
- {
-
- m_lslApi.llReleaseURL(returnedUri);
- Assert.That(m_lslApi.llGetFreeURLs().value, Is.EqualTo(m_urlModule.TotalUrls));
- HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(returnedUri);
- bool gotExpectedException = false;
- try
- {
- using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
- {}
- }
- catch (WebException e)
- {
- using (HttpWebResponse response = (HttpWebResponse)e.Response)
- gotExpectedException = response.StatusCode == HttpStatusCode.NotFound;
- }
- Assert.That(gotExpectedException, Is.True);
- }
- {
-
- m_lslApi.llReleaseURL(returnedUri);
- Assert.That(m_lslApi.llGetFreeURLs().value, Is.EqualTo(m_urlModule.TotalUrls));
- }
- }
- [Test]
- public void TestLlRequestUrl()
- {
- TestHelpers.InMethod();
- string requestId = m_lslApi.llRequestURL();
- Assert.That(requestId, Is.Not.EqualTo(UUID.Zero.ToString()));
- string returnedUri;
- {
-
- Assert.That(m_lslApi.llGetFreeURLs().value, Is.EqualTo(m_urlModule.TotalUrls - 1));
- Assert.That(m_engine.PostedEvents.ContainsKey(m_scriptItem.ItemID));
- List<EventParams> events = m_engine.PostedEvents[m_scriptItem.ItemID];
- Assert.That(events.Count, Is.EqualTo(1));
- EventParams eventParams = events[0];
- Assert.That(eventParams.EventName, Is.EqualTo("http_request"));
- UUID returnKey;
- string rawReturnKey = eventParams.Params[0].ToString();
- string method = eventParams.Params[1].ToString();
- returnedUri = eventParams.Params[2].ToString();
- Assert.That(UUID.TryParse(rawReturnKey, out returnKey), Is.True);
- Assert.That(method, Is.EqualTo(ScriptBaseClass.URL_REQUEST_GRANTED));
- Assert.That(Uri.IsWellFormedUriString(returnedUri, UriKind.Absolute), Is.True);
- }
- {
-
- string testResponse = "Hello World";
- m_engine.ClearPostedEvents();
- m_engine.PostEventHook
- += (itemId, evp) => m_lslApi.llHTTPResponse(evp.Params[0].ToString(), 200, testResponse);
- AssertHttpResponse(returnedUri, testResponse);
- Assert.That(m_engine.PostedEvents.ContainsKey(m_scriptItem.ItemID));
- List<EventParams> events = m_engine.PostedEvents[m_scriptItem.ItemID];
- Assert.That(events.Count, Is.EqualTo(1));
- EventParams eventParams = events[0];
- Assert.That(eventParams.EventName, Is.EqualTo("http_request"));
- UUID returnKey;
- string rawReturnKey = eventParams.Params[0].ToString();
- string method = eventParams.Params[1].ToString();
- string body = eventParams.Params[2].ToString();
- Assert.That(UUID.TryParse(rawReturnKey, out returnKey), Is.True);
- Assert.That(method, Is.EqualTo("GET"));
- Assert.That(body, Is.EqualTo(""));
- }
- }
- private void AssertHttpResponse(string uri, string expectedResponse)
- {
- HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);
- using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
- {
- using (Stream stream = webResponse.GetResponseStream())
- {
- using (StreamReader reader = new StreamReader(stream))
- {
- Assert.That(reader.ReadToEnd(), Is.EqualTo(expectedResponse));
- }
- }
- }
- }
- }
- }
|