var cantina = require('../')
  , assert = require('assert');

function inArray(val, arr) {
  var i = arr.length;
  while (i--) {
    if (arr[i] === val) {
      return true;
    }
  }
  return false;
}

describe('Service', function() {
  var client, daemon;
  before(function() {
    client = new cantina.Service();
    daemon = new cantina.Service();
  });

  describe('pub/sub', function() {
    it('should only receive from the subscribed event', function(done) {
      var left = 2;
      var letters = ['A', 'B'];
      daemon.subscribe('alphabet', function(letter) {
        if (inArray(letter, letters)) {
          if (!--left) {
            done();
          }
        }
        else {
          assert.fail(letter, letters, 'Letter not in list', 'in');
        }
      });
      // Give time for the subscribe to take effect.
      setTimeout(function() {
        client.publish('nonsense', 'aefae');
        client.publish('alphabet', 'A');
        client.publish('nonsense', '23523');
        client.publish('nonsense', 'awfad');
        client.publish('alphabet', 'B');
      }, 50);
    });
  });

  describe('push/pull', function() {
    it('should only receive from the subscribed queue', function(done) {
      var left = 4;
      var beatles = ['Ringo', 'John', 'Paul', 'George'];
      daemon.pull('beatles', function(name) {
        if (inArray(name, beatles)) {
          if (!--left) {
            done();
          }
        }
        else {
          assert.fail(name, beatles, 'Name not in list', 'in');
        }
      });
      client.push('u2', 'Bono');
      client.push('beatles', 'Ringo');
      client.push('jimi', 'Hendrix');
      client.push('mamas', 'Cass');
      client.push('beatles', 'John');
      client.push('beatles', 'George');
      client.push('cream', 'Eric');
      client.push('beatles', 'Paul');
    });
  });

  describe('req/rep', function() {
    describe('GET', function() {
      before(function() {
        client.reply('math.edu', function(req, done) {
          var parsed = require('url').parse(req.path, true);
          // Field requests for square roots.
          if (parsed.pathname == '/square') {
            done({code: 200, data: Math.pow(parsed.query.input, 2)});
          }
          else {
            done({code: 404, data: 'Page not found'});
          }
        });
      });
      it('returns correct answer', function(done) {
        var input = Math.round(100 * Math.random());
        var url = '//math.edu/square?input=' + input;

        client.request(url, function(err, res) {
          assert(res.data === input * input, 'Square correctly returned');
          assert(JSON.stringify(res.data) === res.body, 'res.body is JSON of data');
          assert(res.headers['Content-Type'] === 'application/json', 'res content-type is JSON');
          assert(res.code === 200, 'response code is 200');
          done(err);
        });
      });

      it('returns 404', function(done) {
        var input = 100 * Math.random();
        var url = '//math.edu/round?input=' + input;
        client.request(url, function(err, res) {
          assert(res.data === 'Page not found', 'res.data is error message');
          assert(res.code === 404, 'res.code is 404');
          assert(err === null, 'err is null');
          done();
        });
      });
    });
  });
});

@@ -2096,26 +2096,63 @@
 ests
- for square roots.
+.%0A          var input = new Number(parsed.query.input);
 %0A   
@@ -2158,18 +2158,22 @@
         
-if
+switch
  (parsed
@@ -2181,19 +2181,36 @@
 pathname
- ==
+) %7B%0A            case
  '/squar
@@ -2211,20 +2211,20 @@
 /square'
-) %7B%0A
+:%0A  
         
@@ -2259,29 +2259,16 @@
 ath.pow(
-parsed.query.
 input, 2
@@ -2286,27 +2286,159 @@
     
-%7D%0A          else %7B%0A
+    break;%0A            case '/meaning-of-life':%0A              done(%7Bcode: 500, data: %22Can't calculate!%22%7D);%0A              break;%0A            default:%0A  
     
@@ -2468,17 +2468,17 @@
 , data: 
-'
+%22
 Page not
@@ -2483,17 +2483,17 @@
 ot found
-'
+%22
 %7D);%0A    
@@ -3492,24 +3492,479 @@
 );%0A      %7D);
+%0A%0A      it('returns 500', function(done) %7B%0A        var url = '//math.edu/meaning-of-life';%0A        client.request(url, function(err, res) %7B%0A          assert(res.data === %22Can't calculate!%22, 'res.data is error message');%0A          assert(res.code === 500, 'res.code is 500');%0A          assert(err.code === 500, 'err.code is 500');%0A          assert(err.message === %22Can't calculate!%22, 'err.message is error message');%0A          done();%0A        %7D);%0A      %7D);
 %0A    %7D);%0A  %7D

Add 500 test.