Pregunta

I am using the simple as possible web server straight off of the flatiron website and wanted to experiment testing it with vows. I can get the tests to pass but the test never exits. I assume this is because I the flatiron server never shuts down. How do I shut the server down or there a better way to do simple http tests with another technology?

server.js

var flatiron = require('flatiron'),
    app = flatiron.app;

app.use(flatiron.plugins.http);

app.router.get('/', function () {
  this.res.writeHead(200, { 'Content-Type': 'text/plain' });
  this.res.end('Hello world!\n');
});

app.start(8000);

server-test.js

var request = require('request'),
    vows = require('vows'),
    assert = require('assert');

vows.describe('Hello World').addBatch({
  "A GET to /": {
    topic: function () {
      server = require('../server');
      request({
        uri: 'http://localhost:8000',
        method: 'GET'
      }, this.callback)
    },
    "should respond with 200": function (error, response, body) {
      assert.equal("Hello world!\n", body);
    },
    teardown: function (topic) {
      // *********************************
      // Do something to shutdown flatiron
      // *********************************
    }
  }
}).export(module);
¿Fue útil?

Solución

You need to export the server to be able to shut it down. Simply add this in server.js : module.exports = app;

Now, you can use server var to shut flatiron down. The docs are not too verbose on how to close it, but I manage to have it closed with app.server.close(). Here are the files :

server.js

var flatiron = require('flatiron'),
    app = flatiron.app;

module.exports = app;

app.use(flatiron.plugins.http);

app.router.get('/', function () {
  this.res.writeHead(200, { 'Content-Type': 'text/plain' });
  this.res.end('Hello world!\n');
});

app.start(8000);

server-test.js

var request = require('request'),
    vows = require('vows'),
    assert = require('assert');

var app = require('./server');

vows.describe('Hello World').addBatch({
  "A GET to /": {
    topic: function () {
      request({
        uri: 'http://localhost:8000',
        method: 'GET'
      }, this.callback)
    },
    "should respond with 200": function (error, response, body) {
      assert.equal("Hello world!\n", body);
    },
    teardown: function (topic) {
      app.server.close();
    }
  }
}).export(module);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top