简体   繁体   中英

How can I test a http request in JMeter when the service is not available

I am trying to test a http request but the service is not available, is there a way to create a dummy response in JMeter? The purpose of this is to test the request that is actually being sent

The dummy sampler has been recommended however, if another tester has not got this plugin then they will not be able to test it. Is there another sampler or tool already built-in that can be used?

Actually, you ultimately can do whatever you want with JSR223 Sampler.

And it's fairly easy to code an HTTP request in Groovy, as the HTTP Client functionality is effectively embedded into the environment, something like:

def urlString = 'http://' + restHost + ':' + restPort + '/' + restPath + '?' + paramString.toString()
def url = urlString.toURL()
def response = url.getText()

As simple as that.

You can catch the protocol error then, and respond with whatever you want - you've got full access to your SampleResult object from script:

try {
   ...
} catch(IOException ioe) {
    SampleResult.setResponseCode("200")
    SampleResult.setResponseMessage("FAKE OK")
    SampleResult.setResponseData("My dummy response!", "UTF-8") 
    SampleResult.setSuccessful(true)
}

PS Not sure I truly understand the reasoning behind a request like this.

If situation is so unfortunate that the team responsible for the service under test is refusing to cooperate that badly, I would rather quickly do a the mock service, and leave the test pure, simple & versatile.

The request will not be sent as you need to establish underlying TCP connection prior to sending whatever data over the wire.

If you want to test the fact that the server is down you can define connect and response timeouts, the settings live under "Advanced" tab of the HTTP Request sampler (or even better HTTP Request Defaults )

在此处输入图像描述

The above configuration will "tell" JMeter to attempt to establish the connection and if there will be no response in 1 second - the sampler will be marked as failed:

在此处输入图像描述

If this is the behaviour you expect you can configure JMeter to accept this response by adding a Response Assertion as a child of the request and configuring it like:

  • Field to Test: Response Code
  • Pattern Matching Rules: Equals
  • Patterns to Test: Non HTTP response code: org.apache.http.conn.ConnectTimeoutException
  • Tick Ignore Status box

在此处输入图像描述

This way JMeter will treat the timed-out request as successful:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM