BrokerApiVersionInterceptor extends HandlerInterceptorAdapter { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServiceBrokerApiVersionException { if (version != null && !anyVersionAllowed()) { String apiVersion = request.getHeader(version.getBrokerApiVersionHeader()); if (!version.getApiVersion().equals(apiVersion)) { throw new ServiceBrokerApiVersionException(version.getApiVersion(), apiVersion); } } return true; } BrokerApiVersionInterceptor();  BrokerApiVersionInterceptor(BrokerApiVersion version); boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object handler);  }
@Test public void noBrokerApiVersionConfigured() throws IOException, ServletException, ServiceBrokerApiVersionException { BrokerApiVersionInterceptor interceptor = new BrokerApiVersionInterceptor(null); assertTrue(interceptor.preHandle(request, response, null)); }
@Test public void anyVersionAccepted() throws IOException, ServletException, ServiceBrokerApiVersionException { String header = "header"; String version = BrokerApiVersion.API_VERSION_ANY; when(brokerApiVersion.getBrokerApiVersionHeader()).thenReturn(header); when(brokerApiVersion.getApiVersion()).thenReturn(version); when(request.getHeader(header)).thenReturn("version"); BrokerApiVersionInterceptor interceptor = new BrokerApiVersionInterceptor(brokerApiVersion); assertTrue(interceptor.preHandle(request, response, null)); verify(brokerApiVersion, atLeastOnce()).getApiVersion(); }
@Test public void versionsMatch() throws IOException, ServletException, ServiceBrokerApiVersionException { String header = "header"; String version = "version"; when(brokerApiVersion.getBrokerApiVersionHeader()).thenReturn(header); when(brokerApiVersion.getApiVersion()).thenReturn(version); when(request.getHeader(header)).thenReturn(version); BrokerApiVersionInterceptor interceptor = new BrokerApiVersionInterceptor(brokerApiVersion); assertTrue(interceptor.preHandle(request, response, null)); verify(brokerApiVersion, atLeastOnce()).getApiVersion(); }
@Test(expected = ServiceBrokerApiVersionException.class) public void versionMismatch() throws IOException, ServletException, ServiceBrokerApiVersionException { String header = "header"; String version = "version"; String notVersion = "not_version"; when(brokerApiVersion.getBrokerApiVersionHeader()).thenReturn(header); when(brokerApiVersion.getApiVersion()).thenReturn(version); when(request.getHeader(header)).thenReturn(notVersion); BrokerApiVersionInterceptor interceptor = new BrokerApiVersionInterceptor(brokerApiVersion); interceptor.preHandle(request, response, null); verify(brokerApiVersion).getBrokerApiVersionHeader(); verify(brokerApiVersion).getApiVersion(); }