在Frisby中访问头文件

Access headers in Frisby

本文关键字:文件 访问 Frisby      更新时间:2023-09-26

我正在使用Frisby.js库来测试一些api,并且我需要访问set-cookie头中的会话cookie以发送以下请求。

当我尝试inspectJSON()方法时,我没有看到输出中包含的标头,并且我无法识别任何方法来访问响应中的标头。任何帮助,非常感谢。谢谢!

这个例子链接了一个Login调用和一个后续调用,该调用将"set-cookie"添加到下一个调用。cookie像web浏览器一样被维护,并且第二次调用将只与传递给第二次调用的set-cookie一起工作。

var cookie = "";
frisby.create('Login')
.post('localhost:8080/login', {
    "username": "test_user_01",
    "password": "abcd1234",
}, {
    json: true
})
.inspectJSON()      // Prints response json to stdout
 .inspectHeaders()  // Prints out all the headers in response
.expectStatus(200)  // Check for http success
.expectJSONTypes({  // Check for any reported errors
    status: 1,
    message: 'success'
})
.after(function(body,res) {
    cookie = res.headers['set-cookie'];
    // Fetch account
    frisby.create('Get Accounts')
        .get(utils.URL + '/accounts?includeAllAccounts=1', {
                    headers:  {
                        "Content-Type": "application/json",
                        "Accept": "application/json",
                        "Cookie": cookie,
                    }
                })
        .inspectJSON()
        .expectStatus(200)
        .expectJSONTypes('responseMap.accounts.0', {
            //These tests will fail if the value is null
            accountNumber: String,
        })
        .toss();
}).toss();

好的,大约2分钟后张贴这个问题,我找到了答案。我能够访问.after()函数中的头文件,如下所示:

.after(function(body,res) {
    //output headers to console
    console.log('************* Headers: ' + JSON.stringify(res.headers, null, 4));
})