var render = require('../index.js');
var expect = require('chai').expect;
var connect = require('connect');
var request = require('supertest');
var Mocksy = require('mocksy');
var server = new Mocksy({port: 8888});
var bot = 'Baiduspider+(+http://www.baidu.com/search/spider.htm)';

describe('prerender service', function () {
  var app;
  
  beforeEach(function (done) {
    app = connect()
      .use(function (req, res, next) {
        req.service = {
          name: 'prerender',
          config: {}
        };
        next();
      });
    server.start(done);
  });
  
  afterEach(function (done) {
    server.stop(done);
  });
  
  it('skips the middleware if request method is not GET', function (done) {
    var matches = render().matches({method: 'POST', headers: {}});
    expect(matches).to.equal(false);
    done();
  });
  
  it('makes a request to a custom prerender server with a host set', function (done) {
    app.use(render({
      host: 'http://localhost:8888'
    }));
    
    request(app)
      .get('/render')
      .set('user-agent', bot)
      .expect(function (data) {
        var res = data.res.body;
        expect(res.headers.host).to.equal('localhost:8888');
        expect(res.method).to.equal('GET');
      })
      .end(done);
  });
  
  it('sends the api token as a header', function (done) {
    app.use(render({
      host: 'http://localhost:8888',
      token: 'testing'
    }));
    
    request(app)
      .get('/render')
      .set('user-agent', bot)
      .expect(function (data) {
        expect(data.res.body.headers['x-prerender-token']).to.equal('testing');
      })
      .end(done);
  });
  
  it('returns rendered page from the default prerender service', function (done) {
    app.use(render({
      token: 'testing'
    }));
    
    request(app)
      .get('/render')
      .set('user-agent', bot)
      .expect(200)
      .expect('<html><head></head><body></body></html>')
      .end(done);
  });
  
  it('sets the whitelist values on prerender', function (done) {
    app
      .use(function (req, res, next) {
        req.service.config.whitelist = '/prerender';
        next();
      })
      .use(render({
        host: 'http://localhost:8888'
      }));
    
    request(app)
      .get('/')
      .set('user-agent', bot)
      .expect(function (data) {
        expect(data.res.body).to.equal(undefined);
      })
      .end(done);
  });
  
  it('sets the blacklist values on prerender', function (done) {
    app
      .use(function (req, res, next) {
        req.service.config.blacklist = '/prerender';
        next();
      })
      .use(render({
        host: 'http://localhost:8888'
      }));
    
    request(app)
      .get('/prerender')
      .set('user-agent', bot)
      .expect(function (data) {
        expect(data.res.body).to.equal(undefined);
      })
      .end(done);
  });
  
  it('sets the refetch value from the config');
  
});
@@ -1709,32 +1709,56 @@
 nction (done) %7B%0A
+    this.timeout(5000);%0A
     app.use(rend
@@ -1886,11 +1886,11 @@
 ect(
-200
+504
 )%0A  
@@ -1906,47 +1906,8 @@
 ct('
-%3Chtml%3E%3Chead%3E%3C/head%3E%3Cbody%3E%3C/body%3E%3C/html%3E
 ')%0A 

fix prerender testing