### Question:
CrestProtocolEnforcementFilter implements Filter { @Override public Promise<Response, NeverThrowsException> filter(Context context, Request request, Handler next) { try { enforceProtocolVersion(defaultProtocolVersion(request)); return next.handle(context, request); } catch (BadRequestException e) { Response response = new Response() .setStatus(Status.valueOf(e.getCode())) .setEntity(e.toJsonValue().getObject()); return newResultPromise(response); } }  @Override Promise<Response, NeverThrowsException> filter(Context context, Request request, Handler next);  }### Answer:
@Test public void requestWithCorrectProtocolVersionShouldCallHandler() { Context context = mock(Context.class); Request request = new Request(); Handler next = mock(Handler.class); request.getHeaders().put(AcceptApiVersionHeader.valueOf("protocol=1")); filter.filter(context, request, next); assertThat(AcceptApiVersionHeader.valueOf(request).getProtocolVersion()).isEqualTo(version(1)); verify(next).handle(context, request); }
@Test public void requestWithNoProtocolVersionShouldBeDefaultedAndThenCallHandler() { Context context = mock(Context.class); Request request = new Request(); Handler next = mock(Handler.class); filter.filter(context, request, next); assertThat(AcceptApiVersionHeader.valueOf(request).getProtocolVersion()).isEqualTo(version(1)); verify(next).handle(context, request); }
@Test public void requestWithIncorrectProtocolMinorVersionShouldReturnBadRequestResponse() throws IOException { Context context = mock(Context.class); Request request = new Request(); Handler next = mock(Handler.class); request.getHeaders().put(AcceptApiVersionHeader.valueOf("protocol=1.1")); Response response = filter.filter(context, request, next).getOrThrowUninterruptibly(); assertThat(getUnsupportedMinorVersionExceptionJson(version(1, 1))).isEqualTo(response.getEntity().getJson()); assertThat(AcceptApiVersionHeader.valueOf(request).getProtocolVersion()).isEqualTo(version(1, 1)); verify(next, never()).handle(context, request); }
@Test public void requestWithIncorrectProtocolMajorVersionShouldReturnBadRequestResponse() throws IOException { Context context = mock(Context.class); Request request = new Request(); Handler next = mock(Handler.class); request.getHeaders().put(AcceptApiVersionHeader.valueOf("protocol=2")); Response response = filter.filter(context, request, next).getOrThrowUninterruptibly(); assertThat(getUnsupportedMajorVersionExceptionJson(version(2))).isEqualTo(response.getEntity().getJson()); assertThat(AcceptApiVersionHeader.valueOf(request).getProtocolVersion()).isEqualTo(version(2)); verify(next, never()).handle(context, request); }