### Question:
BaseUriHandler implements Handler { @Override public Promise<Response, NeverThrowsException> handle(final Context context, final Request request) { final String newBaseUri = baseUri.eval(bindings(context, request)); if (newBaseUri == null) { logger.error(format("BaseUri Expression '%s' was evaluated to null", baseUri)); return newResultPromise(newInternalServerError()); } if (request != null && request.getUri() != null) { try { request.getUri().rebase(new URI(newBaseUri)); } catch (URISyntaxException e) { logger.error(format("Invalid baseUri '%s'", baseUri)); return newResultPromise(newInternalServerError().setCause(e)); } } return delegate.handle(context, request); } BaseUriHandler(final Handler delegate, final Expression<String> baseUri, final Logger logger); @Override Promise<Response, NeverThrowsException> handle(final Context context, final Request request);  }### Answer:
@Test public void shouldRebaseUri() throws Exception { final BaseUriHandler handler = new BaseUriHandler(delegate, Expression.valueOf("http: String.class), logger); final Request request = createRequest(); handler.handle(context, request); verify(delegate).handle(context, request); assertThat(request.getUri().toString()).isEqualTo("http: verifyZeroInteractions(logger); }
@Test(expectedExceptions = NullPointerException.class) public void shouldFailWithNullExpression() throws Exception { final BaseUriHandler handler = new BaseUriHandler(delegate, null, logger); final Request request = createRequest(); handler.handle(context, request); verify(logger).error(anyString()); }
@Test public void shouldFailWhenRebasingFail() throws Exception { final BaseUriHandler handler = new BaseUriHandler(delegate, Expression.valueOf("http: String.class), logger); final Request request = createRequest(); Response response = handler.handle(context, request).get(); assertThat(response.getStatus()).isEqualTo(INTERNAL_SERVER_ERROR); assertThat(response.getEntity().getString()).isEmpty(); assertThat(response.getCause()).isInstanceOf(URISyntaxException.class); verify(logger).error(anyString()); }
@Test public void shouldReturnErrorResponseDueToUnresolvableExpression() throws Exception { final BaseUriHandler handler = new BaseUriHandler(delegate, Expression.valueOf("${EXPRESSION_ERROR}", String.class), logger); final Request request = createRequest(); final Response response = handler.handle(context, request).get(); assertThat(response.getStatus()).isEqualTo(INTERNAL_SERVER_ERROR); assertThat(response.getEntity().getString()).isEmpty(); verify(logger).error(anyString()); }