Testing RESTful services with OpenRasta
This week I’ve been spending time learning OpenRasta, a .NET framework that’s best described as a super-awesome WCF replacement. In fact it kicks WCF’s butt.
OpenRasta helps you write RESTful web services with very little effort. Not just the implementation but your tests too. Unfortunately the testability of OpenRasta-based systems is something of a mystery to beginners like myself, since the test API documentation is non-existent. What follows is how I’ve started out with building my tests.
Assume you’ve got three classes ready to go:
- a
MyResourcetype, which is your business object - a
MyResourceHandler, for handling requests to theMyResourceresource - a
Configurationclass for initializing OpenRasta.
Normally you’ll also have an ASP.NET project that hosts OpenRasta and your functionality within IIS. That’s what happens at runtime. But for the purposes of testing, you can also host OpenRasta “in memory” without requiring the instantiation of IIS and without the use of “real-life” network resources. You can programmatically fire requests at the in-memory host using a very simple API. Here’s what a barebones test looks like:
using(var host = new InMemoryHost(new Configuration()))
{
var request = new InMemoryRequest()
{
Uri = new Uri("http://localhost/MyResource"),
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;
// deserialize the content from the response
MyResource 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(MyResource));
returnedObject = serializer.ReadObject(response.Entity.Stream);
}
}
And that’s all there is to it – for a GET operation. If you’re doing a POST, you’ll need to make sure you populate the entity’s Stream and ContentLength properties.
var serializer = new DataContractJsonSerializer(typeof(MyResource)); serializer.WriteObject(Request.Entity.Stream, content); request.Entity.Stream.Seek(0, SeekOrigin.Begin); request.Entity.ContentLength = Request.Entity.Stream.Length;
In my own tests I don’t have code that looks exactly like this – I’ve factored it nicely into reusable methods that belong in my test context, and that work across multiple resource types. For example to set up my request I can write
given_new_request<AnotherResource>(
"PUT",
"AnotherResource",
anotherResource);
One final, important point. If you’ve designed your classes well, you should be relying on unit tests that do not require OpenRasta to be instantiated at all. Your OpenRasta tests should exist only as end-to-end integration tests and should focus on sending a variety of requests, testing the responses plus any side effects. You should send across a variety of both well-formed and malformed HTTP requests, using the tests to ensure that you stick to REST principles.
2 Responses to Testing RESTful services with OpenRasta
Leave a Reply Cancel reply
Tweets
- Confession: This past year, I forgot that I'm a geek. I love technology. I am obsessed with software. And now it's time to be myself again!
- @Jermolene I can just imagine you outside with your laptop in a cardboard box. That is so completely you. Awesome.
- What's the best device for writing code outdoors in the sunshine? It's a beautiful day outside and I'm stuck indoors:(
- RT @randompunter: 10 Check Amazon.co.uk for cheap touchpad. 20 GOTO 10
- @IJohnson_TNF typical example of a beautiful Ruby blog - http://t.co/6lHzwX2 show me a .NET blog like that!
- @IJohnson_TNF I think the ruby people are just more shiny
- Blogs about Ruby are always so much prettier than blogs about .NET.
- @Oura_In_Flames It's not all that amazing, and very short!
- @Oura_In_Flames you played the Sonic Generations demo yet? ;)
- @Oura_In_Flames cheers for the birthday wishes, I've been too busy at work to even think about it...
- Reading Programming in Scala, and loving it!
- Learning Scala this weekend... Quite exciting!
- @serialseb okay, on Thursday ;)
- @Oura_In_Flames lol thanks
- After two weeks of dev, finally deployed my OpenRasta app on IIS. Feeling so pleased with myself!
- Arrived at work for 8am. First time that's happened in years!
- Last one in the office - again. #deadlines
- Finishing the evening with a book in bed!
- @Oura_In_Flames Then I must have done something terribly bad as I just got drenched
- Dear Mother Nature, please check your wall calendar is on the correct page. It's June, not January. Yes, June. #wet





Excellent article, I was searching for Unittests for handlers and your article was bulls eye. One question – DataContractJsonSerializer is a class in OpenRasta or did you use System.Runtime.Serialization namespace class. I couldn’t find any for Xml serialization as such, that’s why this question
I’m using the DataContractJsonSerializer from the System.Runtime.Serialization namespace. You can use the DataContractSerializer for XML serialization, it works in a similar way.