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

com.jayway.restassured.internal.SpecificationMerger.groovy 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.internal

import com.jayway.restassured.authentication.ExplicitNoAuthScheme
import com.jayway.restassured.config.SessionConfig
import com.jayway.restassured.response.Cookies
import com.jayway.restassured.spi.AuthFilter

import static com.jayway.restassured.internal.assertion.AssertParameter.notNull

class SpecificationMerger {

    /**
     * Merge this builder with settings from another specification. Note that the supplied specification
     * can overwrite data in the current specification. The following settings are overwritten:
     * 
    *
  • Content type
  • *
  • Root pathStatus code
  • *
  • Status line
  • *
  • Fallback parser
  • *
* The following settings are merged: *
    *
  • Response body expectations
  • *
  • Cookies
  • *
  • Headers
  • *
  • Response parser settings
  • *
*/ def static void merge(ResponseSpecificationImpl thisOne, ResponseSpecificationImpl with) { notNull thisOne, "Specification to merge" notNull with, "Specification to merge with" thisOne.contentType = with.contentType thisOne.rpr.defaultParser = with.rpr.defaultParser thisOne.rpr.additional.putAll(with.rpr.additional) thisOne.bodyMatchers << with.bodyMatchers thisOne.bodyRootPath = with.bodyRootPath thisOne.cookieAssertions.addAll(with.cookieAssertions) thisOne.expectedStatusCode = with.expectedStatusCode thisOne.expectedStatusLine = with.expectedStatusLine thisOne.headerAssertions.addAll(with.headerAssertions) } /** * Merge this builder with settings from another specification. Note that the supplied specification * can overwrite data in the current specification. The following settings are overwritten: *
    *
  • Port
  • *
  • Authentication schemeContent type
  • *
  • Request body
  • *
  • Keystore
  • *
  • URL Encoding enabled/disabled
  • *
  • Config
  • *
* The following settings are merged: *
    *
  • Parameters
  • *
  • Query Parameters
  • *
  • Form Parameters
  • *
  • Path parameters
  • *
  • Multi-part form data parameters
  • *
  • Cookies
  • *
  • Headers
  • *
  • Filters
  • *
*/ def static void merge(RequestSpecificationImpl thisOne, RequestSpecificationImpl with) { notNull thisOne, "Specification to merge" notNull with, "Specification to merge with" thisOne.port = with.port thisOne.requestParameters.putAll(with.requestParameters) thisOne.queryParameters.putAll(with.queryParams) thisOne.formParameters.putAll(with.formParams) thisOne.pathParameters.putAll(with.pathParams) thisOne.multiParts.addAll(with.multiParts) thisOne.authenticationScheme = with.authenticationScheme thisOne.keyStoreSpec = with.keyStoreSpec thisOne.contentType = with.contentType thisOne.headers(with.requestHeaders) mergeSessionId(thisOne, with) thisOne.cookies(with.cookies) thisOne.requestBody = with.requestBody mergeFilters(thisOne, with) thisOne.urlEncodingEnabled = with.urlEncodingEnabled thisOne.restAssuredConfig = with.restAssuredConfig } private static def mergeSessionId(RequestSpecificationImpl thisOne, RequestSpecificationImpl with) { def thisOneConfig = thisOne.config def thisOneCookies = thisOne.cookies def oldSessionIdName = SessionConfig.DEFAULT_SESSION_ID_NAME if(thisOneConfig != null) { oldSessionIdName = thisOneConfig.sessionConfig.sessionIdName() } def cookieList = thisOneCookies.findAll {!it.getName().equalsIgnoreCase(oldSessionIdName)} thisOne.cookies = new Cookies(cookieList); } private static def mergeFilters(RequestSpecificationImpl thisOne, RequestSpecificationImpl with) { def thisFilters = thisOne.filters; def withFilters = with.filters // Overwrite auth filters def instanceOfAuthFilter = { it instanceof AuthFilter } if((thisFilters.any(instanceOfAuthFilter) && withFilters.any(instanceOfAuthFilter)) || with.authenticationScheme instanceof ExplicitNoAuthScheme) { thisFilters.removeAll(instanceOfAuthFilter) } // Only add filters not already present def toAdd = withFilters.findAll ({ !thisFilters.contains(it) }) thisFilters.addAll (toAdd) } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy