如何在 http 响应中对 "cache control" 进行单元测试 - spring mvc 应用程序
How to unit test "cache control" in http response - spring mvc application
我有以下过滤器来清除缓存:
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);
}
}
我想做一个单元测试,测试上面代码执行时Cache-Control
是否被设置为no-cache
。
基本上我想在 Cache-Control
响应中添加一些虚拟值 header 并执行我的单元测试以检查它是否被设置为 no-cache
例如。
我正在使用 mockito,这是我执行测试的起点,但我不知道如何为响应设置虚拟值 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);
}
}
欢迎提出任何建议。
首先,如果您使用的是 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.
为了做到这一点,我个人更喜欢使用 spring 的 HttpServletRequest
和 HttpServletResponse
的模拟实现,它们是 MockHttpServletRequest
和 MockHttpServletResponse
,分别。因此,到目前为止,您的 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!
}
然后您可以在测试中使用这些模拟实现,例如:
@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);
}
我有以下过滤器来清除缓存:
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);
}
}
我想做一个单元测试,测试上面代码执行时Cache-Control
是否被设置为no-cache
。
基本上我想在 Cache-Control
响应中添加一些虚拟值 header 并执行我的单元测试以检查它是否被设置为 no-cache
例如。
我正在使用 mockito,这是我执行测试的起点,但我不知道如何为响应设置虚拟值 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);
}
}
欢迎提出任何建议。
首先,如果您使用的是 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.
为了做到这一点,我个人更喜欢使用 spring 的 HttpServletRequest
和 HttpServletResponse
的模拟实现,它们是 MockHttpServletRequest
和 MockHttpServletResponse
,分别。因此,到目前为止,您的 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!
}
然后您可以在测试中使用这些模拟实现,例如:
@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);
}