首页 > 解决方案 > 如何使用放心获取初始 URL 的 Http 状态码?

问题描述

假设我们有一个 URL http://example1.com并且我们有 301 URL 重定向设置,它重定向到http://example2.com

到达http://example2.com后,返回代码 200。

我正在使用以下放心代码来获取 HTTP 状态:

public int httpResponseCodeViaGet(String url) {
    System.out.println("Checking Status code for URL : "+ url);
    int code = RestAssured.get(url).statusCode();
    System.out.println("Status code is : "+ code);
    return code;
}

这是从 URL http://example2.com返回 200 ,我想检查我的初始 URL http://example1.com的重定向状态

如何使用放心获取初始 URL 的 Http 状态码。

谢谢你,贾吉特

标签: rest-assured

解决方案


你可以这样尝试

@Test
public void followsRedirectsByDefault() throws Exception {
        given().
        param("url1", "/hello").
        expect().statusCode(302).
        when().
        get("url2");
}

我还建议参考包含不同场景的类似案例的放心测试的官方文档。


推荐阅读