Ori Marko
Ori Marko

Reputation: 58862

Can JMeter mock HTTP request

I want to Mock HTTP requests, meaning sending real request to real server, but ignore (not wait) and override the response with a dummy response,

JMeter have many tools which are close but not enough,

DummySampler plugin is close but not really sending request,

An old answer direct to Mirror Server which seems irrelevant for specific API requests and responses.

JMeter does not simulate servers.

Having said that, JMeter 2.3 has a built-in mirror server - it accepts any HTTP request and responds with a page containing the request details.

If server B does not care what server C sends back, then you could use this to "mock" server C.

My answer on ignoring HTTP response by adding Runtime controller with 1 second and updating the response data is a problematic workaround but can work.

Is there a better option available in plugins or executing some other tool in parallel?

Is opening an enhancement for JMeter is relevant and if so, should it improve HTTP Request or is it a new sampler as Mock HTTP Request? can Runtime controller support only sending and stop waiting for response (by using 0 seconds for example) ?

Upvotes: 5

Views: 10260

Answers (3)

Lemonseed
Lemonseed

Reputation: 1732

From what I gather you would like to simulate a request for the purposes of the test results, while simultaneously sending a real request independent of your test results. One way you can accomplish this is to attach a JSR223 PostProcessor to a Dummy Sampler in this fashion:

JMeter JSR223 PostProcesssor element added under Dummy Sampler element

The PostProcessor element is added under the Dummy Sampler and executes after the simulated request. Inside the script for the JSR223 PostProcessor, we can add a few lines of scripting to execute a fire-and-forget request which satisfies your use case:

import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.HttpResponse;
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost request = new HttpPost("https://www.your-domain.com");
HttpResponse response = httpClient.execute(request);
log.info("HTTP Response: " + response);

This script can be applied anywhere you want to make an HTTP call, but do not want the result of that call to impact your test results. You can simply change the following line to the actual URL you intend to call:

enter image description here

The last line containing log.info(...) is optional, but will output the result (i.e., status code 200, 404, etc.) for the request in the test logs which may be useful for debugging purposes.

Upvotes: 2

Vitaly  Kirilyuk
Vitaly Kirilyuk

Reputation: 21

I manged to do it using previous post (https://stackoverflow.com/a/49130518/5210267), but succeed only after putting it in "bzm - Parallel Controller" and setting timeout to define time of WireMockServer's work: image example

Upvotes: 0

Dmitri T
Dmitri T

Reputation: 168157

The easiest option would be going for i.e. WireMock which is extremely powerful and flexible.

You can integrate it with JMeter by adding WireMock jar (along with dependencies) to JMeter Classpath and running the WireMockServer from the JSR223 Test Elements using Groovy language.

If you're not too comfortable with Groovy you can run WireMock as a standalone Java application using OS Process Sampler


import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;

import static com.github.tomakehurst.wiremock.client.WireMock.*;

public class WireMockTest {

    public static void main(String[] args) {
        WireMockServer wireMockServer = new WireMockServer();
        configureFor("0.0.0.0", 8080);
        wireMockServer.start();
        StubMapping foo = stubFor(get(urlEqualTo("/wiretest"))
                .willReturn(aResponse()
                        .withStatus(200)
                        .withBody("Hello World")));
        wireMockServer.addStubMapping(foo);
    }
}

Upvotes: 8

Related Questions