Uploaded image for project: 'HttpComponents HttpClient'
  1. HttpComponents HttpClient
  2. HTTPCLIENT-2338

Accept-Encoding lost on request retrying

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Minor
    • Resolution: Fixed
    • 5.3.1
    • 5.4
    • HttpClient (classic)
    • None

    Description

      Make an HTTP request that is retried (e.g. response status 429). The HTTP headers of the retried request does not include an `Accept-Encoding` header.

      import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
      import org.apache.hc.client5.http.impl.classic.HttpClients;
      import org.apache.hc.client5.http.protocol.HttpClientContext;
      import org.apache.hc.core5.http.ClassicHttpRequest;
      import org.apache.hc.core5.http.impl.io.DefaultClassicHttpRequestFactory;
      import org.junit.jupiter.api.Test;
      import org.mockserver.integration.ClientAndServer;
      import org.mockserver.model.HttpRequest;
      import org.mockserver.socket.PortFactory;
      
      import java.io.IOException;
      import java.io.UncheckedIOException;
      
      import static org.assertj.core.api.Assertions.assertThat;
      import static org.mockserver.model.HttpRequest.request;
      import static org.mockserver.model.HttpResponse.response;
      
      public class ApacheTest {
      
        @Test
        void repeatedRequestHasNoAcceptEncodingHeader() {
          int serverPort = PortFactory.findFreePort();
          String serverUrl = "http://localhost:" + serverPort;
          ClientAndServer server = ClientAndServer.startClientAndServer(serverPort);
          server.when(request()).respond(response().withStatusCode(429));
      
          try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            ClassicHttpRequest request = DefaultClassicHttpRequestFactory.INSTANCE.newHttpRequest("GET", serverUrl);
            httpClient.execute(request, HttpClientContext.create(), r -> r);
          } catch (IOException e) {
            throw new UncheckedIOException(e.getMessage(), e);
          }
      
          HttpRequest[] sentRequests = server.retrieveRecordedRequests(null);
          assertThat(sentRequests).satisfiesExactly(
            first -> assertThat(first.getHeaderList()).extracting(h -> h.getName().getValue())
              .containsExactlyInAnyOrder("content-length", "Connection", "User-Agent", "Host", "Accept-Encoding"),
            second -> assertThat(second.getHeaderList()).extracting(h -> h.getName().getValue())
              .containsExactlyInAnyOrder("content-length", "Connection", "User-Agent", "Host")
          );
        }
      
      }

      Attachments

        Activity

          People

            Unassigned Unassigned
            michu4k MichaƂ Krysztofik
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: