JMeter. Why doesn't if statement work? - jmeter

I am frustrated by my failure to get a simple IF statement to trigger and am begining to suspsect that I do not understand the way IF statements are implemented in JMeter.
What I am trying to do is query an API that returns 202 responses while it is processes a request, then returns a 200 when the results are returned. Finally a 404 is returned if I query again after getting the results.
I have a while controller with no condition that performs this query and it exits on the 404.
This seems to work OK.
What I want to do is confirm that I have valid JSON returned for the 202 and the 200 but ignore the 404.
I thought I could add an IF controller after each query in the loop to run a a BSF assertion when I have 202 or 200 but I cannot seem to get it to work. Even if I put in "true" or "1 == 1" as the condition the assertion is never called.
"${httpCode}" == "200"
In my assertion I have added a deliberate error "eval x;" that causes the assertion to fail if I drag and drop it to other parts of the suite -
try {
eval('var response = ' + prev.getResponseDataAsString());
eval x;
} catch(e) {
prev.setSuccessful(false);
prev.setResponseMessage("Invalid response. Expected a valid JSON.");
}
Like I said, even if I add true or 1 == 1 the assertion error never shows in the assertion results. Am I fundamentally misunderstanding the way the IF controller works? Certainly the Java programmers I work with cannot see what is wrong here.
Unfortunately this site won't let me post an image of the suite...
BTW
Sorry for asking a "vague" question but I have been evaluating JMeter for over a week now and find the online resources to be lacking in "How To" type tutorials. Lots of references to specific JMeter objects and basic "Getting Started" type posts but nothing detailed. Also most of the references seem to be circular and and out of date; I keep finding myself back at the same pages over again.
The apache site seems more geared up as an object reference.
Is anyone able to recommend a really good online resource?

What JMeter version do you have? In my ver.2.10 if controller with 1==1 works correctly.
Also I could recommend you to use If controller with JMeterThread.last_sample_ok function. It returns true in case previous sampler was ok.
You could use in as if condition.

Try the following:
${__BeanShell("${httpCode}".equals("200"))}

Related

Facing issue while handling if controller

I am facing issue while using if controller in my jmeter script.
i used condition "${Response}"=="Test unsuccessful" in if controller.
Response variable contains complete response and i'm checking test unsuccessful in response if not it should execute next. however its not executing next even though we are not getting correct response.
How to handle this?
I don't think you can use the condition like "${Response}"=="Test unsuccessful", you can use a JMeter Variable if it resolves to true (or false), however if you want to compare 2 strings and basing on result conditionally execute (or don't execute child sampler(s)) you need to use a function which compares the strings and returns true or false, i.e. __jexl3() would be a good candidate:
${__jexl3("${Response}"=="Test unsuccessful",)}
More information: 6 Tips for JMeter If Controller Usage

how can I run multiple if controller in jmeter

I am currently working in a heavy load test, I have one login request which access with user and password and basic auth, I have to validate some info from the response and I am using assertions but I need to apply different kind of assert depending on the code response and to be able to do that I am using an if control putting the assertions inside as a child, the problem begins when I try to execute the assertions with an error code response, some how the if controller is not taking the value of the variable I created to store the code response. could some one help me? thanks!
You cannot put assertion as a direct child of the If Controller. In fact you can, however it will not make any sense as assertions obey JMeter Scoping Rules and since there is no any Sampler in the Assertion scope - it will simply not get executed.
I would recommend going for JSR223 Assertion where you have all power of Groovy SDK and JMeter API in order to set up your custom pass/fail criteria. The pseudo code would be something like:
if (SampleResult.getResponseCode().equals('200')) {
//do what you need when response code is 200
//for example let's check if response contains "foo" line
if (!SampleResult.getResponseDataAsString().contains('foo')) {
AssertionResult.setFailure(true)
AssertionResult.setFailureMessage('Failed to find "foo" line in the response')
}
}
else if (SampleResult.getResponseCode().equals('300')) {
//do what you need when response code is 300
}
else if (SampleResult.getResponseCode().equals('400')){
//do what you need when response code is 400
}
else {
AssertionResult.setFailure(true)
AssertionResult.setFailureMessage('Unexpected response code: ' + SampleResult.getResponseCode())
}
References:
SampleResult documentation
AssertionResult documentation
Scripting JMeter Assertions in Groovy - A Tutorial

How to use custom error settings for JWT middleware

I have followed the cook books guide to the letter, found here https://echo.labstack.com/cookbook/jwt
But when using the JWT middleware I am having some issues with adding custom error messages. Login works fine, even to the point of not giving details (username & password) that returns a 404.
But when the JWT is missing it returns a 400, I want it to also return a 404.
So in my research I found this, https://forum.labstack.com/t/custom-error-message-in-jwt-middleware/325/3 which lists the following middleware.ErrJWTMissing & middleware.ErrJWTInvalid But is very unclear on how to set these?
I have tried setting them as vars on the router file, like so
var (
ErrJWTInvalid = echo.NewHTTPError(http.StatusTeapot, "test 104")
ErrJWTMissing = echo.NewHTTPError(http.StatusTeapot, "test 103")
)
But the error that sill comes back to me is a 400 and not a 418 (as this is just a test). So what am I doing wrong?
You can change the HTTP code and message this way.
func init() {
middleware.ErrJWTMissing.Code = 401
middleware.ErrJWTMissing.Message = "Unauthorized"
}
First, a point on your statement that you want to return a 400 and also a 404 error - you cannot do this. You're sending one response from the server so it gets exactly one response code. You could send a 207, but we're not really talking about multiple resources here, so don't do that. In my opinion, a 400 error is indeed the correct response for a missing JWT as that constitutes a bad request. A 404 "Not Found" means that the requested resource (the thing on the server side) could not be found. It does not mean that something in the request could not be found.
As for setting your custom error message, you're likely to be out of luck without altering the source code for Echo. That specific response is coming from within the middleware handlers of the package itself (you can see it here). This is mostly abstracted away from you, so without looking at the inner workings of the package, there would be no way to tell where this was coming from, and frankly there's not a lot that you can easily do about it. ErrJWTMissing is indeed the variable that the package uses internally for this error message, but Echo does not appear to provide an exported setter method for you to change this value, so you're stuck with what it is.
If you truly wanted to set a custom error method for this case I think your options would be to:
Write your own middleware to intercept the request before it was handled by Echo's middleware, where you could handle the request however you wanted.
Edit the Echo source to work how you wanted it to work -- specifically, all you would have to do is edit ErrJWTMissing.
Basically, Echo is trying to do you favors by handling all of this middleware processing for you, and it's a lot of work or hackery to un-do that work while still using Echo.

RestKit: set expected status codes without using response descriptor

I need to PUT/POST data to a service. Upon success, the server returns 201/Created (no body). The parameters are (necessarily) a NSDictionary, so no object binding on either side is needed. RKObjectManager.requestWithObject works fine for this, but raises an error it expected a 204 rather than a 201. As far as I can tell, the only place to alter expected status codes is in a matching RKResponseDescriptor. RKResponseDescriptors seem like overkill for a response which has no body, and I'm unsure how to even construct one that works with no body. How can I tell RestKit that a 201 is OK for this POST?
Update
I eventually gave up on trying to do requests that didn't involve any sort of binding (ie, posting a dictionary and receiving a 201 response), and just dropped down to NSURLConnection stuff - it wasn't bad)
On your RKObjectRequestOperation:
operation.HTTPRequestOperation.acceptableStatusCodes = [NSIndexSet indexSetWithIndex:201];

Jmeter - pause when response assertion triggers?

I've got a Jmeter test up and running on an API I'm building.
The API returns this if it gets overloaded:
{"status":{"type":"failure","cause":"internal","http":500}}
What I'd like to do is have JMeter also PAUSE if it gets that result.
I already have set up a Response Assertion that captures these errors. It seems like this should be a simple thing to set up, but I'm not seeing it. I see that I can add an If Controller, but that only works with Javascript vars.
Do I need to add a Regular Expression Extractor, grab that 'failure' as 'type' variable, and then add that to the If Controller?
Seems a little over-complicated?
Am I missing something?
Try to use BeanShell Timer to handle this situation.
It
allows scripting so you can program timer behavior how you need;
has access to jmeter context - via ctx variable - so you can handle ResponseCode condition.
Here you can find good article with example how to use.
And here - how to access beanshell variables and handle response code.
Your condition will be something like this one:
if (prev.getResponseCode().equals("500") == true) {
. . .
}
PLEASE NOTE: script used in BeanShell Timer should return value is used as the number of milliseconds to wait.
Another question is what the reason to do this.
If you have load/stress-test scenario then these results are something you should get during your tests and analyze then.
If you have kind of "functional" test-scenario so you have to handle this situation in more general way, adding any kind of properly configured timer to each sampler - to avoid overload or simulate "real-life" scenario.
Am I missing something?

Resources