如何使用JUnit对Cache Control标头进行单元测试? [英] How to Unit Test Cache Control header using JUnit?

查看:166
本文介绍了如何使用JUnit对Cache Control标头进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚创建了一个CacheControl以用于REST服务的响应,它是一个ResponseBuilder,它以ResponseBuilder作为参数,在该参数上我设置了no缓存选项.代码如下所示.

I just created a CacheControl to use on the response from REST services, it is a ResponseBuilder which takes a ResponseBuilder as parameter on which i set the no cache options. The code is shown below.

        /**
 * Cacheless method to control the Cache Header in REST responses.
 * @param builder the response builder
 * @return Cache Control Header for REST Responses
 */
private ResponseBuilder setCacheControlHeader(final ResponseBuilder builder) {
    CacheControl control = new CacheControl();
    control.setNoCache(true);
    control.setNoStore(true);
    control.setMaxAge(0);
    control.setPrivate(true);
    control.setMustRevalidate(true);
    control.setNoTransform(true);
    builder.cacheControl(control);
    builder.header("Pragma", "no-cache");
    builder.header("Expires", 0);

    return builder;
}

当我正在研究使用PostMan测试标头的方法时,这似乎很好. 现在,我试图创建一个JUnit测试,以测试传递给该方法的Response构建器是否在该方法上分配了诸如"noCache,"noStore"和"expires"之类的参数.

As I am just working on on the methods I am using PostMan to test the headers which seems to be fine. Now I am trying to create a JUnit test to test if the Response builder passed on to the method is getting assigned the parameters on the method such as "noCache, "noStore" and "expires".

我需要针对使用此方法的每个RestServices进行测试. 有人可以建议如何测试吗?

I would need to test it for each of my RestServices which is using this method. Can someone suggest how to test it?

致谢

推荐答案

使用RestAssured,它很简单:

Using RestAssured, it is as simple as:

    given().param("bookId", book.getId())
           .get("/api/books/")
           .then()
           .statusCode(HttpStatus.SC_OK)
           .header("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate");

这篇关于如何使用JUnit对Cache Control标头进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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