Skip to content

Commit

Permalink
Added new config called ParamConfig that allows you to configure how …
Browse files Browse the repository at this point in the history
…parameter types should be updated
  • Loading branch information
johanhaleby committed Oct 2, 2015
1 parent 0d32286 commit 9f0dc4c
Show file tree
Hide file tree
Showing 14 changed files with 1,005 additions and 100 deletions.
14 changes: 14 additions & 0 deletions changelog.txt
Expand Up @@ -87,6 +87,20 @@ Change log next version
You can then use a mapping function to first convert the header value to an int and then use an "integer" Hamcrest matcher: You can then use a mapping function to first convert the header value to an int and then use an "integer" Hamcrest matcher:
when().get("/something").then().header("Content-Length", Integer::parseInt, lessThan(1000)); when().get("/something").then().header("Content-Length", Integer::parseInt, lessThan(1000));
This is also implemented for the MockMvc module (issue 594). This is also implemented for the MockMvc module (issue 594).
* Added new config called ParamConfig that allows you to configure how parameter types should be updated. By default all parameters are merged so if you do:
given().queryParam("param1", "value1").queryParam("param1", "value2").when().get("/x"). ...
REST Assured will send a query string of "param1=value1&param1=value2". This is not always what you want though so from now on you can configure REST Assured to replace
values instead:
given().
config(config().paramConfig(paramConfig().queryParamsUpdateStrategy(REPLACE))).
queryParam("param1", "value1").
queryParam("param1", "value2").
when().
get("/x"). ..
REST Assured will now replace "param1" with "value2" (since it's written last) instead of merging them together. You can configure the update strategy for each
parameter type of for all parameter types:
given().config(config().paramConfig(paramConfig().replaceAllParameters())). ..
This is also implemented for the MockMvc module (but the config there is called MockMvcParamConfig) (issue 589)


Change log 2.5.0 (2015-08-09) Change log 2.5.0 (2015-08-09)
----------------------------- -----------------------------
Expand Down
@@ -0,0 +1,159 @@
/*
* Copyright 2015 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.itest.java;

import com.jayway.restassured.RestAssured;
import com.jayway.restassured.itest.java.support.WithJetty;
import org.junit.Test;

import static com.jayway.restassured.RestAssured.config;
import static com.jayway.restassured.RestAssured.given;
import static com.jayway.restassured.config.ParamConfig.UpdateStrategy.REPLACE;
import static com.jayway.restassured.config.ParamConfig.paramConfig;
import static org.hamcrest.Matchers.equalTo;

public class ParamConfigITest extends WithJetty {

@Test public void
merges_request_params_by_default() {
given().
param("list", "value1").
param("list", "value2").
when().
get("/multiValueParam").
then().
body("list", equalTo("value1,value2"));
}

@Test public void
merges_query_params_by_default() {
given().
queryParam("list", "value1").
queryParam("list", "value2").
when().
get("/multiValueParam").
then().
body("list", equalTo("value1,value2"));
}

@Test public void
merges_form_params_by_default() {
given().
formParam("list", "value1").
formParam("list", "value2").
when().
post("/multiValueParam").
then().
body("list", equalTo("value1,value2"));
}

@Test public void
replaces_request_params_when_configured_to_do_so() {
given().
config(config().paramConfig(paramConfig().requestParamsUpdateStrategy(REPLACE))).
param("list", "value1").
param("list", "value2").
queryParam("list2", "value3").
queryParam("list2", "value4").
formParam("list3", "value5").
formParam("list3", "value6").
when().
post("/threeMultiValueParam").
then().
body("list", equalTo("value2")).
body("list2", equalTo("value3,value4")).
body("list3", equalTo("value5,value6"));
}

@Test public void
replaces_query_params_when_configured_to_do_so() {
given().
config(config().paramConfig(paramConfig().queryParamsUpdateStrategy(REPLACE))).
param("list", "value1").
param("list", "value2").
queryParam("list2", "value3").
queryParam("list2", "value4").
formParam("list3", "value5").
formParam("list3", "value6").
when().
post("/threeMultiValueParam").
then().
body("list", equalTo("value1,value2")).
body("list2", equalTo("value4")).
body("list3", equalTo("value5,value6"));
}

@Test public void
replaces_form_params_when_configured_to_do_so() {
given().
config(config().paramConfig(paramConfig().formParamsUpdateStrategy(REPLACE))).
param("list", "value1").
param("list", "value2").
queryParam("list2", "value3").
queryParam("list2", "value4").
formParam("list3", "value5").
formParam("list3", "value6").
when().
post("/threeMultiValueParam").
then().
body("list", equalTo("value1,value2")).
body("list2", equalTo("value3,value4")).
body("list3", equalTo("value6"));
}

@Test public void
replaces_all_parameters_when_configured_to_do_so() {
given().
config(config().paramConfig(paramConfig().replaceAllParameters())).
param("list", "value1").
param("list", "value2").
queryParam("list2", "value3").
queryParam("list2", "value4").
formParam("list3", "value5").
formParam("list3", "value6").
when().
post("/threeMultiValueParam").
then().
body("list", equalTo("value2")).
body("list2", equalTo("value4")).
body("list3", equalTo("value6"));
}

@Test public void
merges_all_parameters_when_configured_to_do_so() {
RestAssured.config = config().paramConfig(paramConfig().replaceAllParameters());

try {
given().
config(config().paramConfig(paramConfig().mergeAllParameters())).
param("list", "value1").
param("list", "value2").
queryParam("list2", "value3").
queryParam("list2", "value4").
formParam("list3", "value5").
formParam("list3", "value6").
when().
post("/threeMultiValueParam").
then().
body("list", equalTo("value1,value2")).
body("list2", equalTo("value3,value4")).
body("list3", equalTo("value5,value6"));
} finally {
RestAssured.reset();
}
}
}
Expand Up @@ -236,6 +236,12 @@ class ScalatraRestExample extends ScalatraServlet {
anotherGreetXML anotherGreetXML
} }


post("/threeMultiValueParam") {
"{ \"list\" : \""+multiParams("list").mkString(",") +"\", " +
"\"list2\" : \"" + multiParams("list2").mkString(",") + "\", " +
"\"list3\" : \"" + multiParams("list3").mkString(",") + "\"}"
}

get("/multiValueParam") { get("/multiValueParam") {
"{ \"list\" : \""+multiParams("list").mkString(",") +"\" }" "{ \"list\" : \""+multiParams("list").mkString(",") +"\" }"
} }
Expand Down

0 comments on commit 9f0dc4c

Please sign in to comment.