我们可以在 RestAssured 中提取带有查询参数的完整 URL吗

问题描述 投票:0回答:1

我正在开发一个 RestAssured 项目,出于某些调试目的,希望打印请求发送到的完整 url。

在我正在开发的项目中,我们在测试方法中分别设置 ApiHost、BasePath、uri 和查询参数,到目前为止,我们将它们连接在一起并打印完整路径。

但是我想知道是否有一种方法可以打印请求发送到的完整路径/url(包括查询参数),而无需像我们一样添加部分。

我做了一些研究,发现“QueryableRequestSpecification”可用于提取请求详细信息。我已经使用了“queryable.getURI()”并尝试了其他一些可用的方法。但是 queryable.getURI() 提取没有查询参数的 uri。

还有其他办法吗?

rest rest-assured web-api-testing
1个回答
0
投票

您可以借助请求过滤器来实现这一点:

public static void main(String[] args) throws IOException {

    RestAssured
            .given()
            .baseUri("https://httpbin.org")
            .basePath("/get/{deviceId}")
            .queryParam("param", "value")
            .pathParam("deviceId", 123)
            .filter(new Filter() {
                @Override
                public Response filter(
                        FilterableRequestSpecification requestSpec, 
                        FilterableResponseSpecification responseSpec, 
                        FilterContext ctx) {
                            System.out.println(
                                    requestSpec.getURI()
                            );
                            return null;
                }
            })
            .get();

}

输出:

https://httpbin.org/get/123?param=value

© www.soinside.com 2019 - 2024. All rights reserved.