Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

OkHttpClientBuilderTest.java 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonarqube.ws.client;
  21. import javax.net.ssl.SSLSocketFactory;
  22. import okhttp3.OkHttpClient;
  23. import org.junit.Test;
  24. import static org.assertj.core.api.Assertions.assertThat;
  25. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  26. import static org.mockito.Mockito.mock;
  27. public class OkHttpClientBuilderTest {
  28. private OkHttpClientBuilder underTest = new OkHttpClientBuilder();
  29. @Test
  30. public void build_default_instance_of_OkHttpClient() {
  31. OkHttpClient okHttpClient = underTest.build();
  32. assertThat(okHttpClient.proxy()).isNull();
  33. assertThat(okHttpClient.networkInterceptors()).hasSize(2);
  34. assertThat(okHttpClient.sslSocketFactory()).isNotNull();
  35. assertThat(okHttpClient.followRedirects()).isTrue();
  36. assertThat(okHttpClient.followSslRedirects()).isTrue();
  37. }
  38. @Test
  39. public void build_with_custom_sslSocketFactory() {
  40. SSLSocketFactory sslSocketFactory = mock(SSLSocketFactory.class);
  41. OkHttpClient okHttpClient = underTest
  42. .setSSLSocketFactory(sslSocketFactory)
  43. .build();
  44. assertThat(okHttpClient.sslSocketFactory()).isEqualTo(sslSocketFactory);
  45. }
  46. @Test
  47. public void build_follow_redirects() {
  48. OkHttpClient okHttpClientWithRedirect = underTest
  49. .setFollowRedirects(true)
  50. .build();
  51. assertThat(okHttpClientWithRedirect.followRedirects()).isTrue();
  52. assertThat(okHttpClientWithRedirect.followSslRedirects()).isTrue();
  53. OkHttpClient okHttpClientWithoutRedirect = underTest
  54. .setFollowRedirects(false)
  55. .build();
  56. assertThat(okHttpClientWithoutRedirect.followRedirects()).isFalse();
  57. assertThat(okHttpClientWithoutRedirect.followSslRedirects()).isFalse();
  58. }
  59. @Test
  60. public void build_throws_IAE_if_connect_timeout_is_negative() {
  61. assertThatThrownBy(() -> underTest.setConnectTimeoutMs(-10))
  62. .isInstanceOf(IllegalArgumentException.class)
  63. .hasMessage("Connect timeout must be positive. Got -10");
  64. }
  65. @Test
  66. public void build_throws_IAE_if_read_timeout_is_negative() {
  67. assertThatThrownBy(() -> underTest.setReadTimeoutMs(-10))
  68. .isInstanceOf(IllegalArgumentException.class)
  69. .hasMessage("Read timeout must be positive. Got -10");
  70. }
  71. }