1

我正在尝试测试休息端点“ 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;
      }); 
});
4

2 回答 2

0

尝试这样的事情:

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();
                    })          
            });             
    });
});
于 2019-02-09T11:32:24.883 回答
0

您当前正在发送正文中的对象,就好像您正在使用'multipart/form-data'. 根据需要发送请求,'application/x-www-form-urlencoded'对每个属性进行 URI 编码,然后将它们作为查询字符串发布

像这样试试

var objToSend = { j_username:'xxxx@xxxxx.com', j_password:'xxxx' };
var uriObj = Object.keys(objToSend).map(key => encodeURIComponent(key) + '=' + encodeURIComponent(objToSend[key])).join('&');
var url = baseURL + '?' + uriObj
frisby7.post(url);
于 2018-12-30T07:42:41.970 回答