Skip to content

Commit

Permalink
feat: add optionalQueryParam method
Browse files Browse the repository at this point in the history
rest-assured#1751
If this QueryParam value is null, no query parameter is added
If not null, the query parameter is added with the given parameter name and value.
  • Loading branch information
beng9re committed Mar 18, 2024
1 parent 034ebfb commit be28b24
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,15 @@ public MockMvcRequestSpecification queryParam(String parameterName, Collection<?
return this;
}

@Override
public MockMvcRequestSpecification optionalQueryParam(String parameterName, Object parameterValue) {
notNull(parameterName, "parameterName");
if (parameterValue != null) {
return queryParam(parameterName, parameterValue);
}
return this;
}

@Override
public MockMvcRequestSpecification pathParams(String firstParameterName, Object firstParameterValue, Object... parameterNameValuePairs) {
notNull(firstParameterName, "firstParameterName");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.restassured.module.mockmvc.specification;

import io.restassured.config.SessionConfig;
import io.restassured.http.*;
import io.restassured.mapper.ObjectMapper;
import io.restassured.mapper.ObjectMapperType;
Expand Down Expand Up @@ -359,6 +360,16 @@ public interface MockMvcRequestSpecification extends MockMvcRequestSender {
*/
MockMvcRequestSpecification queryParam(String parameterName, Collection<?> parameterValues);

/**
* Adds an optional query parameter to the request if the parameter value is not {@code null}.
* The parameter name must not be null and must pass the {@code notNull} validation.
*
* @param parameterName The parameter name
* @param parameterValue The value of the query parameter as an object. If this value is {@code null}, no query parameter is added.
* If not null, the query parameter is added with the given parameter name and value.
*/
MockMvcRequestSpecification optionalQueryParam(String parameterName, Object parameterValue);

/**
* Specify the path parameters that'll be sent with the request.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,31 @@ public void query_param() throws Exception {
body("_link", equalTo("http://localhost/queryParam?name=John&message=Good!"));
}

@Test
public void optional_query_param() throws Exception {
RestAssuredMockMvc.given().
standaloneSetup(new QueryParamController()).
optionalQueryParam("name", "John").
optionalQueryParam("message", "Good!").
when().
get("/queryParam").
then().log().all().
body("name", equalTo("Hello, John!")).
body("message", equalTo("Good!")).
body("_link", equalTo("http://localhost/queryParam?name=John&message=Good!"));
}

@Test
public void optional_query_param_message_is_null() throws Exception {
RestAssuredMockMvc.given().
standaloneSetup(new QueryParamController()).
optionalQueryParam("name", "John").
optionalQueryParam("message", null).
when().
get("/queryParam").
then().log().all().
statusCode(400);
}

// @formatter:on
}

0 comments on commit be28b24

Please sign in to comment.