### Question:
HttpClientChooser { public CloseableHttpClient chooseClient() { if (isRequestToSign()) { return clientWithCertificate; } else { return clientWithoutCertificate; } } HttpClientChooser(CloseableHttpClient clientWithoutCertificate, CloseableHttpClient clientWithCertificate); CloseableHttpClient chooseClient();  }### Answer:
@Test void givenBypassScheme_whenNoAuthentication_thenWithoutCertificate() { RequestContext.getCurrentContext().put(AUTHENTICATION_SCHEME_BY_PASS_KEY, true); assertThat(chooser.chooseClient(), is(clientWithoutCertificate)); }
@Test void givenNoBypassScheme_whenChoose_thenWithCertificate() { assertThat(chooser.chooseClient(), is(clientWithoutCertificate)); }
@Test void givenBypassScheme_whenGenericAuthentication_thenWithout() { RequestContext.getCurrentContext().put(AUTHENTICATION_SCHEME_BY_PASS_KEY, true); SecurityContextHolder.getContext().setAuthentication(mock(Authentication.class)); assertThat(chooser.chooseClient(), is(clientWithoutCertificate)); }
@Test void givenBypassSchemeAndX509Authentication_whenX509NotAuthenticated_thenWithout() { RequestContext.getCurrentContext().put(AUTHENTICATION_SCHEME_BY_PASS_KEY, true); Authentication auth = mock(Authentication.class); when(auth.getCredentials()).thenReturn(mock(X509Certificate.class)); SecurityContextHolder.getContext().setAuthentication(auth); assertThat(chooser.chooseClient(), is(clientWithoutCertificate)); }
@Test void givenBypassSchemeAndX509Authentication_whenX509Authenticated_thenWith() { RequestContext.getCurrentContext().put(AUTHENTICATION_SCHEME_BY_PASS_KEY, true); Authentication auth = mock(Authentication.class); when(auth.isAuthenticated()).thenReturn(true); when(auth.getCredentials()).thenReturn(mock(X509Certificate.class)); SecurityContextHolder.getContext().setAuthentication(auth); assertThat(chooser.chooseClient(), is(clientWithCertificate)); }
@Test void givenNoBypassSchemeX509Authentication_whenX509Authenticated_thenWithout() { Authentication auth = mock(Authentication.class); when(auth.isAuthenticated()).thenReturn(true); when(auth.getCredentials()).thenReturn(mock(X509Certificate.class)); SecurityContextHolder.getContext().setAuthentication(auth); assertThat(chooser.chooseClient(), is(clientWithoutCertificate)); }