首页 > 解决方案 > 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;
      }); 
});

标签: javascriptfrisby.js

解决方案


尝试这样的事情:

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();
                    })          
            });             
    });
});

推荐阅读