簡體   English   中英

frisby npm中的x-www-form-urlencoded后參數(正文)不起作用

[英]x-www-form-urlencoded post parameters (body) in frisby npm not working

我正在嘗試測試其余端點' 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;
      }); 
});

當前,您正在將對象發送到主體中,就像您正在使用'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);

嘗試這樣的事情:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM