If-Modified-Since标头不适用于RestTemplate

时间:2018-11-18 15:15:36

标签: spring spring-mvc

我用Last-Modified: Tue, 13 Nov 2018 18:22:52 GMT调用URL。因此,如果我用标头If-Modified-Since: Sun, 18 Nov 2018 11:20:00 GMT调用相同的URL,则期望得到304响应,但得到200。

卷曲测试

λ curl -I https://stackoverflow.com/questions/53267836/term-aggregation-on-filtered-array-items
HTTP/1.1 200 OK
Last-Modified: Tue, 13 Nov 2018 18:22:52 GMT

λ curl -I https://stackoverflow.com/questions/53267836/term-aggregation-on-filtered-array-items -H "If-Modified-Since: Sun, 18 Nov 2018 11:20:00 GMT"
HTTP/1.1 304 Not Modified

Spring集成测试

@RunWith(SpringRunner.class)
public class RestTemplateIT {

    @Autowired
    private RestTemplate restTemplate;

    @Test
    public void testIfModifiedSince() {
        HttpHeaders headers = new HttpHeaders();
        headers.setIfModifiedSince(System.currentTimeMillis());
        HttpEntity<?> requestEntity = new HttpEntity<>(headers);
        ResponseEntity<String> response = restTemplate.exchange("https://stackoverflow.com/questions/53267836/term-aggregation-on-filtered-array-items", HttpMethod.GET, requestEntity, String.class);
        assertEquals(HttpStatus.NOT_MODIFIED, response.getStatusCode());
    }

    @SpringBootApplication
    static class TestConfiguration {
        @Bean
        public RestTemplate restTemplate() {
            return new RestTemplate();
        }
    }
}

headers debug value

0 个答案:

没有答案