Archive for the ‘REST’ Category

DynamicJson and unit-testing JSON services

January 1, 2011

I’m currently messing with setting up unittests for a JSON/REST’ish service. My goal is having (or at least have a looke at having :)) a test suite that executes over HTTP in exatcly the same manner as an actual client. One advantages of this I think, will be the ability to execute the tests against a remote system, once the system is deployed.

Trying to figure out the best way to inspect the returned JSON data, I came across DynamicJson ( The library combines the new C# 4.0 dynamic features with a JSON parser to allow for what you see below:

public void GetList()
    var webClient = new WebClient();
    webClient.Encoding = UTF8Encoding.UTF8;
    webClient.Headers.Add(HttpRequestHeader.ContentType, "application/json");

    var result = DynamicJson.Parse(webClient.DownloadString("http://localhost/WebSite/List/BE33A625-63F3-4014-85E7-D306F00A5A7E"));

    Assert.AreEqual("TestList", result.Title);

The library does contain a serializer as well, but it only support basic types (so GUID’s for instance are not supported). I find the JavaScriptSerializer much more usefull..

Streaming MP3 audio from a WCF REST service

June 29, 2009

For a project i am working on in my spare time, i needed to stream MP3 audio from a REST service, implemented in WCF. I was supprised at how easy this was. It’s as simple as setting the response content type and returning a stream:

IAudioDemoService.cs method definition:

Method = "GET",
UriTemplate = "/AudioStream")]
Stream GetAudioStream();

AudioDemoService.cs method implementation:

public Stream GetAudioStream()
	WebOperationContext.Current.OutgoingResponse.ContentType = "audio/mpeg";
	return new FileStream(@"c:\AudioFile.mp3", FileMode.Open);

(actually the content type isn’t strictly nessesary, but it enables a client to identify the information in the stream – a browser for instance might use it to select an appropriate application for processing)