Sunday, October 7, 2012

wssh: Handy utility for WebSocket testing

WebSockets are a pretty darn cool evolution of the web, but with any new technology comes some challenges providing adequate tools for testing and exploration.  For HTTP, we have curl.  For plain sockets, we have netcat.  WebSockets now have wssh.  I stumbled upon this very useful tool recently in starting up my own advanced WebSocket project.

Unfortunately, there were some glaring bugs and annoyances, in particular in the area of automation and general netcat compliance.  So, I brushed off my Python skills and I'm happy to report that after the weekend was over, the tool had matured quite a lot and was ready for some test automation.

Hoping to move the project to some QA folks for automation, I decided to whip up a quick recipe that demonstrates how the tool can be used to verify both client and server behaviour with minimal headache:

A simple echo server test

#!/bin/bash

set -e
(echo foo; echo bar; echo baz) > testinput
cat <(cat testinput | while read a; do echo $a; sleep 1; done) | wssh -q 0 ws://echo.websocket.org/ > testoutput
cmp testinput testoutput
rm -f testinput testoutput

This script tests ws://echo.websocket.org under "real-world" usage simulating a 1 second delay between each line of input.  This concept can be extended much further with some basic bash-fu, creating some fairly sophisticated unit tests.