简体   繁体   English

如何在熨斗中测试HTTP服务器

[英]How to test HTTP server in flatiron

I am using the simple as possible web server straight off of the flatiron website and wanted to experiment testing it with vows. 我正在直接使用熨斗网站上尽可能简单的Web服务器,并想通过誓言进行测试。 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? 如何关闭服务器,或者有更好的方法用另一种技术进行简单的http测试?

server.js 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 服务器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);

You need to export the server to be able to shut it down. 您需要导出服务器才能将其关闭。 Simply add this in server.js : module.exports = app; 只需将其添加到server.js中module.exports = app;

Now, you can use server var to shut flatiron down. 现在,您可以使用服务器var关闭熨斗。 The docs are not too verbose on how to close it, but I manage to have it closed with app.server.close() . 文档对如何关闭它不太详细,但是我设法通过app.server.close()将其关闭。 Here are the files : 这些是文件:

server.js 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 服务器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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM