4

I’m about to start work on an OpenRasta project (an xml over http web service). OpenRasta looks great but unfortunately worked examples seem few and far between on the internet. Looking at the test side of the project, if my handlers are returning strongly typed objects (not OperationResult), i.e.:

public class PersonHandler
...
 public Person Get(int id)
 {
 ...

How can I test for http status codes? (For example if the handler throws an uncaught exception). I’m not sure what level the tests pitch in at, and what needs mocking (using moq btw)

Any help appreciated, particularly coded examples!

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
Vman
  • 3,016
  • 2
  • 24
  • 20

2 Answers2

1

I faced the same problem, and ended up writing my tests as integration tests at a much higher level, actually making real REST/HTTP calls through a simple HttpWebRequest client. This allowed me to check the HTTP response headers / status codes and double-check the JSON/XML serialization from the client's perspective, which was just as important as whether or not the operations succeeded.

I started by returning OperationResult from all my handlers, and used these to wrap the strongly-typed objects. My handlers all inherit from a base class with a few helper methods that make it easier to return a custom error with a user-friendly error message. The more I coded this up, the more my handlers resembled a ASP.NET MVC controller. e.g.:

    public OperationResult GetById(int id)
    {
        try
        {
            // do stuff here
            return OKResult( // some strongly-typed resource );
        }
        catch(SomeException ex)
        {
            return BadRequestResult(SomeErrorCode, ex.Message);
        }
    }

Then in the test client, it's pretty easy to just check the HTTP status code. Obviously this doesn't help much with mocking. I'm not sure what the best solution is, in fact I've favorited this question in the hope that someone answers it better than I can - but this has worked pretty well for me so far.

realworldcoder
  • 699
  • 5
  • 16
  • Thanks RWC. Yes, we’ve have opted for OperationResult for now but hopefully this is just a short term patch so we can get started. It’s not perfect, it leaves too much untested. For example, any interjection points that provide functionality pre/post the handler will be untested (and, as we are TDD, untested means unwritten!). Also, the interface uri’s will be untested. – Vman Oct 27 '10 at 09:11
  • Re testing against a real client: I did look into that but every dev has their own unique deployment and implementing a physical client on our build servers would be a pain. I may reluctantly pursue that again but have a strong preference for mocks. – Vman Oct 27 '10 at 09:12
1

The handler is just a class--ideally with minimal dependencies--so your unit tests can just test the isolated logic in the class.

If you want to test for status codes, I recommend (based on very little experience!) using OpenRasta self-hosting.

Here's a test (somewhat changed) that I wrote recently:

    [TestMethod]
    public void POST_with_inaccurate_contentLength_returns_405()
    {
        var resource = GetResource();

        IRequest request = new InMemoryRequest
        {
            HttpMethod = "POST",
            Uri = new Uri("http://localhost/Resource"),
        };
        request.Headers.ContentLength = 16;  //wrong!

        request.Entity.Stream.Write(resource.Content, 0, resource.Content.Length);

        var response = _host.ProcessRequest(request);

        Assert.AreEqual(405, response.StatusCode);
    }

I should add that the host is set up in the TestInitialize method as such:

        _host = new InMemoryHost(new Configuration());
        _host.Resolver.AddDependencyInstance(typeof(IFileResourceRepository), _repository, DependencyLifetime.Singleton);

...and is cleaned up in the TestCleanup method.

Dave Nichol
  • 181
  • 3
  • 8
  • It looks like this is an old question, but hopefully it will help someone. The question and previous answer may refer to older versions of OpenRasta (I'm working with 2.0). – Dave Nichol Mar 28 '12 at 02:25