'use strict';

var http = require('http');
var request = require('request');
var expect = require('expect.js');
var replay = require('../');

describe('request-replay', function () {
    it('should replay on network error', function (next) {
        replay(request.get('http://somedomainthatwillneverexistforsure.com:8089', function (error) {
            expect(error).to.be.an(Error);
            expect(['ENOTFOUND', 'EADDRINFO']).to.contain(error.code);
            expect(error.replays).to.equal(5);
            next();
        }), {
            minTimeout: 10,
            maxTimeout: 10
        });
    });

    it('should succeed if first fails but one of others succeed', function (next) {
        var tries = 0;

        this.timeout(15000);

        replay(request.get('http://127.0.0.1:8089', { json: true }, function (error, response, body) {
            expect(error).to.be(null);
            expect(response).to.be.ok();
            expect(body).to.eql({ 'foo': 'bar' });
            expect(tries).to.be(1);
            next();
        }), {
            errorCodes: ['ECONNREFUSED'],
            minTimeout: 10,
            maxTimeout: 10
        })
        .on('replay', function () {
            tries++;

            http.createServer(function (req, res) {
                res.writeHead(200, { 'Content-Type': 'application/json' });
                res.end('{ "foo": "bar" }');
            })
            .listen(8089, '127.0.0.1');
        });
    });

    it('should fire a replay event on each retry', function (next) {
        var stream;
        var tries = 0;

        stream = replay(request.get('http://somedomainthatwillneverexistforsure.com:8089', function (error) {
            expect(error).to.be.an(Error);
            expect(['ENOTFOUND', 'EADDRINFO']).to.contain(error.code);
            expect(error.replays).to.equal(5);
            next();
        }), {
            minTimeout: 10,
            maxTimeout: 10
        })
        .on('replay', function (replay) {
            expect(replay).to.be.an('object');
            expect(replay.number).to.equal(tries);
            expect(replay.error).to.be.an(Error);
            expect(['ENOTFOUND', 'EADDRINFO']).to.contain(replay.error.code);
            expect(replay.delay).to.be(10);
            tries++;
        });
    });
});

@@ -172,24 +172,211 @@
 nction () %7B%0A
+    var server;%0A%0A    beforeEach(function (next) %7B%0A        if (server) %7B%0A            server.close(next);%0A            server = null;%0A        %7D else %7B%0A            next();%0A        %7D%0A    %7D);%0A%0A
     it('shou
@@ -414,32 +414,32 @@
 nction (next) %7B%0A
-
         replay(r
@@ -1413,16 +1413,25 @@
         
+ server =
  http.cr
@@ -2456,16 +2456,16 @@
 be(10);%0A
-
         
@@ -2496,13 +2496,1136 @@
 %0A    %7D);
+%0A%0A    it('should fire only one end event and no error events on success', function (next) %7B%0A        var tries = 0;%0A        var events = %7B error: 0, end: 0 %7D;%0A%0A        this.timeout(15000);%0A%0A        replay(request.get('http://127.0.0.1:8089', %7B json: true %7D, function (error, response, body) %7B%0A            expect(error).to.be(null);%0A            expect(response).to.be.ok();%0A            expect(body).to.eql(%7B 'foo': 'bar' %7D);%0A%0A            expect(tries).to.be(1);%0A            expect(events).to.eql(%7B error: 0, end: 1 %7D);%0A            next();%0A        %7D), %7B%0A            errorCodes: %5B'ECONNREFUSED'%5D,%0A            minTimeout: 10,%0A            maxTimeout: 10%0A        %7D)%0A        .on('error', function () %7B%0A            events.error += 1;%0A        %7D)%0A        .on('end', function () %7B%0A            events.end += 1;%0A        %7D)%0A        .on('replay', function () %7B%0A            tries++;%0A%0A            server = http.createServer(function (req, res) %7B%0A                res.writeHead(200, %7B 'Content-Type': 'application/json' %7D);%0A                res.end('%7B %22foo%22: %22bar%22 %7D');%0A            %7D)%0A            .listen(8089, '127.0.0.1');%0A        %7D);%0A    %7D);
 %0A%7D);%0A

Add spec to test some more events.