/*
  This set of UT validate request/responses
  against public resources.
*/

var request = require('supertest'),
    should = require('should');

var host = 'http://127.0.0.1:8082';

var app = request(host);

public_resources = [
  {
    'url': '/',
    'type': 'text/html',
    'status': 200
  },
  {
    'url': '/index.html',
    'type': 'text/html',
    'status': 200
  },
  {
    'url': '/index.js',
    'type': 'application/javascript',
    'status': 200
  },
  {
    'url': '/app.html',
    'type': 'text/html',
    'status': 200
  },
  {
    'url': '/js/scripts.js',
    'type': 'application/javascript',
    'status': 200
  },
  {
    'url': '/css/styles.css',
    'type': 'text/css',
    'status': 200
  },
  {
    'url': '/static/custom_stylesheet.css',
    'type': 'text/css',
    'status': 200
  },
  {
    'url': '/static/logo.png',
    'type': 'image/png',
    'status': 200
  },
  {
    'url': '/node',
    'type': 'application/json',
    'status': 200
  },
  {
    'url': '/contexts',
    'type': 'application/json',
    'status': 200
  },
  {
    'url': '/receivers',
    'type': 'application/json',
    'status': 200
  },
  {
    'url': '/unexistent',
    'type': 'text/html; charset=UTF-8',
    'status': 404
  }
];

var validate_mandatory_headers = function(headers) {
  var mandatory_headers = {
    'X-XSS-Protection': '1; mode=block',
    'X-Robots-Tag': 'noindex',
    'X-Content-Type-Options': 'nosniff',
    'Expires': '-1',
    'Server': 'globaleaks',
    'Pragma':  'no-cache',
    'Cache-control': 'no-cache, no-store, must-revalidate'
  }

  for (var key in mandatory_headers) {
    if (headers[key.toLowerCase()] != mandatory_headers[key]) {
      throw key + ' != ' + mandatory_headers[key];
    }
  }
}

public_resources.forEach(function(req){
  describe('GET ' + req['url'], function(){
    it('responds with ' + req['type'], function(done){
      app
      .get(req['url'])
      .expect('Content-Type', req['type'])
      .expect(req['status'])
      .end(function(err, res) {
        if (err) {
          return done(err);
        } else {

          validate_mandatory_headers(res.headers);

          if (req['type'] == 'application/json') {
            // TODO JSON FORMAT VALIDATION
            // https://npmjs.org/package/jsonschema
          };
          done();
        } 
      });
    })
  })
})

public_resources.forEach(function(req){
  describe('POST ' + req['url'], function(){
    it('responds with 405', function(done){
      app
      .post(req['url'])
      .expect(405)
      .end(function(err, res) {
        if (err) {
          return done(err);
        } else {
          validate_mandatory_headers(res.headers);

          if (req['type'] == 'application/json') {
            // TODO JSON FORMAT VALIDATION
            // https://npmjs.org/package/jsonschema
          }
          done();
        }
      });
    })
  })
})

public_resources.forEach(function(req){
  describe('DELETE ' + req['url'], function(){
    it('responds with ' + req['type'], function(done){
      app
      .del(req['url'])
      .expect(405)
      .end(function(err, res) {
        if (err) {
          return done(err);
        } else {
          validate_mandatory_headers(res.headers);

          if (req['type'] == 'application/json') {
            // TODO JSON FORMAT VALIDATION
            // https://npmjs.org/package/jsonschema
          }
          done();
        }
      });
    })
  })
})

@@ -719,103 +719,8 @@
   %7B%0A
-    'url': '/static/custom_stylesheet.css',%0A    'type': 'text/css',%0A    'status': 200%0A  %7D,%0A  %7B%0A
     

Remove testing against /static/custom_stylesheet.css that is no more existing on default install