Hacking together a VCR for JavaScript

At SimpleReach we have a very large number of internal API endpoints and each one can bring back dozens of fields of data into our Ember.js application. In order to make integration tests we started with static fixtures, but very soon the sheer number of fields and endpoints become overwhelming.

I really wanted something like VCR for Rails, but alas, there is nothing in the front-end world that does that yet.

Chrome provided a great hack to use though. In the network developer tools you can right click and select “save as HAR with content”. This will save a recording of all network requests that have happened since you opened the tool. Voila, instant tape of all the network requests that have happened on a site.

Once you have the HAR saved, you need a way to serve the information up programatically. Sinon.js has a mock server that will respond with saved JS based on incoming requests. Setting it up looks something like this:

Now, as long as an object has been passed to “server.respondWith” the server will respond to that.

In order to make the HAR file play nice with Sinon, I forked an application called “Mockbuilder“. Mockbuilder allows you to add a HAR file to a folder and it will automatically convert it into something that plays nicely with Sinon. The fork allows you to specify a url for internal request overwrites and a location where the new mock file should go. That way you can have it send them immediately to the mock folder in your application.

Not only does this make writing JavaScript integration tests much easier, it is also a great way to make completely offline demos of your application. Simple click around, save the HAR file, and then with the new mocks in place, you can click around the same way and it will use the recordings instead of the actual requests. Perfect for sales teams who want to have a gurantee that things will work the way they expected.

Presentation below.

Are we fighting a war?

I just watched the movie “Idiocracy” as recommended by Brian and Joe. In the words of d’Arcy Norman“damn. that movie was depressing, funny, and awesome”. It tells the story of a distopian future, where due to the fact that smart people have less children than stupid people, by the year 2500, smart people have died out. Everyone is incredibly stupid. Those left spend their time drinking “Brawndo, The thirst mutilator. It’s got electrolytes!” and watching people get kicked in the balls on television. It’s a future where everyone behaves exactly as Kraft, Walmart, etc want us to behave. It’s a brilliant cautionary tale and I highly recommend watching it.

Idiocracy Poster
Idiocracy poster (via Wikipedia)

Will it happen though?
Is there a possibility that humanity is doomed to get dumber? I think yes. There are many different reasons why this may or may not be so (all of which better suited for a non-wee-hours-of-the-morning post), but I think the largest of those is that in a world of stupid people, the corporations win. Corporations are psychotic entities that would do anything to get us to behave as they want and they have a lot of power (as described in another thought-provoking move, the Corporation). The power is evident everywhere. They are doing their damndest to use all media at their disposal to dumb-down children and make them into perfect buying machines, doing their bidding.

How do we stop it? We fight back in the schools. If education can be revolutionized (and there are many smart people working on it) then we can teach the young how to take back the power from the corporations and to make them do our bidding instead. Eating healthy,  exercising, learning and being compassionate are what smart people do and we need to ensure that despite the corporation’s efforts, everyone is given the tools and motivation to do so.

This is a war, it’s humanity VS. the corporations.We are fighting to see who controls who. If we get real about being flexible and innovative enough to fix education and make it a place where people learn to become smart enough to take back power from our creations.

Theme Update

FINALLY. Wow, that took me way to long to get done. So, as I promised in this post here, after many late nights, I have implemented some major changes to the theme of this blog. So for all those behind the feed readers… leave the reader for a bit and click around. Let me know what you think. I spent a lot of time on various elements (including some that I ripped out due to lack of patience) so any feedback would be awesome. Below is a picture of the old home page for comparison (click to see fullscreen):

Screenshot of old theme
Screenshot of old theme

Some features to note are:

  • Snazzy JavaScript slider. Why? Lets me show pictures without taking up too much space. One of my goals for this year is to learn to actually draw well in Illustrator and as is obvious, I have a long way to go and need a place/reason to practice.
  • Proper archives page using the awesome wp_archives plugin. I can’t think of a better way to browse archives (aside from tags maybe). The default WordPress way of dates is stupid… they just have no context. One way in which I might change this page in future is modify the plugin so that I can annotate dates around important events in my life.
  • Social Networks page. This has snippets from other places around the web that I haunt. I’m considering changing it in the future to be a bit of a life-stream, but am holding out for someone to come up with an exceptional plugin to make that happen.
  • Better fonts.
  • More pictures!

Again, let me know what you think in the comments below, any feedback is always welcome.

Reblog this post [with Zemanta]

Under Construction

Creative Commons License photo credit: carboila
So as some people have noticed lately my blog’s theme has changed. This is simply due to the fact that since last summer when I coded the old theme I have learned a lot about usability, accessibility and just general good practices on the web. As such I couldn’t stand the old theme any longer (due to its violation of those practices) and had to change.

While the new theme does still break a whole bunch of rules when it comes to accessibility and usability, I will be fixing those as soon as I can. The theme is chosen for its style and how I identify with it more than anything else. One of the key fixes is going to be a nice (really complex to code) JavaScript navigation. However, due to the fact that school is in crunch mode and due to my impending trip to Mexico, I will not be able to fix things up as early as I would like.

So there it is, new theme with many changes and improvements still to come. Comments, suggestions and all that jazz are welcome (especially as I am still on the fence about some of my ideas).