|
| 1 | +package io.simplelocalize.cli.client; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 5 | +import uk.org.webcompere.systemstubs.environment.EnvironmentVariables; |
| 6 | +import uk.org.webcompere.systemstubs.jupiter.SystemStub; |
| 7 | +import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension; |
| 8 | + |
| 9 | +import java.net.http.HttpClient; |
| 10 | +import java.time.Duration; |
| 11 | +import java.time.temporal.ChronoUnit; |
| 12 | + |
| 13 | +import static org.assertj.core.api.Assertions.assertThat; |
| 14 | + |
| 15 | +@ExtendWith(SystemStubsExtension.class) |
| 16 | +class HttpClientFactoryTest |
| 17 | +{ |
| 18 | + |
| 19 | + @SystemStub |
| 20 | + private EnvironmentVariables environmentVariables; |
| 21 | + |
| 22 | + |
| 23 | + @Test |
| 24 | + void createHttpClient() |
| 25 | + { |
| 26 | + //when |
| 27 | + HttpClient result = HttpClientFactory.createHttpClient(); |
| 28 | + |
| 29 | + //then |
| 30 | + assertThat(result).isNotNull(); |
| 31 | + assertThat(result.connectTimeout().get()).isEqualTo(Duration.of(5, ChronoUnit.MINUTES)); |
| 32 | + assertThat(result.version()).isEqualTo(HttpClient.Version.HTTP_2); |
| 33 | + assertThat(result.followRedirects()).isEqualTo(HttpClient.Redirect.NEVER); |
| 34 | + assertThat(result.proxy()).isEmpty(); |
| 35 | + assertThat(result.authenticator()).isEmpty(); |
| 36 | + assertThat(result.cookieHandler()).isEmpty(); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void createHttpClientWithUnauthenticatedProxy() |
| 41 | + { |
| 42 | + //given |
| 43 | + environmentVariables.set("http_proxy", "http://proxy:8080"); |
| 44 | + |
| 45 | + //when |
| 46 | + HttpClient result = HttpClientFactory.createHttpClient(); |
| 47 | + |
| 48 | + //then |
| 49 | + assertThat(result).isNotNull(); |
| 50 | + assertThat(result.connectTimeout().get()).isEqualTo(Duration.of(5, ChronoUnit.MINUTES)); |
| 51 | + assertThat(result.version()).isEqualTo(HttpClient.Version.HTTP_2); |
| 52 | + assertThat(result.followRedirects()).isEqualTo(HttpClient.Redirect.NEVER); |
| 53 | + |
| 54 | + assertThat(result.proxy()).isPresent(); |
| 55 | + |
| 56 | + assertThat(result.authenticator()).isEmpty(); |
| 57 | + assertThat(result.cookieHandler()).isEmpty(); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void createHttpClientWithAuthenticatedProxy() |
| 62 | + { |
| 63 | + //given |
| 64 | + environmentVariables.set("http_proxy", "http://foo:bar@proxy:8080"); |
| 65 | + |
| 66 | + //when |
| 67 | + HttpClient result = HttpClientFactory.createHttpClient(); |
| 68 | + |
| 69 | + //then |
| 70 | + assertThat(result).isNotNull(); |
| 71 | + assertThat(result.connectTimeout().get()).isEqualTo(Duration.of(5, ChronoUnit.MINUTES)); |
| 72 | + assertThat(result.version()).isEqualTo(HttpClient.Version.HTTP_2); |
| 73 | + assertThat(result.followRedirects()).isEqualTo(HttpClient.Redirect.NEVER); |
| 74 | + |
| 75 | + assertThat(result.proxy()).isPresent(); |
| 76 | + assertThat(result.authenticator()).isPresent(); |
| 77 | + |
| 78 | + assertThat(result.cookieHandler()).isEmpty(); |
| 79 | + } |
| 80 | +} |
0 commit comments