From 3ddf5fa6a4d200f3914f8d1f6aa5b4902fec2cee Mon Sep 17 00:00:00 2001 From: Michael Olayemi Date: Sat, 3 Jun 2023 03:42:18 +0000 Subject: [PATCH] How to Display a Full HTTP Response Body with Gatling (#14148) --- testing-modules/gatling-java/pom.xml | 11 +++-- .../http/FetchSinglePostSimulation.java | 23 ++++++++++ .../http/FetchSinglePostSimulationLog.java | 42 +++++++++++++++++++ 3 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 testing-modules/gatling-java/src/test/java/org/baeldung/gatling/http/FetchSinglePostSimulation.java create mode 100644 testing-modules/gatling-java/src/test/java/org/baeldung/gatling/http/FetchSinglePostSimulationLog.java diff --git a/testing-modules/gatling-java/pom.xml b/testing-modules/gatling-java/pom.xml index 54e18b3ac1..abe033f298 100644 --- a/testing-modules/gatling-java/pom.xml +++ b/testing-modules/gatling-java/pom.xml @@ -60,7 +60,12 @@ gatling-maven-plugin ${gatling-maven-plugin.version} - org.baeldung.EmployeeRegistrationSimulation + + org.baeldung.EmployeeRegistrationSimulation + org.baeldung.gatling.http.FetchSinglePostSimulation + org.baeldung.gatling.http.FetchSinglePostSimulationLog + + true @@ -70,8 +75,8 @@ 1.8 1.8 UTF-8 - 3.9.0 - 4.2.9 + 3.9.5 + 4.3.0 1.0.2 2.7.5 diff --git a/testing-modules/gatling-java/src/test/java/org/baeldung/gatling/http/FetchSinglePostSimulation.java b/testing-modules/gatling-java/src/test/java/org/baeldung/gatling/http/FetchSinglePostSimulation.java new file mode 100644 index 0000000000..16cf25faef --- /dev/null +++ b/testing-modules/gatling-java/src/test/java/org/baeldung/gatling/http/FetchSinglePostSimulation.java @@ -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); + } +} diff --git a/testing-modules/gatling-java/src/test/java/org/baeldung/gatling/http/FetchSinglePostSimulationLog.java b/testing-modules/gatling-java/src/test/java/org/baeldung/gatling/http/FetchSinglePostSimulationLog.java new file mode 100644 index 0000000000..9ad4d6edf6 --- /dev/null +++ b/testing-modules/gatling-java/src/test/java/org/baeldung/gatling/http/FetchSinglePostSimulationLog.java @@ -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(); + } + } +}