使用POST方法通过Zombie.js浏览器调用API

我正在用Zombie.jstesting我的node.js代码。 我有以下的API,这是在POST方法:

/api/names 

并在我的testing/ person.js文件中的代码如下:

 it('Test Retreiving Names Via Browser', function(done){ this.timeout(10000); var url = host + "/api/names"; var browser = new zombie.Browser(); browser.visit(url, function(err, _browser, status){ if(browser.error) { console.log("Invalid url!!! " + url); } else { console.log("Valid url!!!" + ". Status " + status); } done(); }); }); 

现在,当我从我的terminal执行命令摩卡 ,它进入browser.error条件。 但是,如果我设置我的API获取方法,它按预期工作,并进入有效的Url (否则部分)。 我想这是因为有我的API在后期的方法。

PS:我没有创build任何窗体来执行button单击查询,因为我正在开发一个移动的后端。

任何关于如何使用POST方法执行API的帮助将不胜感激。

僵尸更多的是与实际的网页进行交互,以及在发布后请求实际的forms。

为了您的testing,请使用请求模块,并自己手工制作post请求

 var request = require('request') var should = require('should') describe('URL names', function () { it('Should give error on invalid url', function(done) { // assume the following url is invalid var url = 'http://localhost:5000/api/names' var opts = { url: url, method: 'post' } request(opts, function (err, res, body) { // you will need to customize the assertions below based on your server // if server returns an actual error should.exist(err) // maybe you want to check the status code res.statusCode.should.eql(404, 'wrong status code returned from server') done() }) }) it('Should not give error on valid url', function(done) { // assume the following url is valid var url = 'http://localhost:5000/api/foo' var opts = { url: url, method: 'post' } request(opts, function (err, res, body) { // you will need to customize the assertions below based on your server // if server returns an actual error should.not.exist(err) // maybe you want to check the status code res.statusCode.should.eql(200, 'wrong status code returned from server') done() }) }) }) 

对于上面的示例代码,您将需要requestshould模块

 npm install --save-dev request should