下面是我的构造函数“ Req”和原型函数“ get”来发出http请求。
var request = require('request');
function Req(url, type, body) {
this.url = url;
this.username = "##############";
this.password = "##############";
this.type = type;
this.body = body
this.authenticationHeader = "Basic " + new Buffer(this.username + ":" + this.password).toString("base64");
}
Req.prototype.get = function (callback) {
request(
{
url: this.url,
headers: { "Authorization": this.authenticationHeader }
},
function (error, response, body) {
if (error) {
console.log("Error in Get Request is " + error)
}
else if (body) {
console.log("******************GET Response is****************" + "\n" + body)
}
callback();
}
);
}
中这样调用此原型函数
var testObject;
Given('I have request with {string}, {string} and {string}', function (url, type, body) {
testObject = new Req(url, type, body)
});
When('the request is triggered', function (callback) {
testObject.get(callback);
})
但是我只想显示The Step中的“ Body”响应值
Then('I should get the result', function () {
How to do here??
});
感谢您的帮助。
答案 0 :(得分:0)
将error
和body
的值传递到回调中,以便可以看到这些值。
Req.prototype.get = function(callback) {
request({
url: this.url,
headers: {
"Authorization": this.authenticationHeader
}
}, function(error, response, body) {
if (error) {
console.log("Error in Get Request is " + error)
callback(error);
} else {
console.log("******************GET Response is****************" + "\n" + body)
callback(null, body);
}
}
);
}
someObj.get(function(err, body) {
if (!err) {
console.log(body);
}
});
仅供参考,我不了解量角器和黄瓜,所以我向您展示了一种纯Javascript方式。