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

io.restassured.http.Cookies Maven / Gradle / Ivy

There is a newer version: 5.5.0
Show newest version
/*
 * Copyright 2019 the original author or authors.
 *
 * 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 io.restassured.http;

import io.restassured.internal.MultiValueEntity;
import io.restassured.internal.common.assertion.AssertParameter;

import java.util.*;

/**
 * Represents a collection of cookies
 */
public class Cookies implements Iterable {

    private final MultiValueEntity cookies;

    public Cookies(Cookie... cookies) {
        this(Arrays.asList(cookies));
    }

    public Cookies(List cookies) {
        AssertParameter.notNull(cookies, "Cookies");
        this.cookies = new MultiValueEntity(cookies);
    }

    /**
     * @return The size of the cookies
     */
    public int size() {
        return cookies.size();
    }

    /**
     * @return true if one or more cookies are defined, false otherwise.
     */
    public boolean exist() {
        return cookies.exist();
    }

    /**
     * See if a cookie with the given name exists
     *
     * @param cookieName The name of the cookie to check
     * @return true if the cookie exists
     */
    public boolean hasCookieWithName(String cookieName) {
        return cookies.hasEntityWithName(cookieName);
    }

    /**
     * @return All cookies as a list.
     */
    protected List list() {
        return cookies.list();
    }

    /**
     * Get a single cookie with the supplied name. If there are several cookies match the cookieName then
     * the first one is returned.
     *
     * @param cookieName The name of the cookie to find
     * @return The found cookie or null if no cookie was found.
     */
    public Cookie get(String cookieName) {
        AssertParameter.notNull(cookieName, "Cookie name");
        return cookies.get(cookieName);
    }

    /**
     * Get a single cookie value with the supplied name. If there are several cookies matching the cookieName then
     * the first one is returned.
     *
     * @param cookieName The name of the cookie to find
     * @return The found cookie or null if no cookie was found.
     */
    public String getValue(String cookieName) {
        AssertParameter.notNull(cookieName, "Cookie name");
        return cookies.getValue(cookieName);
    }

    /**
     * Get all cookies with the supplied name. If there's only one cookie matching the cookieName then
     * a list with only that cookie is returned.
     *
     * @param cookieName The name of the cookie to find
     * @return The found cookies or empty list if no cookie was found.
     */
    public List getList(String cookieName) {
        return cookies.getList(cookieName);
    }

    /**
     * Get all cookie values of the cookie with the supplied name. If there's only one header matching the cookieName then
     * a list with only that cookie value is returned.
     *
     * @param cookieName The name of the header to find
     * @return The found header values or empty list if no header was found.
     */
    public List getValues(String cookieName) {
        return cookies.getValues(cookieName);
    }

    /**
     * @return All headers as a list
     */
    public List asList() {
        return cookies.asList();
    }

    /**
     * @return Cookies iterator
     */
    public Iterator iterator() {
        return cookies.iterator();
    }

    /**
     * An alternative way to create a Cookies object from the constructor.
     *
     * @param cookie            The cookie to be included
     * @param additionalCookies Additional cookies to be included (optional)
     * @return A new cookies object containing the specified cookies
     */
    public static Cookies cookies(Cookie cookie, Cookie... additionalCookies) {
        AssertParameter.notNull(cookie, "Cookie");
        final List cookieList = new LinkedList();
        cookieList.add(cookie);
        Collections.addAll(cookieList, additionalCookies);
        return new Cookies(cookieList);
    }

    @Override
    public String toString() {
        return cookies.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy