Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement optionalQueryParam Following Discussion in #1751 #1776

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}