简体   繁体   中英

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

Trying to test rest api. In my project there is no api for login. whenever you hit the api it is being redirect to login , takes user/pass and give you result.

I tried GET as follows which is working fine for me.

    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.

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

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.

need to add environment variable to bypass certificate. add 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM