@Test public void cacheHeadersSetWhenPathMatches() { long now = System.currentTimeMillis(); WebFilterChain filterChain = (filterExchange) -> { try { HttpHeaders headers = filterExchange.getResponse().getHeaders(); assertThat(headers.getPragma()).isEqualTo("cache"); assertThat(headers.getCacheControl()).isEqualTo("max-age=172800000, public"); assertThat(headers.getExpires() - now).isBetween(ttl - 1000, ttl + 1000); } catch (AssertionError ex) { return Mono.error(ex); } return Mono.empty(); }; MockServerWebExchange exchange = MockServerWebExchange.from( MockServerHttpRequest.get("/app/foo") ); this.filter.filter(exchange, filterChain).block(); }
@Override public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { return ServerWebExchangeMatchers.pathMatchers("/i18n/**", "/content/**", "/app/**") .matches(exchange) .filter(ServerWebExchangeMatcher.MatchResult::isMatch) .doOnNext(matchResult -> { ServerHttpResponse response = exchange.getResponse(); response.getHeaders().setCacheControl("max-age=" + cacheTimeToLive + ", public"); response.getHeaders().setPragma("cache"); response.getHeaders().setExpires(cacheTimeToLive + System.currentTimeMillis()); }) .then(Mono.defer(() -> chain.filter(exchange))); }
CachingHttpHeadersFilter implements WebFilter { @Override public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { return ServerWebExchangeMatchers.pathMatchers("/i18n/**", "/content/**", "/app/**") .matches(exchange) .filter(ServerWebExchangeMatcher.MatchResult::isMatch) .doOnNext(matchResult -> { ServerHttpResponse response = exchange.getResponse(); response.getHeaders().setCacheControl("max-age=" + cacheTimeToLive + ", public"); response.getHeaders().setPragma("cache"); response.getHeaders().setExpires(cacheTimeToLive + System.currentTimeMillis()); }) .then(Mono.defer(() -> chain.filter(exchange))); } }
CachingHttpHeadersFilter implements WebFilter { @Override public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { return ServerWebExchangeMatchers.pathMatchers("/i18n/**", "/content/**", "/app/**") .matches(exchange) .filter(ServerWebExchangeMatcher.MatchResult::isMatch) .doOnNext(matchResult -> { ServerHttpResponse response = exchange.getResponse(); response.getHeaders().setCacheControl("max-age=" + cacheTimeToLive + ", public"); response.getHeaders().setPragma("cache"); response.getHeaders().setExpires(cacheTimeToLive + System.currentTimeMillis()); }) .then(Mono.defer(() -> chain.filter(exchange))); } CachingHttpHeadersFilter(Long cacheTimeToLive); }
CachingHttpHeadersFilter implements WebFilter { @Override public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { return ServerWebExchangeMatchers.pathMatchers("/i18n/**", "/content/**", "/app/**") .matches(exchange) .filter(ServerWebExchangeMatcher.MatchResult::isMatch) .doOnNext(matchResult -> { ServerHttpResponse response = exchange.getResponse(); response.getHeaders().setCacheControl("max-age=" + cacheTimeToLive + ", public"); response.getHeaders().setPragma("cache"); response.getHeaders().setExpires(cacheTimeToLive + System.currentTimeMillis()); }) .then(Mono.defer(() -> chain.filter(exchange))); } CachingHttpHeadersFilter(Long cacheTimeToLive); @Override Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain); }
CachingHttpHeadersFilter implements WebFilter { @Override public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { return ServerWebExchangeMatchers.pathMatchers("/i18n/**", "/content/**", "/app/**") .matches(exchange) .filter(ServerWebExchangeMatcher.MatchResult::isMatch) .doOnNext(matchResult -> { ServerHttpResponse response = exchange.getResponse(); response.getHeaders().setCacheControl("max-age=" + cacheTimeToLive + ", public"); response.getHeaders().setPragma("cache"); response.getHeaders().setExpires(cacheTimeToLive + System.currentTimeMillis()); }) .then(Mono.defer(() -> chain.filter(exchange))); } CachingHttpHeadersFilter(Long cacheTimeToLive); @Override Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain);  }