How to Display a Full HTTP Response Body with Gatling (#14148)
This commit is contained in:
parent
ad2e59fb47
commit
3ddf5fa6a4
|
@ -60,7 +60,12 @@
|
|||
<artifactId>gatling-maven-plugin</artifactId>
|
||||
<version>${gatling-maven-plugin.version}</version>
|
||||
<configuration>
|
||||
<simulationClass>org.baeldung.EmployeeRegistrationSimulation</simulationClass>
|
||||
<includes>
|
||||
<include>org.baeldung.EmployeeRegistrationSimulation</include>
|
||||
<include>org.baeldung.gatling.http.FetchSinglePostSimulation</include>
|
||||
<include>org.baeldung.gatling.http.FetchSinglePostSimulationLog</include>
|
||||
</includes>
|
||||
<runMultipleSimulations>true</runMultipleSimulations>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
|
@ -70,8 +75,8 @@
|
|||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<encoding>UTF-8</encoding>
|
||||
<gatling.version>3.9.0</gatling.version>
|
||||
<gatling-maven-plugin.version>4.2.9</gatling-maven-plugin.version>
|
||||
<gatling.version>3.9.5</gatling.version>
|
||||
<gatling-maven-plugin.version>4.3.0</gatling-maven-plugin.version>
|
||||
<faker.version>1.0.2</faker.version>
|
||||
<spring.version>2.7.5</spring.version>
|
||||
</properties>
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
package org.baeldung.gatling.http;
|
||||
|
||||
import io.gatling.javaapi.core.*;
|
||||
import io.gatling.javaapi.http.*;
|
||||
import static io.gatling.javaapi.http.HttpDsl.*;
|
||||
import static io.gatling.javaapi.core.CoreDsl.*;
|
||||
|
||||
public class FetchSinglePostSimulation extends Simulation {
|
||||
|
||||
public FetchSinglePostSimulation() {
|
||||
HttpProtocolBuilder httpProtocolBuilder = http.baseUrl("https://jsonplaceholder.typicode.com");
|
||||
|
||||
ScenarioBuilder scn = scenario("Display Full HTTP Response Body").exec(http("GET Request").get("/posts/1")
|
||||
.check(status().is(200))
|
||||
.check(bodyString().saveAs("responseBody")))
|
||||
.exec(session -> {
|
||||
System.out.println("Response Body:");
|
||||
System.out.println(session.getString("responseBody"));
|
||||
return session;
|
||||
});
|
||||
setUp(scn.injectOpen(atOnceUsers(1))).protocols(httpProtocolBuilder);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package org.baeldung.gatling.http;
|
||||
|
||||
import io.gatling.javaapi.core.ScenarioBuilder;
|
||||
import io.gatling.javaapi.core.Simulation;
|
||||
import io.gatling.javaapi.http.HttpProtocolBuilder;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import static io.gatling.javaapi.core.CoreDsl.*;
|
||||
import static io.gatling.javaapi.http.HttpDsl.http;
|
||||
import static io.gatling.javaapi.http.HttpDsl.status;
|
||||
|
||||
public class FetchSinglePostSimulationLog extends Simulation {
|
||||
|
||||
public FetchSinglePostSimulationLog() {
|
||||
HttpProtocolBuilder httpProtocolBuilder = http.baseUrl("https://jsonplaceholder.typicode.com");
|
||||
|
||||
ScenarioBuilder scn = scenario("Display Full HTTP Response Body").exec(http("GET Request").get("/posts/1")
|
||||
.check(status().is(200))
|
||||
.check(bodyString().saveAs("responseBody")))
|
||||
.exec(session -> {
|
||||
|
||||
String responseBody = session.getString("responseBody");
|
||||
try {
|
||||
writeFile("response_body.log", responseBody);
|
||||
} catch (IOException e) {
|
||||
System.err.println("error writing file");
|
||||
}
|
||||
return session;
|
||||
});
|
||||
setUp(scn.injectOpen(atOnceUsers(1))).protocols(httpProtocolBuilder);
|
||||
}
|
||||
|
||||
private void writeFile(String fileName, String content) throws IOException {
|
||||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName, true))) {
|
||||
writer.write(content);
|
||||
writer.newLine();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue