You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GzipRejectorInterceptorTest.java 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 java.io.IOException;
  22. import java.util.List;
  23. import okhttp3.Headers;
  24. import okhttp3.Interceptor;
  25. import okhttp3.Request;
  26. import okhttp3.Response;
  27. import org.junit.Before;
  28. import org.junit.Test;
  29. import static org.mockito.ArgumentMatchers.any;
  30. import static org.mockito.Mockito.mock;
  31. import static org.mockito.Mockito.times;
  32. import static org.mockito.Mockito.verify;
  33. import static org.mockito.Mockito.when;
  34. public class GzipRejectorInterceptorTest {
  35. private final GzipRejectorInterceptor underTest = new GzipRejectorInterceptor();
  36. private Interceptor.Chain chain = mock();
  37. private Response response = mock(Response.class);
  38. private Request request = mock(Request.class);
  39. private Request.Builder builderThatRemovesHeaders = mock(Request.Builder.class);
  40. @Before
  41. public void before() throws IOException {
  42. when(builderThatRemovesHeaders.removeHeader(any())).thenReturn(builderThatRemovesHeaders);
  43. when(builderThatRemovesHeaders.build()).thenReturn(request);
  44. when(request.newBuilder()).thenReturn(builderThatRemovesHeaders);
  45. when(chain.request()).thenReturn(request);
  46. when(chain.proceed(any())).thenReturn(response);
  47. }
  48. @Test
  49. public void intercept_shouldAlwaysRemoveAcceptEncoding() throws IOException {
  50. underTest.intercept(chain);
  51. verify(builderThatRemovesHeaders, times(1)).removeHeader("Accept-Encoding");
  52. }
  53. @Test
  54. public void intercept_whenGzipContentEncodingIncluded_shouldCloseTheResponse() throws IOException {
  55. when(response.headers("Content-Encoding")).thenReturn(List.of("gzip"));
  56. underTest.intercept(chain);
  57. verify(response, times(1)).close();
  58. }
  59. @Test
  60. public void intercept_whenGzipContentEncodingNotIncluded_shouldNotCloseTheResponse() throws IOException {
  61. when(response.headers()).thenReturn(Headers.of("Custom-header", "not-gzip"));
  62. underTest.intercept(chain);
  63. verify(response, times(0)).close();
  64. }
  65. }