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

时间:2017-02-15 21:54:17

标签: javascript rest frisby.js

尝试测试休息api。在我的项目中,没有用于登录的API。每当你点击api时,它都会被重定向到登录,接受用户/通行证并给你结果。

我按照以下方式尝试了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)

1 个答案:

答案 0 :(得分:0)

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

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

还有一件事,它也适用于常规的帖子请求。无需链接获取,一起发布cookie。