[英]Performing a RESTcall in Mocha usning node.js
I Am trying to perform a REST call in Mocha.我正在尝试在 Mocha 中执行 REST 调用。 I´tried to use the "request" framework to login at auth0.我尝试使用“请求”框架在 auth0 登录。 The REST call is correct, I tried the same data in Postman and got the correct answer. REST 调用是正确的,我在 Postman 中尝试了相同的数据并得到了正确的答案。
Now I am trying to implement the call in our test, but this doesn't work.现在我试图在我们的测试中实现调用,但这不起作用。 The methode doesn't open the call-back function and I'm not getting any answer from the server.该方法没有打开回调函数,我没有从服务器得到任何答复。 In the backend-log I could see, that the call doesn't get through to the backend, and no login is performed.在后端日志中,我可以看到,调用没有通过后端,也没有执行登录。 I tried several different methods from XMLHttpRequest to the request library.我尝试了几种不同的方法,从 XMLHttpRequest 到请求库。
Here the code I am using:这里是我使用的代码:
var should = require("should");
var assert = require('assert');
var request = require("request");
var authToken ='test';
var url = 'https://*****.eu.auth0.com/oauth/ro';
describe('auth0', function() {
describe('Login', function() {
it('should return authToken if user data is valid', function() {
//REST call to login
request.post({url:"https://********.eu.auth0.com/oauth/ro",
form: {
client_id:'*******************',
username:'*******************',
password:'*******************',
connection:'Username-Password-Authentication',
grant_type:'password',
scope:'openid'}},
function(err,httpResponse,body){
console.log('entered call-back function');
console.log(httpResponse);
});
console.log('accessToken: ' + authToken);
});
});
});
And here is what I get after running the code:这是我运行代码后得到的:
"C:\Program Files\nodejs\node.exe" "C:\Users\*******\AppData\Roaming\npm\node_modules\mocha\bin\_mocha" --ui bdd --reporter "C:\Program Files (x86)\JetBrains\WebStorm 2016.2.2\plugins\NodeJS\js\mocha-intellij\lib\mochaIntellijReporter.js" "C:\Users\*********\Desktop\********\Testing"
accessToken:test
Process finished with exit code 0
Hope you could help me.希望你能帮助我。
Thank you!谢谢!
You have to make the test run async
.您必须使测试运行async
。 You can add a argument in the mocha callback and then invoque it when the request is done:您可以在 mocha 回调中添加一个参数,然后在请求完成时调用它:
it('should return authToken if user data is valid', function(done) {
//REST call to login
request.post({
url: "https://********.eu.auth0.com/oauth/ro",
form: {
client_id: '*******************',
username: '*******************',
password: '*******************',
connection: 'Username-Password-Authentication',
grant_type: 'password',
scope: 'openid'
}
},
function(err, httpResponse, body) {
console.log('entered call-back function');
console.log(httpResponse);
done(); // <-- here you tell mocha the async part is done
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.