Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用SpringTestMVC定制http头测试_Java_Spring_Spring Mvc_Spring Test Mvc - Fatal编程技术网

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"));