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

com.jayway.restassured.config.SessionConfig Maven / Gradle / Ivy

There is a newer version: 2.9.0
Show newest version
/*
 * Copyright 2013 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 com.jayway.restassured.config;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;

/**
 * Configure session management for REST Assured. Here you can define a default session id value that'll be used for each request as well as
 * defining the default session id name (by default it's {@value SessionConfig#DEFAULT_SESSION_ID_NAME}).
 */
public class SessionConfig {

    public static final String DEFAULT_SESSION_ID_NAME = "JSESSIONID";
    private final String sessionIdName;
    private final String sessionIdValue;

    /**
     * Create a new session configuration  with session id name {@value #DEFAULT_SESSION_ID_NAME} and no session id value.
     */
    public SessionConfig() {
        this(DEFAULT_SESSION_ID_NAME, null);
    }

    /**
     * Create a new session configuration  with session id name {@value #DEFAULT_SESSION_ID_NAME} and with the supplied session id value.
     *
     * @param sessionIdValue The session id to use for each request.
     */
    public SessionConfig(String sessionIdValue) {
        this(DEFAULT_SESSION_ID_NAME, sessionIdValue);
    }

    /**
     * Create a new session config with the given session id name and value.
     *
     * @param sessionIdName The name of the session id, by default it's {@value #DEFAULT_SESSION_ID_NAME} 
     * @param sessionIdValue The value of the session id. This session id will be used for each request that uses this session configuration instance (unless it's overwritten by the DSL).
     *                         Default is null.  
     */
    public SessionConfig(String sessionIdName, String sessionIdValue) {
        Validate.notEmpty(sessionIdName, "Session id name cannot be empty.");
        this.sessionIdName = sessionIdName;
        this.sessionIdValue = sessionIdValue;
    }

    public boolean isSessionIdValueDefined() {
        return !StringUtils.isBlank(sessionIdValue);
    }

    /**
     * Specify the default session id to use for each request..
     *
     * @param defaultSessionId The stream
     * @return A new SessionConfig instance
     */
    public SessionConfig sessionIdValue(String defaultSessionId) {
        return new SessionConfig(sessionIdName, defaultSessionId);
    }


    /**
     * Set the session id name. This is the name of the cookie that contains the session id. By default it's {@value #DEFAULT_SESSION_ID_NAME}.
     *
     * @param sessionIdName The name of the session id variable
     * @return A new SessionConfig instance
     */
    public SessionConfig sessionIdName(String sessionIdName) {
        return new SessionConfig(sessionIdName, sessionIdValue);
    }

    /**
     * @return The session id name
     */
    public String sessionIdName() {
        return sessionIdName;
    }

    /**
     * @return The session id value
     */
    public String sessionIdValue() {
        return sessionIdValue;
    }

    /**
     * @return A static way to create a new SessionConfig instance without calling "new" explicitly. Mainly for syntactic sugar.
     */
    public static SessionConfig sessionConfig() {
        return new SessionConfig();
    }

    /**
     * Syntactic sugar.
     *
     * @return The same session config instance.
     */
    public SessionConfig and() {
        return this;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy