var Asker = require('../lib/asker'),
    ask = Asker,
    httpTest = require('./lib/http'),
    assert = require('chai').assert;

function filter(code) {
    return {
        /* jshint bitwise:false */
        accept : ~[200, 201].indexOf(code),
        isRetryAllowed : 400 > code || code > 499 || code === 404
    };
}

module.exports = {
    'retries => default => success 500-201' : httpTest(function(done, server) {

        server.addTest(function(req, res) {
            res.statusCode = 500;
            res.end();
        });

        server.addTest(function(req, res) {
            res.statusCode = 201;
            res.end();
        });

        ask({ port : server.port, maxRetries : 1 }, function(error, response) {
            assert.strictEqual(error, null, 'no errors');
            assert.strictEqual(response.meta.retries.used, 1, 'one retry was used');
            assert.strictEqual(response.meta.retries.limit, 1, 'retries limit is correct');

            done();
        });

    }),

    'retries => default => fail 500-500' : httpTest(function(done, server) {

        server.addTest(function(req, res) {
            res.statusCode = 500;
            res.end();
        });

        server.addTest(function(req, res) {
            res.statusCode = 500;
            res.end();
        });

        ask({ port : server.port, maxRetries : 1 }, function(error) {
            assert.strictEqual(error.code, 907, 'retries limit exceeded');

            done();
        });

    }),

    'retries => custom => success 404-201' : httpTest(function(done, server) {

        server.addTest(function(req, res) {
            res.statusCode = 404;
            res.end();
        });

        server.addTest(function(req, res) {
            res.statusCode = 201;
            res.end();
        });

        ask({ port : server.port, maxRetries : 1, statusFilter : filter }, function(error, response) {
            assert.strictEqual(error, null, 'no errors');
            assert.strictEqual(response.meta.retries.used, 1, 'one retry was used');
            assert.strictEqual(response.meta.retries.limit, 1, 'retries limit is correct');

            done();
        });

    }),

    'retries => custom => fail 404-404' : httpTest(function(done, server) {

        server.addTest(function(req, res) {
            res.statusCode = 404;
            res.end();
        });

        server.addTest(function(req, res) {
            res.statusCode = 404;
            res.end();
        });

        ask({ port : server.port, maxRetries : 1, statusFilter : filter }, function(error) {
            assert.strictEqual(error.code, 907, 'retries limit exceeded');

            done();
        });

    }),

    'retries => socket timeout => success' : httpTest(function(done, server) {

        server.addTest(function(req, res) {
            setTimeout(function() {
                res.statusCode = 201;
                res.end();
            }, 200);
        });

        server.addTest(function(req, res) {
            res.statusCode = 201;
            res.end();
        });

        ask({ port : server.port, maxRetries : 1, timeout : 50 }, function(error, response) {
            assert.strictEqual(error, null, 'no errors');
            assert.strictEqual(response.meta.retries.used, 1, 'one retry was used');
            assert.strictEqual(response.meta.retries.limit, 1, 'retries limit is correct');

            done();
        });

    })
};

@@ -3432,22 +3432,1608 @@
        %7D);%0A%0A
+    %7D),%0A%0A    'onretry callback must be called when retry has been commited' : httpTest(function(done, server) %7B%0A        var EXPECTED_RETRIES = 2,%0A            counter = 0,%0A%0A            retriesHandler = function (reason, count) %7B%0A                assert.strictEqual(reason.code, Asker.Error.CODES.UNEXPECTED_STATUS_CODE,%0A                    'expected retry reason passed');%0A%0A                assert.ok(count %3C= EXPECTED_RETRIES, 'expected retries count');%0A%0A                counter++;%0A            %7D,%0A%0A            request = new Asker(%7B port : server.port, maxRetries : EXPECTED_RETRIES, onretry : retriesHandler %7D,%0A                function(error, response) %7B%0A                    assert.strictEqual(error, null, 'request has been executed without errors');%0A                    assert.strictEqual(response.data, null, 'response is empty');%0A                    assert.strictEqual(counter, response.meta.retries.used,%0A                        'onretry callback was called for each used request attemt');%0A%0A                    done();%0A                %7D);%0A%0A        assert.strictEqual(request._onretry, retriesHandler,%0A            'retries handler setup is done');%0A%0A        server.addTest(function(req, res) %7B%0A            // instigate for the retry of request%0A            res.statusCode = 500;%0A            res.end();%0A        %7D);%0A%0A        server.addTest(function(req, res) %7B%0A            // instigate for the retry of request%0A            res.statusCode = 500;%0A            res.end();%0A        %7D);%0A%0A        server.addTest(function(req, res) %7B%0A            res.end();%0A        %7D);%0A%0A        request.execute();%0A
     %7D)%0A%7D;%0A

add onretry callback test