0

我已经完成了我的第一个 OpenRasta RESTful Web 服务的实现,并成功获得了我希望工作的 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);
            }
        }
    }
}

}

如果我在浏览器中手动导航到 Uri,我会得到正确的响应和 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();
        }
    }
}

任何建议都会受到欢迎。

4

1 回答 1

0

我在 Nate Taylor ( http://taylonr.com/integration-testing-openrasta ) 的帮助下解决了这个问题,他为 POST 实施了类似的测试方法。我在 Uri 中包含了解决方案名称“PoppyService”,这是托管在 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 是什么意思。现在对我来说很明显。

于 2012-12-03T17:09:08.720 回答