var assert = require('assert'),
    request = require('request'),
    cheerio = require('cheerio'),
    url = require('url'),
    resourcesInDoc = require('../_resources_in_doc'),
    assertResourcesAvailable = require('../_assert_resources_available');

describe('error page test', function () {
    it('Serve not found page.', function (done) {
        var scenario = this;
        var nonExistUrl = url.resolve(scenario.baseUrl, '/foo/bar/baz/not-exists');
        request.get(nonExistUrl, function (err, res, body) {
            assert.ifError(err);
            assert.equal(res.statusCode, 404);
            assert.ok(body);

            var $ = cheerio.load(body);

            assert.equal($('title').text(), 'Page not found');

            var resources = resourcesInDoc($);
            assertResourcesAvailable(
                resources.map(function (pathname) {
                    return url.resolve(scenario.baseUrl, pathname);
                }),
                function (err) {
                    assert.ifError(err);
                    done();
                }
            );
        });
    });
});