REST service mock

Unit testing a class that has a dependency on a external service, can be annoying.  The last thing you want to do is send live calls.  The other options appear to be setting up a test service with a test database, or writing an Echo style service that simply accepts any incoming data and echoes it back.  Neither of these are cool, and require far too much work and plumbing (not to mention slow). The ideal would seem to be the ability to quickly configure and run up a mock http listener to receive requests and give back programmable responses all isolated to the test.

I stumbled across a partial solution here:

I customised, and perhaps simplified for my use case to this: (apologies for the bad copy and paste of my IDE style)

Start at the start, the TestMethod:


        public void SendNotificationTest() {

            var mockService = new MockRestService();

            var subjectUnderTest = new SendNotificationJob() {

                Identity = GetTestData(Guid.NewGuid(), CreateUniqueLogOnId()),

                Url = new Uri(mockService.ListenOnUrl + "TestMethodName")





            Assert.IsTrue(mockService.RequestUrlsReceived.Count == 1);

            Assert.AreEqual(mockService.ListenOnUrl + "TestMethodName"mockService.RequestUrlsReceived[0]);


Secondly the MockRestService class:

    using System;

    using System.Collections.Generic;

    using System.IO;

    using System.Net;

    using System.Threading;


    public class MockRestService {

        private readonly HttpListener testServer;

        private bool keepListening = true;

        private Thread listeningThread;


        public MockRestService() {

            string uniqueServiceName = Guid.NewGuid().ToString().Replace("-"string.Empty);

            string serviceUrl = "http://localhost:60340/" + uniqueServiceName + "/";

            this.testServer = new HttpListener();


            this.ListenOnUrl = new Uri(serviceUrl);


            this.ResponsesToReturn = new Queue<string>();

            this.StatusesToReturn = new Queue<HttpStatusCode>();

            this.RequestUrlsReceived = new List<string>();



        public Uri ListenOnUrl { getprivate set; }


        public IList<string> RequestUrlsReceived { getprivate set; }


        public Queue<string> ResponsesToReturn { getprivate set; }


        public Queue<HttpStatusCode> StatusesToReturn { getprivate set; }


        public void Stop() {

            this.keepListening = false;




        public void StartAsync() {

            this.listeningThread = new Thread(() => this.Start());




        private void Start() {

            do {

                var context = this.testServer.GetContext();

                if (context.Request != null && context.Request.Url != null && !string.IsNullOrEmpty(context.Request.Url.ToString())) {


                    var writer = new StreamWriter(context.Response.OutputStream);

                    if (this.ResponsesToReturn.Count == 0) {


                    else {





                    if (this.StatusesToReturn.Count == 0) {

                        context.Response.StatusCode = (int)HttpStatusCode.OK;

                    else {

                        context.Response.StatusCode = (int)this.StatusesToReturn.Dequeue();





            while (this.keepListening);



Thats basically the crux of it.  If anyone's interested I'll post complete code.