All Downloads are FREE. Search and download functionalities are using the official Maven repository.

okhttp3.RecordingCookieJar Maven / Gradle / Ivy

There is a newer version: 3.14.9
Show newest version
/*
 * Copyright (C) 2015 Square, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package okhttp3;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

public final class RecordingCookieJar implements CookieJar {
  private final Deque> requestCookies = new ArrayDeque<>();
  private final Deque> responseCookies = new ArrayDeque<>();

  public void enqueueRequestCookies(Cookie... cookies) {
    requestCookies.add(Arrays.asList(cookies));
  }

  public List takeResponseCookies() {
    return responseCookies.removeFirst();
  }

  public void assertResponseCookies(String... cookies) {
    List actualCookies = takeResponseCookies();
    List actualCookieStrings = new ArrayList<>();
    for (Cookie cookie : actualCookies) {
      actualCookieStrings.add(cookie.toString());
    }
    assertThat(actualCookieStrings).containsExactly(cookies);
  }

  @Override public void saveFromResponse(HttpUrl url, List cookies) {
    responseCookies.add(cookies);
  }

  @Override public List loadForRequest(HttpUrl url) {
    if (requestCookies.isEmpty()) return Collections.emptyList();
    return requestCookies.removeFirst();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy