how should I assert that a response's status was in a certain family?

1,765 views
Skip to first unread message

Todd Bradley

unread,
Feb 24, 2015, 5:09:01 PM2/24/15
to rest-a...@googlegroups.com
I'm testing a REST API where in some of my tests, I don't care about the exact response status code, but only that the response was in a certain family of status codes. For instance, it's acceptable to receive a 200 or 201 or 204. So I was wondering if there's some nifty REST-assured way of doing a check for a status code in a certain Response.Status.Family.

Right now I'm doing stuff like this:
...then().assertThat().statusCode(anyOf(is(201), is(202)))

But it would be more flexible and more readable to do something like this:

...then().assertThat().statusCode(inFamily(SUCCESS))

Is there something like that built into REST-assured, or do I have to create my own Hamcrest matcher? (which I don't know how to do)


Thanks,
Todd.

Johan Haleby

unread,
Feb 25, 2015, 12:44:56 AM2/25/15
to rest-a...@googlegroups.com
I usually just do statusCode(and(greaterThanOrEqualTo(200), lessThan(300)).

/Johan

--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brian Farrell

unread,
Feb 25, 2015, 2:30:49 PM2/25/15
to rest-a...@googlegroups.com
You also might consider something like this in your test:

static final Matcher<Integer> SUCCESS = isOneOf(201, 202, 204);

...then().assertThat().statusCode(is(SUCCESS))

Todd Bradley

unread,
Feb 26, 2015, 10:36:38 AM2/26/15
to rest-a...@googlegroups.com
Thanks. I ended up just writing my own custom matcher (my first one ever). That was easier than looking up all the status codes and mapping them to their families myself. Here's what I came up with. If you know Hamcrest better than me and have suggestions for improving this, I'm all ears.


import javax.ws.rs.core.Response;

import javax.ws.rs.core.Response.Status.Family;


import org.hamcrest.Description;

import org.hamcrest.Factory;

import org.hamcrest.Matcher;

import org.hamcrest.TypeSafeMatcher;


/**

 * This is a Hamcrest matcher that checks that a given Integer, representing an HTTP response status code, is in a

 * certain response family.

 */

public class IsInFamily extends TypeSafeMatcher<Integer> {


Family family;


public IsInFamily(Family family) {

this.family = family;

}


@Override

public void describeTo(Description description) {

description.appendText("in response status code family " + family);

}


@Factory

public static Matcher<Integer> isInFamily(Response.Status.Family family) {

return new IsInFamily(family);

}


@Override

protected boolean matchesSafely(Integer item) {

return Response.Status.Family.familyOf(item).equals(family);

}


} 

Johan Haleby

unread,
Feb 27, 2015, 2:02:01 AM2/27/15
to rest-a...@googlegroups.com
Thanks for sharing
Reply all
Reply to author
Forward
0 new messages