[英]How to pass basic auth along with post request in Frisby?
Trying to test rest api. 试图测试REST API。 In my project there is no api for login. 在我的项目中,没有用于登录的api。 whenever you hit the api it is being redirect to login , takes user/pass and give you result. 每当您点击api时,它都会被重定向到login,需要用户/通过并给您结果。
I tried GET as follows which is working fine for me. 我尝试了GET如下,这对我来说很好。
var frisby = require('frisby')
var url = 'https://localhost:8443';
var api= '/api/v2/events/details'
var endpoint = '?id=ccce2aef971f'
var user= 'demo@demo.com'
var pass= 'temp'
frisby.create('GET event details based on eventid from an endpoint')
.get(url+api+endpoint,{strictSSL: false})
.auth(user,pass,false)
.expectStatus(200)
.expectHeader('Content-Type', 'application/json;charset=UTF-8')
.toss();
now I want to use POST method with same format but no luck in response. 现在我想使用相同格式的POST方法,但运气不好。
var frisby = require('frisby')
var user= 'demo@demo.com'
var pass= 'temp'
var jsonbody = {"group": "a10bbd20",
"category":"Attack",
"notes":"abcdddd",
"criteria":[ {"attribute":"PATH" ,"operator": "EQUALS", "value":"/Account/demo.aspx" }]
}
frisby.create('TEST POST')
.post('https://localhost:8443/api/v2/rule-settings/pointwise',jsonbody,{ json: true },{ headers: { 'Content-Type': 'application/json' }},{strictSSL: false})
.auth(user,pass,false)
.expectStatus(201)
.toss();
====================== ======================
I tried to do POST request by storing cookie from previous response but still same error for time out 我试图通过存储来自先前响应的cookie来执行POST请求,但超时仍然存在相同错误
var frisby = require('frisby')
var url = 'https://localhost:8443';
var api= '/api/v2/events/details'
var endpoint = '?id=ccce2aef971f'
var user= 'demo@demo.com'
var pass= 'temp'
var loginState = {
};
frisby.create('GET event details based on eventid from an endpoint')
.get(url+api+endpoint,{strictSSL: false})
.auth(user,pass,false)
.after(function (body,res) {
var cookie = res.headers['set-cookie'];
console.log(cookie);
loginState.user1 = {}; // build an object for user 1
loginState.user1.cookie = cookie;
var jsonbody = {"group": "a10bbd20",
"category":"Attack",
"notes":"abcdddd",
"criteria":[ {"attribute":"PATH" ,"operator": "EQUALS", "value":"/Account/demo.aspx" }]
}
frisby.create('TEST POST')
.post('https://localhost:8443/api/v2/rule-settings/pointwise',jsonbody,{ json: true },{strictSSL: false})
.addHeader('cookie', loginState.user1.cookie)
.addHeader('Content-Type', 'application/json')
.inspectRequest()
.expectStatus(201)
.toss();
})
.toss();
output I am getting is 我得到的输出是
Message:
Expected 500 to equal 200.
Stacktrace:
Error: Expected 500 to equal 200.
at null.<anonymous> (C:\Users\Administrator\Documents\TestSuite\node_modules
\frisby\lib\frisby.js:493:42)
at null.<anonymous> (C:\Users\Administrator\Documents\TestSuite\node_modules
\frisby\lib\frisby.js:1074:43)
at Timer.listOnTimeout (timers.js:92:15)
I found the solution after couple of search and frisby issue page. 经过几次搜索和frisby问题页面后,我找到了解决方案。
need to add environment variable to bypass certificate. 需要添加环境变量以绕过证书。 add process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; 添加process.env.NODE_TLS_REJECT_UNAUTHORIZED =“ 0”; before post method and everything is good to go. 在发布方法之前,一切都很好。
one more thing, it will work with regular post request as well. 还有一件事,它将与常规的发布请求一起使用。 no need to chain get,post together for cookies. 无需链接获取,一起发布cookie。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.