Java 使用SpringTestMVC定制http头测试
我正在用Java 使用SpringTestMVC定制http头测试,java,spring,spring-mvc,spring-test-mvc,Java,Spring,Spring Mvc,Spring Test Mvc,我正在用spring-test-MVC测试我的MVC服务,我使用了如下方法: MockMvc mockMvc = standaloneSetup(controller).build(); mockMvc.perform(get("<my-url>")).andExpect(content().bytes(expectedBytes)).andExpect(content().type("image/png")) .andExpect(header().string("c
spring-test-MVC
测试我的MVC服务,我使用了如下方法:
MockMvc mockMvc = standaloneSetup(controller).build();
mockMvc.perform(get("<my-url>")).andExpect(content().bytes(expectedBytes)).andExpect(content().type("image/png"))
.andExpect(header().string("cache-control", "max-age=3600"));
MockMvc MockMvc=standaloneSetup(controller.build();
mockMvc.perform(get(“”).andExpect(content().bytes(expectedBytes)).andExpect(content().type(“image/png”))
.andExpect(header().string(“缓存控制”,“最大年龄=3600”);
效果很好
现在我将缓存图像更改为特定范围内的随机图像。例如,它可能不是
3600
,而是3500-3700
。我正在试图找出如何获得标题值并对其进行一些测试,而不是使用和expect
的模式。也许你是指这样的模式
MvcResult mvcResult = mvc.perform(get("/")).andReturn();
String headerValue = mvcResult.getResponse().getHeader("headerName");
要添加更详细的内容来说明Allow的答案:如果您在代码中还可以访问JAX-RS实现,那么可以使用CacheControl对象进行非常明确的测试(例如使用hamcrest matchers):
最好的方法是spring测试的
MockMvcResultMatchers.header()
mockMvc.perform(MockMvcRequestBuilders.get("/api"))
.andExpect(MockMvcResultMatchers.header()
.stringValues("count", "150"));
mockMvc.perform(MockMvcRequestBuilders.get("/api"))
.andExpect(MockMvcResultMatchers.header()
.stringValues("count", "150"));