Skip to content

Commit

Permalink
chore(#100): add sample with exploded query parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
bbortt committed May 14, 2024
1 parent 2b2b4ef commit d3a82da
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 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 org.citrusframework.simulator.sample.scenario;

import org.citrusframework.http.message.HttpMessage;
import org.citrusframework.simulator.scenario.AbstractSimulatorScenario;
import org.citrusframework.simulator.scenario.Scenario;
import org.citrusframework.simulator.scenario.ScenarioRunner;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Map;

import static org.citrusframework.actions.EchoAction.Builder.echo;

@Scenario("Parameter")
@RequestMapping(value = "/services/rest/simulator/parameter", method = RequestMethod.GET)
public class ParameterScenario extends AbstractSimulatorScenario {

@Override
public void run(ScenarioRunner scenario) {
scenario.$(scenario.http()
.receive()
.get()
.getMessageBuilderSupport()
.extract((message, testContext) -> {
if (message instanceof HttpMessage httpMessage) {
testContext.addVariables(Map.of("pulver", httpMessage.getQueryParams().get("exploded")));
}
}));

scenario.$(echo("Extracted dangerous parameters: ${pulver}"));

scenario.$(scenario.http()
.send()
.response(HttpStatus.OK)
.message()
.body(
// language=json
"""
{
"pulver": ${pulver}
}
"""
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,28 @@ public void testInterveningRequest() {
.body(defaultResponse));
}

@CitrusTest
public void testSimulationWithExplodingQueryParams() {
$(http().client(simulatorClient)
.send()
.get("parameter")
.queryParam("exploded", "1")
.queryParam("exploded", "2"));

$(http().client(simulatorClient)
.receive()
.response(HttpStatus.OK)
.message()
.body(
// language=json
"""
{
"pulver": [1, 2]
}
"""
));
}

/**
* Sends a request to the server, expecting it to purposefully fail a simulation. The response code must therefore
* be {@link HttpStatus#OK}.
Expand All @@ -240,7 +262,19 @@ public void testSimulationFailingExpectantly() {

$(http().client(simulatorClient)
.receive()
.response(HttpStatus.OK));
.response(HttpStatus.OK)
.message()
.body(
// language=json
"""
{
"timestamp":"@ignore@",
"status":555,
"error":"Http Status 555",
"path":"/services/rest/simulator/throw"
}
"""
));
}

/**
Expand All @@ -264,13 +298,13 @@ public void testSimulationWithUnexpectedError() {
.body(
// language=json
"""
{
"timestamp":"@ignore@",
"status":555,
"error":"Http Status 555",
"path":"/services/rest/simulator/throw"
}
"""
{
"timestamp":"@ignore@",
"status":555,
"error":"Http Status 555",
"path":"/services/rest/simulator/throw"
}
"""
));
}

Expand Down

0 comments on commit d3a82da

Please sign in to comment.