记录Rest Assured自己的标题 [英] Logging Rest Assured's own headers

查看:47
本文介绍了记录Rest Assured自己的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问由Rest Assured注入的HTTP标头.Spring的Mock MVC使您可以通过MvcResult访问几乎所有内容,并且您可以使用此结果记录几乎所有您想要的关于请求和响应的内容.我能看到的唯一方法是在RestAssured中使用过滤器.但是,它为您提供了对请求的有限访问权限(您仅获得RequestSpecification).我知道,访问HttpClient添加的标头可能很棘手,但看起来您甚至都无法访问Rest Assured本身添加的标头.例如,我看不到任何与OAuth相关的标题,也看不到content-type或content-length.出现的唯一标头是使用例如 .contentType(ContentType.XML)

I'm trying to get access to the HTTP headers that are injected by Rest Assured. Spring's Mock MVC gives you access to pretty much everything via the MvcResult, and you can use this result to log pretty much anything you would like about the request and response. The only way I can see how to do this is in RestAssured is with a Filter. However, it gives you limited access to the request (you just get the RequestSpecification). I understand that it might be tricky to get access to headers that are added by the HttpClient, but it doesn't look like you can even get access to headers that are added by Rest Assured itself. For example, I can't see any OAuth related headers, nor content-type or content-length. The only headers that appear are those that were manually added using, for example, .contentType(ContentType.XML)

还有其他方法可以访问这些标头吗?我不需要修改请求,我只想能够记录所有请求以及由Rest Assured注入的标头.

Is there any other way to get access to those headers? I don't need to modify the request, I just want to be able to log all of it and the headers that are injected by Rest Assured.

推荐答案

我发现可以用RestAssured注册您自己的 HttpClientFactory :

I found that it's possible to register your own HttpClientFactory with RestAssured:

RestAssured.config().httpClient(
  HttpClientConfig.httpClientConfig().httpClientFactory(
    new CustomHttpClientFactory())

因此,我创建了一个新工厂,该工厂将返回一个HTTP客户端,并向其中注入一些请求和响应拦截器.

So I created a new factory that returns an HTTP client into which I inject some request and response interceptors.

public class CustomHttpClientFactory extends HttpClientConfig.HttpClientFactory {
    @Override
    public HttpClient createHttpClient() {
        DefaultHttpClient client = new DefaultHttpClient();
        client.addRequestInterceptor((request, ctx) -> {
            // do what you will 
        });
        client.addResponseInterceptor((response, ctx) -> {
            // do what you will
        });
        return client;
    }
}

这使您几乎可以完全控制请求和响应.要记住的一件事是,如果您要读取响应的实体,则应首先将其包装在 BufferedHttpEntity 中,以使其可重新读取:

This gives you almost full access to manipulate the request and response. One thing to remember is that if you're going to read from the response's entity, you should first wrap it in a BufferedHttpEntity to make it re-readable:

if (response.getEntity() != null && !response.getEntity().isRepeatable()) {
    response.setEntity(new BufferedHttpEntity(response.getEntity()));
}

我遇到的另一个问题是尝试查看OAuth相关信息时.使用RestAssured的OAuth功能时,它会在执行请求之前立即将其自己的 OAuthSigner 拦截器添加到HTTP客户端.这意味着它将始终是最后一个被调用的拦截器,并且您可能已经注入的任何拦截器都将在对请求进行签名之前被调用.因为我现在真的不需要看到签名,所以我没有对此做进一步调查,而将其留给读者练习.;)

Another problem I ran into is when trying to see the OAuth related information. When using RestAssured's OAuth functionality, it adds its own OAuthSigner interceptor to the HTTP client right before executing the request. This means that it will always be the last interceptor to be called and any interceptor you may have already injected will be called before the request ever gets signed. Because I don't really need to see the signature for now, I didn't investigate this further and I'm leaving it as an exercise for the reader. ;)

这篇关于记录Rest Assured自己的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆