如何在 Frisby 中传递基本身份验证和 post 请求?

How to pass basic auth along with post request in Frisby?

正在尝试测试休息 api。在我的项目中没有 api 用于登录。每当你点击 api 它被重定向到登录,需要 user/pass 并给你结果。

我按如下方式尝试了 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();

现在我想使用具有相同格式的 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();

======================

我尝试通过存储来自先前响应的 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();

我得到的输出是

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)

我在几个搜索和 frisby 问题页面后找到了解决方案。

需要添加环境变量来绕过证书。 在 post 方法之前添加 process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; 一切都很好。

还有一件事,它也适用于常规 post 请求。无需将 getpost 链接在一起以获得 cookie。