Stubbing Flex HTTPServices

On a few projects I have worked on there has been necessary to stub the Flex HttpServices. A couple of reasons has been, server side code is not ready yet or services are too unstable to support a continuous UI development.

I have seen examples where there has been spent a lot of time developing code to accommodate for this, with the only result that code base has a lot of non production code which can cause bugs and add an overhead to maintenance of the system. In my eyes that would be wrong, test/mock coding is money wasted on valuable time that should be spent on developing features.

So why not use something which already (almost) exist. When you use HTTPServices, you can point to nearly anything that gives you some kind of XML result. Pointing to a static XML file on the server should give you almost the same as pointing to a service which would serve you dynamic XML.

package com.lab49.spike
{
	import mx.core.Application;

	import mx.rpc.AsyncToken;
	import mx.rpc.http.mxml.HTTPService;

	public class StubHttpService extends HTTPService
	{
		public var stubUrl : String = "";

		private function get stub() : Boolean
		{
			var stubParam : String = String( Application.application.parameters[ "stub" ] );

			return stubParam == "true";
		}

		public override function send( param : Object  = null ) : AsyncToken
		{
			if( stub )
			{
				url = stubUrl
			}

			return super.send( param );
		}
	}
}

And when you use it, insert “stub=true” in your url, and add this to your StubHTTPService:

<spike:StubHttpService
	url="http://localhost:8080/spike/myService"
	stubUrl="xml/stub.xml"
	...
/>
This entry was posted in Flex and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>