【问题标题】:How to test HTTP server in flatiron如何在 flatiron 中测试 HTTP 服务器
【发布时间】:2012-12-31 23:34:18
【问题描述】:

我正在使用flatiron 网站上的尽可能简单的网络服务器,并想用誓言来测试它。我可以让测试通过,但测试永远不会退出。我认为这是因为我的熨斗服务器从不关闭。如何关闭服务器或有更好的方法使用另一种技术进行简单的 http 测试?

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);

【问题讨论】:

    标签: vows flatiron.js


    【解决方案1】:

    您需要导出服务器才能将其关闭。 只需在 server.js 中添加:module.exports = app;

    现在,您可以使用服务器 var 来关闭 flatiron。文档对如何关闭它并不太冗长,但我设法用app.server.close() 关闭它。 以下是文件:

    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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多