2012-12-03 37 views
0

我完成了我的第一個OpenRasta RESTful web服務的實現,併成功獲取了希望工作的GET請求。OpenRasta單元測試404錯誤中的GET結果

因此,我從Daniel Irvine的http://danielirvine.com/blog/2011/06/08/testing-restful-services-with-openrasta/的帖子中獲得了一些「靈感」,以構建一個自動化測試項目來測試實現。

我已經創建了自己的測試類,但是我經常收到404錯誤作爲響應狀態碼。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using OpenRasta.Hosting.InMemory; 
using PoppyService; 
using OpenRasta.Web; 
using System.IO; 
using System.Runtime.Serialization.Json; 
using Microsoft.VisualStudio.TestTools.UnitTesting; 
using NUnit.Framework; 

namespace PoppyServiceTests 
{ 
//http://danielirvine.com/blog/2011/06/08/testing-restful-services-with-openrasta/ 
[TestFixture] 
class OpenRastaJSONTestMehods 
{ 
    [TestCase("http://localhost/PoppyService/users")] 
    public static void GET(string uri) 
    { 
     const string PoppyLocalHost = "http://localhost/PoppyService/"; 
     if (uri.Contains(PoppyLocalHost)) 
      GET(new Uri(uri)); 
     else 
      throw new UriFormatException(string.Format("The uri doesn't contain {0}", PoppyLocalHost)); 
    } 
    [Test] 
    public static void GET(Uri serviceuri) 
    { 
     using (var host = new InMemoryHost(new Configuration())) 
     { 
      var request = new InMemoryRequest() 
      { 
       Uri = serviceuri, 
       HttpMethod = "GET" 
      }; 

      // set up your code formats - I'm using 
      // JSON because it's awesome 
      request.Entity.ContentType = MediaType.Json; 
      request.Entity.Headers["Accept"] = "application/json"; 

      // send the request and save the resulting response 
      var response = host.ProcessRequest(request); 
      int statusCode = response.StatusCode; 

      NUnit.Framework.Assert.AreEqual(200, statusCode, string.Format("Http StatusCode Error: {0}", statusCode)); 

      // deserialize the content from the response 
      object returnedObject; 
      if (response.Entity.ContentLength > 0) 
      { 
       // you must rewind the stream, as OpenRasta  
       // won't do this for you  
       response.Entity.Stream.Seek(0, SeekOrigin.Begin); 
       var serializer = new DataContractJsonSerializer(typeof(object)); 
       returnedObject = serializer.ReadObject(response.Entity.Stream); 
      } 
     } 
    } 
} 

}

如果我瀏覽到烏里手動在瀏覽器中我得到了正確的反響,HTTP 200

這可能是與我的配置類,但如果我再次嘗試所有的Uris,我得到了正確的結果。

public class Configuration : IConfigurationSource 
{ 
    public void Configure() 
    { 
     using (OpenRastaConfiguration.Manual) 
     {     
      ResourceSpace.Has.ResourcesOfType<TestPageResource>() 
       .AtUri("/testpage").HandledBy<TestPageHandler>().RenderedByAspx("~/Views/DummyView.aspx"); 

      ResourceSpace.Has.ResourcesOfType<IList<AppUser>>() 
       .AtUri("/users").And 
       .AtUri("/user/{appuserid}").HandledBy<UserHandler>().AsJsonDataContract(); 

      ResourceSpace.Has.ResourcesOfType<AuthenticationResult>() 
       .AtUri("/user").HandledBy<UserHandler>().AsJsonDataContract(); 

      ResourceSpace.Has.ResourcesOfType<IList<Client>>() 
       .AtUri("/clients").And 
       .AtUri("/client/{clientid}").HandledBy<ClientsHandler>().AsJsonDataContract(); 

      ResourceSpace.Has.ResourcesOfType<IList<Agency>>() 
       .AtUri("/agencies").And 
       .AtUri("/agency/{agencyid}").HandledBy<AgencyHandler>().AsJsonDataContract(); 

      ResourceSpace.Has.ResourcesOfType<IList<ClientApps>>() 
       .AtUri("/clientapps/{appid}").HandledBy<ClientAppsHandler>().AsJsonDataContract(); 

      ResourceSpace.Has.ResourcesOfType<Client>() 
       .AtUri("/agencyclients").And 
       .AtUri("/agencyclients/{agencyid}").HandledBy<AgencyClientsHandler>().AsJsonDataContract(); 

      ResourceSpace.Has.ResourcesOfType<Client>() 
       .AtUri("/agencyplususerclients/{appuserid}").HandledBy<AgencyPlusUserClientsHandler>().AsJsonDataContract(); 

      ResourceSpace.Has.ResourcesOfType<IList<Permission>>() 
       .AtUri("/permissions/{appuserid}/{appid}").HandledBy<PermissionsHandler>().AsJsonDataContract(); 

      ResourceSpace.Has.ResourcesOfType<IList<Role>>() 
       .AtUri("/roles").And 
       .AtUri("/roles/{appuserid}").And.AtUri("/roles/{appuserid}/{appid}").HandledBy<RolesHandler>().AsJsonDataContract(); 

      ResourceSpace.Has.ResourcesOfType<IList<AppVersion>>() 
       .AtUri("/userappversion").And 
       .AtUri("/userappversion/{appuserid}").HandledBy<UserAppVersionHandler>().AsJsonDataContract(); 
     } 
    } 
} 

任何建議都會很好地收到。

回答

0

我已經在Nate Taylor(http://taylonr.com/integration-testing-openrasta)的幫助下解決了這個問題,他爲POST實現了一個類似的測試方法。我在Uri中包含解決方案名稱'PoppyService',Uri是託管在IIS上的項目的Uri。我錯誤地認爲配置會爲InMemoryHost創建此Uri,但它會直接路由到localhost。取消這個使得Assert測試成功。

[TestCase("http://localhost/users")] 
    public static void GET(string uri) 
    { 
     const string LocalHost = "http://localhost/"; 
     if (uri.Contains(LocalHost)) 
      GET(new Uri(uri)); 
     else 
      throw new UriFormatException(string.Format("The uri doesn't contain {0}", LocalHost)); 
    } 

該帖子也是一個很好的發現,因爲我也需要爲POST實現一個測試方法。希望這也能幫助別人,因爲Daniel沒有具體解釋他的MyResource意味着什麼。現在對我來說很明顯。