ApiVersionInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { if (version != null && !anyVersionAllowed()) { String apiVersion = request.getHeader(version.getBrokerApiVersionHeader()); if (apiVersion == null) { throw new ServiceBrokerApiVersionMissingException(version.getApiVersion()); } if (!version.getApiVersion().equals(apiVersion)) { throw new ServiceBrokerApiVersionException(version.getApiVersion(), apiVersion); } } return true; } ApiVersionInterceptor();  ApiVersionInterceptor(BrokerApiVersion version); @Override boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler);  }
@Test void noBrokerApiVersionConfigured() { ApiVersionInterceptor interceptor = new ApiVersionInterceptor(); assertThat(interceptor.preHandle(request, response, null)).isTrue(); }
@Test void anyVersionAccepted() { BrokerApiVersion brokerApiVersion = new BrokerApiVersion("header", BrokerApiVersion.API_VERSION_ANY); given(request.getHeader("header")).willReturn("9.9"); ApiVersionInterceptor interceptor = new ApiVersionInterceptor(brokerApiVersion); assertThat(interceptor.preHandle(request, response, null)).isTrue(); }
@Test void versionsMatch() { BrokerApiVersion brokerApiVersion = new BrokerApiVersion("header", "9.9"); given(request.getHeader("header")).willReturn("9.9"); ApiVersionInterceptor interceptor = new ApiVersionInterceptor(brokerApiVersion); assertThat(interceptor.preHandle(request, response, null)).isTrue(); }
@Test void versionMismatch() { BrokerApiVersion brokerApiVersion = new BrokerApiVersion("header", "9.9"); given(request.getHeader("header")).willReturn("8.8"); ApiVersionInterceptor interceptor = new ApiVersionInterceptor(brokerApiVersion); assertThrows(ServiceBrokerApiVersionException.class, () -> interceptor.preHandle(request, response, null)); }
@Test void versionHeaderIsMissing() { BrokerApiVersion brokerApiVersion = new BrokerApiVersion("header", "9.9"); given(request.getHeader("header")).willReturn(null); ApiVersionInterceptor interceptor = new ApiVersionInterceptor(brokerApiVersion); assertThrows(ServiceBrokerApiVersionMissingException.class, () -> interceptor.preHandle(request, response, null)); }
@Test void versionHeaderIsMissingAnyVersionAccepted() { BrokerApiVersion brokerApiVersion = new BrokerApiVersion("header", BrokerApiVersion.API_VERSION_ANY); given(request.getHeader("header")).willReturn(null); ApiVersionInterceptor interceptor = new ApiVersionInterceptor(brokerApiVersion); assertThat(interceptor.preHandle(request, response, null)).isTrue(); }