簡體   English   中英

如何在熨斗中測試HTTP服務器

[英]How to test HTTP server in flatiron

我正在直接使用熨斗網站上盡可能簡單的Web服務器,並想通過誓言進行測試。 我可以通過測試,但是測試永遠不會退出。 我認為這是因為熨斗服務器從未關閉過。 如何關閉服務器,或者有更好的方法用另一種技術進行簡單的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);

服務器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);

您需要導出服務器才能將其關閉。 只需將其添加到server.js中module.exports = app;

現在,您可以使用服務器var關閉熨斗。 文檔對如何關閉它不太詳細,但是我設法通過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);

服務器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