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

POSTに同様のテスト方法を実装したNateTaylor( http://taylonr.com/integration-testing-openrasta )の助けを借りて解決しました。IISでホストされているプロジェクトのURIであるURIにソリューション名「PoppyService」を含めていました。構成によってInMemoryHostに対してもこのURIが作成されると誤って想定しましたが、ローカルホストに直接ルーティングされます。これを削除すると、アサートテストが成功します。

[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のテストメソッドも実装する必要があるので、投稿も良い発見です。ダニエルはMyResourceの意味を具体的に説明していなかったので、これが他の人にも役立つことを願っています。今私には明らかです。

于 2012-12-03T17:09:08.720 に答える