You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@jmeter.apache.org by Steve Feehan <sf...@boolecat.com> on 2006/11/17 02:34:37 UTC

assertions on http headers

I'd like to do assertions on HTTP response headers. It seems that 
response assertions only work on the response body. This post from
a couple years back matches my experience:

http://mail-archives.apache.org/mod_mbox/jakarta-jmeter-user/200211.mbox/%3C5DABF7424840D411BE9600508B6621F8040B5BF1@exchukthis02.experian.co.uk%3E

Are we missing something? Is it possible to do regular expression
matches on HTTP response headers? 

Thanks,

-- 
Steve Feehan

---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-user-help@jakarta.apache.org


Re: assertions on http headers

Posted by sebb <se...@gmail.com>.
On 17/11/06, Steve Feehan <sf...@boolecat.com> wrote:
> On Fri, Nov 17, 2006 at 11:39:56AM +0000, sebb wrote:
> > The Regex Post-Processor can be applied to Headers, but this feature
> > has not yet been applied to the Response Assertion, sorry.
> >
> > It's a useful feature - just no-one has got round to implementing it yet.
> >
> > I've added a Bugzilla enhancement request so it does not get forgotten.
> >
> > As a work-round, you could use the BeanShell Assertion to get access
> > to the Headers.
>
> Even with my incredibly weak Java skills I was able to get something
> simple to work. Does this look sane?

Looks OK.
>
> import java.util.regex.Pattern;
> import java.util.regex.Matcher;
>
> if (ResponseCode.equals("200")) {
>     // NOTE: the ResponseHeaders are a String with embedded newlines.
>     // I wasn't able to use the ^ or $ anchors in my regex. If this
>     // were required perhaps the string could be split into an array of
>     // strings?
>

You need to use the modifier (?m) which allows ^ and $ to match
embbeded lines. Either add it to the String below:


>     String p = "X-Cache: HIT";

       String p = "(?m)X-Cache: HIT";

      or add the appropriate flag to the compile method call:

>     Pattern pattern = Pattern.compile(p);

     Pattern pattern = Pattern.compile(p, java.util.regex.Pattern.MULTILINE);

>     Matcher matcher = pattern.matcher(ResponseHeaders);
>     if (!matcher.find()) {
>         Failure = true ;
>         FailureMessage = "The " + p +
>                          " header was not present in response headers.\n" +
>                          ResponseHeaders;
>     }
> } else {
>     Failure = true;
>     FailureMessage = "Error: HTTP Response code " + ResponseCode;
> }
>
> Thanks!
>
> --
> Steve Feehan
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: jmeter-user-help@jakarta.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-user-help@jakarta.apache.org


Re: assertions on http headers

Posted by Steve Feehan <sf...@boolecat.com>.
On Fri, Nov 17, 2006 at 11:39:56AM +0000, sebb wrote:
> The Regex Post-Processor can be applied to Headers, but this feature
> has not yet been applied to the Response Assertion, sorry.
> 
> It's a useful feature - just no-one has got round to implementing it yet.
> 
> I've added a Bugzilla enhancement request so it does not get forgotten.
> 
> As a work-round, you could use the BeanShell Assertion to get access
> to the Headers.

Even with my incredibly weak Java skills I was able to get something
simple to work. Does this look sane?

import java.util.regex.Pattern;
import java.util.regex.Matcher;

if (ResponseCode.equals("200")) {
    // NOTE: the ResponseHeaders are a String with embedded newlines. 
    // I wasn't able to use the ^ or $ anchors in my regex. If this
    // were required perhaps the string could be split into an array of
    // strings?

    String p = "X-Cache: HIT";
    Pattern pattern = Pattern.compile(p);
    Matcher matcher = pattern.matcher(ResponseHeaders);
    if (!matcher.find()) {
        Failure = true ;
        FailureMessage = "The " + p + 
	                 " header was not present in response headers.\n" +
                         ResponseHeaders;
    } 
} else {
    Failure = true;
    FailureMessage = "Error: HTTP Response code " + ResponseCode;
}

Thanks! 

-- 
Steve Feehan

---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-user-help@jakarta.apache.org


Re: assertions on http headers

Posted by sebb <se...@gmail.com>.
The Regex Post-Processor can be applied to Headers, but this feature
has not yet been applied to the Response Assertion, sorry.

It's a useful feature - just no-one has got round to implementing it yet.

I've added a Bugzilla enhancement request so it does not get forgotten.

As a work-round, you could use the BeanShell Assertion to get access
to the Headers.

http://jakarta.apache.org/jmeter/usermanual/component_reference.html#BeanShell_Assertion

S.
On 17/11/06, Steve Feehan <sf...@boolecat.com> wrote:
> I'd like to do assertions on HTTP response headers. It seems that
> response assertions only work on the response body. This post from
> a couple years back matches my experience:
>
> http://mail-archives.apache.org/mod_mbox/jakarta-jmeter-user/200211.mbox/%3C5DABF7424840D411BE9600508B6621F8040B5BF1@exchukthis02.experian.co.uk%3E
>
> Are we missing something? Is it possible to do regular expression
> matches on HTTP response headers?
>
> Thanks,
>
> --
> Steve Feehan
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: jmeter-user-help@jakarta.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-user-help@jakarta.apache.org