BrokerResponse getQueueResponse(String queueName) throws IOException {
    String queryUrl = getQueueEndpoint(messageVpn, queueName);
    HttpResponse response = executeGet(new GenericUrl(baseUrl + queryUrl));
    return BrokerResponse.fromHttpResponse(response);
  }
@Test
  public void testExecuteWithUnauthorized() throws IOException {
    // Making it a final array, so that we can reference it from within the MockHttpTransport
    // instance
    final int[] requestCounter = {0};
    MockHttpTransport transport =
        new MockHttpTransport() {
          @Override
          public LowLevelHttpRequest buildRequest(String method, String url) {
            return new MockLowLevelHttpRequest() {
              @Override
              public LowLevelHttpResponse execute() throws IOException {
                MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
                if (requestCounter[0] == 0) {
                  // The first request has to include Basic Auth header
                  assertTrue(this.getHeaders().containsKey("authorization"));
                  List<String> authorizationHeaders = this.getHeaders().get("authorization");
                  assertEquals(1, authorizationHeaders.size());
                  assertTrue(authorizationHeaders.get(0).contains("Basic"));
                  assertFalse(this.getHeaders().containsKey("cookie"));

                  // Set the response to include Session cookies
                  response
                      .setHeaderNames(ImmutableList.of("Set-Cookie", "Set-Cookie"))
                      .setHeaderValues(
                          ImmutableList.of(
                              "ProxySession=JddSdJaGo6FYYmQk6nt8jXxFtq6n3FCFR14ebzRGQ5w;"
                                  + " HttpOnly; SameSite=Strict;"
                                  + " Path=/proxy; Max-Age=2592000",
                              "Session=JddSdJaGo6FYYmQk6nt8jXxFtq6n3FCFR14ebzRGQ5w;"
                                  + " HttpOnly; SameSite=Strict;"
                                  + " Path=/SEMP; Max-Age=2592000"));
                  response.setStatusCode(200);
                } else if (requestCounter[0] == 1) {
                  // The second request does not include Basic Auth header
                  assertFalse(this.getHeaders().containsKey("authorization"));
                  // It must include a cookie header
                  assertTrue(this.getHeaders().containsKey("cookie"));
                  boolean hasSessionCookie =
                      this.getHeaders().get("cookie").stream()
                              .filter(
                                  c ->
                                      c.contains(
                                          "Session=JddSdJaGo6FYYmQk6nt8jXxFtq6n3FCFR14ebzRGQ5w"))
                              .count()
                          == 1;
                  assertTrue(hasSessionCookie);

                  // Let's assume the Session expired - we return the 401
                  // unauthorized
                  response.setStatusCode(401);
                } else {
                  // The second request has to be retried with a Basic Auth header
                  // this time
                  assertTrue(this.getHeaders().containsKey("authorization"));
                  List<String> authorizationHeaders = this.getHeaders().get("authorization");
                  assertEquals(1, authorizationHeaders.size());
                  assertTrue(authorizationHeaders.get(0).contains("Basic"));
                  assertFalse(this.getHeaders().containsKey("cookie"));

                  response.setStatusCode(200);
                }
                response.setContentType(Json.MEDIA_TYPE);
                requestCounter[0]++;
                return response;
              }
            };
          }
        };

    HttpRequestFactory requestFactory = transport.createRequestFactory();
    SempBasicAuthClientExecutor client =
        new SempBasicAuthClientExecutor(
            "http://host", "username", "password", "vpnName", requestFactory);

    // The first, initial request
    client.getQueueResponse("queue");
    // The second request, which will try to authenticate with a cookie, and then with Basic
    // Auth when it receives a 401 unauthorized
    client.getQueueResponse("queue");

    // There should be 3 requests executed:
    // the first one is the initial one with Basic Auth,
    // the second one uses the session cookie, but we simulate it being expired,
    // so there should be a third request with Basic Auth to create a new session.
    assertEquals(3, requestCounter[0]);
  }