[英]How to unit test “cache control” in http response - spring mvc application
I have the following filter to clear cache: 我有以下过滤器来清除缓存:
public class CacheControlFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
logger.info("doFilter method called clearing cache");
HttpServletResponse resp = (HttpServletResponse) response;
resp.setDateHeader("Expires", 0);
resp.setHeader("Last-Modified", new Date().toString());
resp.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0");
resp.setHeader("Pragma", "no-cache");
chain.doFilter(request, response);
}
}
I want to carry out a unit test to test if the Cache-Control
is being set to no-cache
when the above code is executed. 我想进行一次单元测试,以测试执行以上代码时是否将
Cache-Control
设置为no-cache
。
Basically I wanted to add some dummy value in the Cache-Control
response header and execute my unit test to check if it is being set to no-cache
for example. 基本上,我想在
Cache-Control
响应标头中添加一些虚拟值,并执行我的单元测试以检查例如是否将其设置为no-cache
。
I am using mockito and thats my starting point to carry out the test but i don't know how to set a dummy value Cache-Control
to the response as shown below: 我正在使用模拟和那是我进行测试的起点,但是我不知道如何为响应设置一个伪值
Cache-Control
,如下所示:
public class CacheControlFilterTest {
@InjectMocks CacheControlFilter cacheControlFilter;
@Mock ServletRequest request;
@Mock HttpServletResponse response ;
@Mock FilterChain filterChain;
public CacheControlFilterTest() {}
@Test
public void doFilterTest() throws IOException, ServletException {
cacheControlFilter.doFilter(request, response, filterChain);
}
}
Any advice is most welcome. 任何建议都是最欢迎的。
First off, you should add the Mockito Runner , if you're using JUnit: 首先,如果使用的是JUnit,则应添加Mockito Runner :
@RunWith(MockitoJUnitRunner.class)
public class CacheControlFilterTest { ... }
Basically I wanted to add some dummy value in the Cache-Control response header and execute my unit test to check if it is being set to no-cache for example.
基本上,我想在Cache-Control响应标头中添加一些虚拟值,并执行我的单元测试以检查例如是否将其设置为no-cache。
In order to do that, i would personally prefer to use spring's mock implementations of HttpServletRequest
and HttpServletResponse
, which are MockHttpServletRequest
and MockHttpServletResponse
, respectively. 为了做到这一点,我个人更喜欢使用spring的
HttpServletRequest
和HttpServletResponse
的模拟实现,分别是MockHttpServletRequest
和MockHttpServletResponse
。 So, your CacheControlFilterTest
would be something like following so far: 因此,到目前为止,您的
CacheControlFilterTest
将类似于以下内容:
@RunWith(MockitoJUnitRunner.class)
public class CacheControlFilterTest {
@InjectMocks CacheControlFilter cacheControlFilter;
@Spy MockHttpServletRequest request;
@Spy MockHttpServletResponse response;
@Mock FilterChain filterChain;
// The rest will be roll out very soon!
}
Then you can user those mock implementations in your test, something like: 然后,您可以在测试中使用这些模拟实现,例如:
@Test
public void doFilterTest() throws IOException, ServletException {
final String CACHE_CONTROL_VALUE = "no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0";
response.setHeader("Cache-Control", "some value");
cacheControlFilter.doFilter(request, response, filterChain);
assertEquals(response.getHeader("Cache-Control"), CACHE_CONTROL_VALUE);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.