javascript - frisby npm 中的 x-www-form-urlencoded 后置参数(正文)不起作用
问题描述
我正在尝试测试休息端点“ http://xxxxxxx/ j_spring_security_check ”以使用 frisby npm 包进行身份验证。
我可以在邮递员中工作,方法是选择请求正文作为“x-www-form-urlencoded”选项卡并给定我的应用程序凭据(如键值),它可以按预期正常工作。但在 frisby npm 中,我无法将请求正文设置为“x-www-form-urlencoded”。我无法使用此脚本登录。
请在这个或任何其他替代建议方面帮助我。
Here is my code:
var frisby7=require('frisby');
const qs = require('qs');
describe('API reference', function() {
var baseURL='http://xxxxxx/j_spring_security_check';
it('Simple Test with post url-encode form body request ', function() {
console.log("**********")
frisby7.globalSetup({
request: {
headers:{'Content-Type':'application/x-www-form-urlencoded'}
// headers: { 'X-Ms-Source':'api','X-Ms-Format':'xml','Authorization':'Basic c2hyZXlhIGdveWFsOm0jbWY4cDlMZ2ZAMU1xUTg='}
}
});
return frisby7.post(baseURL,
{
form: { j_username:'xxxx@xxxxx.com', j_password:'xxxx' }
}).then(function (res) { // res = FrisbyResponse object
console.log('status '+res.status);
console.log('body '+res.body);
//return res;
});
});
解决方案
尝试这样的事情:
var frisby = require("frisby");
const Joi = frisby.Joi;
var req1 = {
method: "get",
url: "pass url here",
headers : {
"Accept": "application/json",
"content-type" : "application/json",
'Authorization': 'Basic ' + Buffer.from(username + ":" + password).toString('base64') // pass username and password for //validation
},
body: {}
};
describe('spec file name', function () {
it("spec file name" + dateTime, function(){
return frisby
.setup({ request: { headers : req1.headers } })
.get(req1.url)
.expect("status", 200)
.expect("header", "Content-Type", "application/json; charset=utf-8")
.expect("jsonTypes", {
"message": Joi.string()
})
.then(function(res) {
var body = res.body;
body = JSON.parse(body);
expect(body.message).toBeDefined();
})
.then(function(res) {
var body = res.body;
body = JSON.parse(body);
var req2 = {
method: "put",
url: "pass url here",
headers : {
"Accept": "application/json",
"content-type" : "application/json",
"Authorization": "JWT " + Token // anything that you using to authenticate
},
body: {}
};
return frisby
.setup({ request: { headers : req2.headers } })
.put(req2.url)
.expect("status", 200)
.expect("header", "content-type", "application/json; charset=utf-8")
.expect("jsonTypes", {
"message": Joi.string()
})
.then(function(res) {
var body = res.body;
body = JSON.parse(body);
expect(body.message).toBeDefined();
})
});
});
});
推荐阅读
- android - 如何在 android 中使用套接字从 REST Api 获取 JSON 响应以动态膨胀 recyclerview?
- chefspec - Chefspec - 为执行资源编写单元测试,包括 cwd、命令和环境
- python - 不知道如何让我的函数在 Python 中不返回 None
- ios - 应用程序:openURL:选项:深度链接后未调用
- python - 在另一个带有 args 的 ODE 中使用 ODE 会使代码非常慢
- jquery - 了解 javascript 中的作用域和 jquery 链接
- html - NVDA 未在 div 上读取 aria-label
- swift - 如何使用 Swift KeyPaths 的异构数组
- powershell - Powershell将文件从环境变量复制到另一个环境变量的zip文件夹
- python - 如何在创建父级时清除内部类属性