Testing 检查响应头';邮递员测试中的s值

Testing 检查响应头';邮递员测试中的s值,testing,automated-tests,postman,Testing,Automated Tests,Postman,我想检查来自具体响应标题(“位置”)的值,作为Postman中的测试结果。 在Postman的文档中,我找到了如何使用 pm.test("Content-Type is present", function () { pm.response.to.have.header("Content-Type"); }); 但我要找的是 pm.test("Location value is correct", function () { CODE HERE THAT CHECKS "Locat

我想检查来自具体响应标题(“位置”)的值,作为Postman中的测试结果。 在Postman的文档中,我找到了如何使用

pm.test("Content-Type is present", function () {
   pm.response.to.have.header("Content-Type");
});
但我要找的是

pm.test("Location value is correct", function () {
   CODE HERE THAT CHECKS "Location" HEADER EQUALS TO SOMETHING;
});

我终于找到了解决办法:

pm.test("Redirect location is correct", function () {
   pm.response.to.have.header("Location");
   pm.response.to.be.header("Location", "http://example.com/expected-redirect-url");
});

下面是在测试部分获取特定响应头的另一种方法

loc=pm.response.headers.get(“位置”)

以防万一,如果后续请求需要特定的信息,如标题值,那么您也可以将其存储/设置为环境变量,如下所示,并进一步重用

pm.environment.set(“redirrl”,loc)

优点是-变量中收集的值可以操纵。

但这完全取决于形势。例如,您可能希望提取和预/后处理重定向URL

比如说,

在运行测试集合时,您希望收集变量中的值,并将其更改为指向模拟服务器的主机:端口具有方法
has(item,valueopt)→ {Boolean}
,因此检查标头的最简单方法是:

const base_url = pm.variables.get("base_url")

pm.test("Redirect to OAuth2 endpoint", () => {
    pm.expect(pm.response.headers.has("Location",`${base_url}/oauth2/`)).is.true
})
pm.test(“位置值正确”),函数(){
expect(pm.response.headers.get('Location')).to.eql('http://google.com');

});Postman还支持ES6/ES2015语法,允许我们使用箭头函数

下面是如何通过一个简单的测试来验证公共响应头是否存在:

pm.test("Verify response headers are present ", () => {
    pm.response.to.have.header("Date");
    pm.response.to.have.header("Content-Length");
    pm.response.to.have.header("Content-Type");
});

当然,您可以检查API返回的任何自定义头。

非常感谢您!你是沙盒!:)对于Postman v7.35.0,请记住关闭“设置”中的“自动跟踪重定向”以查看重定向响应。
pm.test("Verify response headers are present ", () => {
    pm.response.to.have.header("Date");
    pm.response.to.have.header("Content-Length");
    pm.response.to.have.header("Content-Type");
});