'use strict';

var assert = require('chai').assert,
    fs = require('fs'),
    mockfs = require('mock-fs'),
    nock = require('nock'),
    lodash = require('lodash'),
    AT = require('../api-validator.js').task,

    baseurl = 'http://fake.host',
    NoConnectURL = 'http://localhost:1/',

    PATHS = {
        NULL: '/getNull',
        ABC123: '/getJsonAbc123',
        ABCDEF: '/getJsonAbcDef'
    },

    setupFakeHTTP = function () {
        nock(baseurl)
        .get(PATHS.NULL).reply(200, '')
        .get(PATHS.ABC123).reply(200, {abc: 123})
        .get(PATHS.ABCDEF).reply(200, {abc: '123', def: 0});
    },

    cleanFakeHTTP = function () {
        nock.cleanAll();
    },

    initMockFS = function () {
        mockfs({});
    },

    clearMockFS = function () {
        mockfs.restore();
    };

describe('Task.fetch', function () {
    before(function () {
        initMockFS();
        setupFakeHTTP();
    });
    after(function () {
        clearMockFS();
        cleanFakeHTTP();
    });

    it('should save results and return save status', function (done) {
        AT.fetch([
            {url: baseurl + PATHS.NULL},
            {url: NoConnectURL},
            {url: baseurl + PATHS.ABC123}
        ], function (L) {
            assert.deepEqual({}, L);
            assert.deepEqual('', JSON.parse(fs.readFileSync('./file_0001.json'), 'utf8').body);
            assert.deepEqual({type: 'request', message: 'connect ECONNREFUSED'}, JSON.parse(fs.readFileSync('./file_0002.json'), 'utf8').error[0]);
            done();
        });
    });
});

@@ -1211,17 +1211,72 @@
 .ABC123%7D
+,%0A            %7Burl: baseurl + PATHS.ABCDEF, json: true%7D
 %0A
-
         
@@ -1383,34 +1383,32 @@
 s.readFileSync('
-./
 file_0001.json')
@@ -1533,18 +1533,16 @@
 leSync('
-./
 file_000
@@ -1570,16 +1570,232 @@
 or%5B0%5D);%0A
+            assert.deepEqual('%7B%22abc%22:123%7D', JSON.parse(fs.readFileSync('file_0003.json'), 'utf8').body);%0A            assert.deepEqual(%7Babc:'123', def: 0%7D, JSON.parse(fs.readFileSync('file_0004.json'), 'utf8').body);%0A
         

test on json format request