       handler = (request, callback) ->
         setImmediate -> callback(text)
       protocol.registerStringProtocol protocolName, handler, (error) ->
+        return done(error) if error
         $.ajax
           url: "#{protocolName}://fake-host"
           success: (data) ->

    it 'can intercept http protocol', (done) ->
      handler = (request, callback) -> callback(new Buffer(text))
      protocol.interceptBufferProtocol 'http', handler, (error) ->
        $.ajax
          url: 'http://fake-host'
          success: (data) ->

    it 'sends string as response', (done) ->
      handler = (request, callback) -> callback(text)
      protocol.registerStringProtocol protocolName, handler, (error) ->
        return done(error) if error
        $.ajax
          url: "#{protocolName}://fake-host"
          success: (data) ->
