Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

c# - Unit Test HTTPRequest Headers with ServiceStack

I have this Service:

public class PlayerService : Service
{
    public IPlayerAppService PlayerAppService { get; set; }

    public PlayerService (IPlayerAppService service)
    {
        if (service == null)
            throw new ArgumentException ("Service null");

        PlayerAppService = service;
    }

    public object Post (PlayerDTO request)
    {
        var newPlayer = new PlayerResponse ()
        {
            Player = PlayerAppService.SendPlayerLocation(request.Position.Latitude, request.Position.Longitude)
        };

        return new HttpResult (newPlayer)
        {
            StatusCode = System.Net.HttpStatusCode.Created,
            Headers =
            {
                { HttpHeaders.Location, base.Request.AbsoluteUri.CombineWith(newPlayer.Player.Id.ToString()) }
            }
        };
    }
}

I've manually verified that the Location and the Response looks correct from my deployments of this service. I would like to figure out how to unit test this though. I wrote a test like this:

[TestFixture]
public class PlayerServiceTests
{
    AppHost appHost;

    [TestFixtureSetUp]
    public void TestFixtureSetUp ()
    {
        appHost = new AppHost ();
        appHost.Init ();
        appHost.Start ("http://localhost:1337/");
    }

    [TestFixtureTearDown]
    public void TestFixtureTearDown ()
    {
        appHost.Dispose ();
        appHost = null;
    }

    [Test]
    public void NewPlayer_Should_Return201AndLocation ()
    {
        // Arrange
        PlayerService service = new PlayerService (appHost.TryResolve<IPlayerAppService>());

        // Act
        HttpResult response = (HttpResult)service.Post (It.IsAny<PlayerDTO>());

        // Assert
        Assert.NotNull (response);
        Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
        Assert.AreEqual(response.Response.ToDto<PlayerResponse>().Player.Id.ToString(), response.Headers.Where(x=> x.Key == HttpHeaders.Location).SingleOrDefault().Value);
    }
}

The base.Request when my unit test runs though. Do you have any suggestions on how I can populate this from my unit test?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're using an self-hosting HttpListener as you would for an integration test, but you're not doing in integration test.

An integration test would look like:

[Test]
public void NewPlayer_Should_Return201AndLocation ()
{
    var client = new JsonServiceClient("http://localhost:1337/") {
        ResponseFilter = httpRes => {
            //Test response headers...
        };
    }

    PlayerResponse response = client.Post(new Player { ... });
}

Otherwise if you want to do an unit test you don't need an AppHost and can just test the PlayerService class just like any other C# class, injecting all the dependencies and the mock Request context it needs.

[Test]
public void NewPlayer_Should_Return201AndLocation ()
{
    var mockCtx = new Mock<IRequestContext>();
    mockCtx.SetupGet (f => f.AbsoluteUri).Returns("localhost:1337/player");

    PlayerService service = new PlayerService {
        MyOtherDependencies = new Mock<IMyOtherDeps>().Object,
        RequestContext = mockCtx.Object,
    };

    HttpResult response = (HttpResult)service.Post(new Player { ... });

    //Assert stuff..
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...