简体   繁体   English

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

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

Trying to test rest api. 试图测试REST API。 In my project there is no api for login. 在我的项目中,没有用于登录的api。 whenever you hit the api it is being redirect to login , takes user/pass and give you result. 每当您点击api时,它都会被重定向到login,需要用户/通过并给您结果。

I tried GET as follows which is working fine for me. 我尝试了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();

now I want to use POST method with same format but no luck in response. 现在我想使用相同格式的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();

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

I tried to do POST request by storing cookie from previous response but still same error for time out 我试图通过存储来自先前响应的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();

output I am getting is 我得到的输出是

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)

I found the solution after couple of search and frisby issue page. 经过几次搜索和frisby问题页面后,我找到了解决方案。

need to add environment variable to bypass certificate. 需要添加环境变量以绕过证书。 add process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; 添加process.env.NODE_TLS_REJECT_UNAUTHORIZED =“ 0”; before post method and everything is good to go. 在发布方法之前,一切都很好。

one more thing, it will work with regular post request as well. 还有一件事,它将与常规的发布请求一起使用。 no need to chain get,post together for cookies. 无需链接获取,一起发布cookie。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何设置基本身份验证、内容类型和数据以在 axios 发布请求中发送? - How to set basic auth, content-type and data to send in axios post request? 如何将基本身份验证标头添加到返回html的发布请求中? - How do I add basic auth headers to a post request that returns html? 如何将基本的auth传递到selenium,而nightwatchjs不在url中 - how to pass basic auth into selenium with nightwatchjs not in the url 使对不同域和基本身份验证的服务器发出ajax POST请求是不可能的? - is making an ajax POST request to a server with a different domain and basic auth impossible? 授权 header 在使用基本身份验证 (JavaScript) 的发布请求时格式错误 - Authorization header malformed on post request with Basic auth (JavaScript) 如何通过命令行将选项传递给frisby测试 - How to pass an option in to the frisby test through command line 如何确保在frisby.js中的post方法上传递标头cookie - how can I make sure the header cookie is passed on a post method in frisby.js 如何使用 frisby 运行特定测试? - How to run specific tests with frisby? frisby.js正文内容语法 - frisby.js post body content syntax 如何将'this'与函数一起传递? - How to pass 'this' along with a function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM