ApiVersionWebFilter implements WebFilter { @Override public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { PathPattern p = new PathPatternParser().parse(V2_API_PATH_PATTERN); Mono<Void> filterMono = chain.filter(exchange); if (p.matches(exchange.getRequest().getPath()) && version != null && !anyVersionAllowed()) { String apiVersion = exchange.getRequest().getHeaders().getFirst(version.getBrokerApiVersionHeader()); ServerHttpResponse response = exchange.getResponse(); String message = null; if (apiVersion == null) { response.setStatusCode(HttpStatus.BAD_REQUEST); message = ServiceBrokerApiVersionErrorMessage.from(version.getApiVersion(), "null").toString(); } else if (!version.getApiVersion().equals(apiVersion)) { response.setStatusCode(HttpStatus.PRECONDITION_FAILED); message = ServiceBrokerApiVersionErrorMessage.from(version.getApiVersion(), apiVersion) .toString(); } if (message != null) { String json; try { json = new ObjectMapper().writeValueAsString(new ErrorMessage(message)); } catch (JsonProcessingException e) { json = "{}"; } Flux<DataBuffer> responseBody = Flux.just(json) .map(s -> toDataBuffer(s, response.bufferFactory())); filterMono = response.writeWith(responseBody); } } return filterMono; } ApiVersionWebFilter();  ApiVersionWebFilter(BrokerApiVersion version); @Override Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain);  }
@Test void noBrokerApiVersionConfigured() { setUpVersionResponse("9.9"); ApiVersionWebFilter webFilter = new ApiVersionWebFilter(); webFilter.filter(exchange, chain).block(); assertThat(exchange.getResponse().getStatusCode()).isEqualTo(HttpStatus.OK); }
@Test void anyVersionAccepted() { setUpVersionResponse("9.9"); BrokerApiVersion brokerApiVersion = new BrokerApiVersion("header", BrokerApiVersion.API_VERSION_ANY); ApiVersionWebFilter webFilter = new ApiVersionWebFilter(brokerApiVersion); webFilter.filter(exchange, chain).block(); assertThat(exchange.getResponse().getStatusCode()).isEqualTo(HttpStatus.OK); }
@Test void versionsMatch() { setUpVersionResponse("9.9"); BrokerApiVersion brokerApiVersion = new BrokerApiVersion("header", "9.9"); ApiVersionWebFilter webFilter = new ApiVersionWebFilter(brokerApiVersion); webFilter.filter(exchange, chain).block(); assertThat(exchange.getResponse().getStatusCode()).isEqualTo(HttpStatus.OK); }
@Test void versionMismatch() { setUpVersionResponse("9.9"); BrokerApiVersion brokerApiVersion = new BrokerApiVersion("header", "8.8"); ApiVersionWebFilter webFilter = new ApiVersionWebFilter(brokerApiVersion); webFilter.filter(exchange, chain).block(); assertThat(exchange.getResponse().getStatusCode()).isEqualTo(HttpStatus.PRECONDITION_FAILED); }
@Test void versionHeaderIsMissing() { setUpVersionResponse(null); BrokerApiVersion brokerApiVersion = new BrokerApiVersion("header", "9.9"); ApiVersionWebFilter webFilter = new ApiVersionWebFilter(brokerApiVersion); webFilter.filter(exchange, chain).block(); assertThat(exchange.getResponse().getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST); }
@Test void versionHeaderIsMissingAnyVersionAccepted() { setUpVersionResponse(null); BrokerApiVersion brokerApiVersion = new BrokerApiVersion("header", BrokerApiVersion.API_VERSION_ANY); ApiVersionWebFilter webFilter = new ApiVersionWebFilter(brokerApiVersion); webFilter.filter(exchange, chain).block(); assertThat(exchange.getResponse().getStatusCode()).isEqualTo(HttpStatus.OK); }