<commit_before>var assert = require('assert');
var http = require('http');
var os = require('os');
var request = require('supertest');
var spdy = require('spdy');
var zetta = require('../');
var Query = require('calypso').Query;
var rels = require('zetta-rels');
var zettacluster = require('zetta-cluster');
var Scout = require('./fixture/example_scout');
var Driver = require('./fixture/example_driver');
var HttpDriver = require('./fixture/example_http_driver');
var Registry = require('./fixture/mem_registry');
var PeerRegistry = require('./fixture/mem_peer_registry');

function getHttpServer(app) {
  return app.httpServer.server;
}

function getBody(fn) {
  return function(res) {
    try {
      if(res.text) {
        var body = JSON.parse(res.text);
      } else {
        var body = '';
      }
    } catch(err) {
      throw new Error('Failed to parse json body');
    }

    fn(res, body);
  }
}

function checkDeviceOnRootUri(entity) {
  assert(entity.class.indexOf('device') >= 0);
  assert(entity.class.indexOf(entity.properties.type) >= 0);
  assert.deepEqual(entity.rel, ["http://rels.zettajs.io/device"]);
  
  assert(entity.properties.name);
  assert(entity.properties.type);
  assert(entity.properties.state);
  assert(!entity.actions); // should not have actions on it

  assert(entity.links);
  hasLinkRel(entity.links, rels.self);
  hasLinkRel(entity.links, rels.server);
  hasLinkRel(entity.links, rels.type);
  hasLinkRel(entity.links, rels.edit);
}

function hasLinkRel(links, rel, title, href) {
  var found = false;

  links.forEach(function(link) {
    if(link.rel.indexOf(rel) != -1) {
      found = true;

      if(title !== undefined && link.title !== title) {
        throw new Error('link title does not match');
      }

      if(href !== undefined && link.href !== href) {
        throw new Error('link href does not match');
      }
    }
  });

  if(!found) {
    throw new Error('Link rel:'+rel+' not found in links');
  }
}


describe('Zetta Api', function() {
  var reg = null;
  var peerRegistry = null;

  beforeEach(function() {
    reg = new Registry();
    peerRegistry = new PeerRegistry();
  });


  it('allow for x-forwarded-host header to be disabled', function(done) {
    var app = zetta({ registry: reg, peerRegistry: peerRegistry, useXForwardedHostHeader: false  })
        .silent()
        .name('local')
        ._run(function(err) {
          if (err) {
            return done(err);
          }

          request(getHttpServer(app))
            .get('/')
            .set('x-forwarded-host', 'google.com')
            .expect(getBody(function(res, body) {<commit_msg>Unit Test for "x-forwarded-host" Header (#313)<commit_after>var assert = require('assert');
var http = require('http');
var os = require('os');
var request = require('supertest');
var spdy = require('spdy');
var zetta = require('../');
var Query = require('calypso').Query;
var rels = require('zetta-rels');
var zettacluster = require('zetta-cluster');
var Scout = require('./fixture/example_scout');
var Driver = require('./fixture/example_driver');
var HttpDriver = require('./fixture/example_http_driver');
var Registry = require('./fixture/mem_registry');
var PeerRegistry = require('./fixture/mem_peer_registry');

function getHttpServer(app) {
  return app.httpServer.server;
}

function getBody(fn) {
  return function(res) {
    try {
      if(res.text) {
        var body = JSON.parse(res.text);
      } else {
        var body = '';
      }
    } catch(err) {
      throw new Error('Failed to parse json body');
    }

    fn(res, body);
  }
}

function checkDeviceOnRootUri(entity) {
  assert(entity.class.indexOf('device') >= 0);
  assert(entity.class.indexOf(entity.properties.type) >= 0);
  assert.deepEqual(entity.rel, ["http://rels.zettajs.io/device"]);
  
  assert(entity.properties.name);
  assert(entity.properties.type);
  assert(entity.properties.state);
  assert(!entity.actions); // should not have actions on it

  assert(entity.links);
  hasLinkRel(entity.links, rels.self);
  hasLinkRel(entity.links, rels.server);
  hasLinkRel(entity.links, rels.type);
  hasLinkRel(entity.links, rels.edit);
}

function hasLinkRel(links, rel, title, href) {
  var found = false;

  links.forEach(function(link) {
    if(link.rel.indexOf(rel) != -1) {
      found = true;

      if(title !== undefined && link.title !== title) {
        throw new Error('link title does not match');
      }

      if(href !== undefined && link.href !== href) {
        throw new Error('link href does not match');
      }
    }
  });

  if(!found) {
    throw new Error('Link rel:'+rel+' not found in links');
  }
}


describe('Zetta Api', function() {
  var reg = null;
  var peerRegistry = null;

  beforeEach(function() {
    reg = new Registry();
    peerRegistry = new PeerRegistry();
  });

  it('updates href hosts using x-forwarded-host header', function(done) {
    var app = zetta({ registry: reg, peerRegistry: peerRegistry  })
        .silent()
        .name('local')
        ._run(function(err) {
          if (err) {
            return done(err);
          }

          request(getHttpServer(app))
            .get('/')
            .set('x-forwarded-host', 'google.com')
            .expect(getBody(function(res, body) {
              var self = body.links.filter(function(link) { return link.rel.indexOf('self') >= 0; })[0];
              assert.equal(self.href, 'http://google.com/');
            }))
            .end(done);
        });
  })

  it('allow for x-forwarded-host header to be disabled', function(done) {
    var app = zetta({ registry: reg, peerRegistry: peerRegistry, useXForwardedHostHeader: false  })
        .silent()
        .name('local')
        ._run(function(err) {
          if (err) {
            return done(err);
          }

          request(getHttpServer(app))
            .get('/')
            .set('x-forwarded-host', 'google.com')
            .expect(getBody(function(res, body) {
{'fraction_non_alphanumeric': 0.16134453781512606, 'fraction_numerical': 0.0018487394957983194, 'mean_word_length': 2.9436713055003314, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3576087', 'n_tokens_mistral': 1891, 'n_tokens_neox': 1842, 'n_words': 412}