org.valid4j:http-matchers

A set of hamcrest-matchers for JAX-RS

License

License

GroupId

GroupId

org.valid4j
ArtifactId

ArtifactId

http-matchers
Last Version

Last Version

1.1
Release Date

Release Date

Type

Type

jar
Description

Description

org.valid4j:http-matchers
A set of hamcrest-matchers for JAX-RS
Project URL

Project URL

https://github.com/valid4j/http-matchers
Project Organization

Project Organization

valid4j
Source Code Management

Source Code Management

http://github.com/valid4j/http-matchers/tree/master

Download http-matchers

How to add to project

<!-- https://jarcasting.com/artifacts/org.valid4j/http-matchers/ -->
<dependency>
    <groupId>org.valid4j</groupId>
    <artifactId>http-matchers</artifactId>
    <version>1.1</version>
</dependency>
// https://jarcasting.com/artifacts/org.valid4j/http-matchers/
implementation 'org.valid4j:http-matchers:1.1'
// https://jarcasting.com/artifacts/org.valid4j/http-matchers/
implementation ("org.valid4j:http-matchers:1.1")
'org.valid4j:http-matchers:jar:1.1'
<dependency org="org.valid4j" name="http-matchers" rev="1.1">
  <artifact name="http-matchers" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.valid4j', module='http-matchers', version='1.1')
)
libraryDependencies += "org.valid4j" % "http-matchers" % "1.1"
[org.valid4j/http-matchers "1.1"]

Dependencies

compile (1)

Group / Artifact Type Version
org.hamcrest : hamcrest-core jar 1.3

provided (1)

Group / Artifact Type Version
javax.ws.rs : javax.ws.rs-api jar 2.0.1

test (6)

Group / Artifact Type Version
org.hamcrest : hamcrest-library jar 1.3
ch.qos.logback : logback-classic jar 1.1.2
org.glassfish.jersey.core : jersey-client jar 2.22.1
io.dropwizard : dropwizard-testing jar 0.9.2
com.google.guava : guava jar 18.0
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

http-matchers

A set of hamcrest-matchers to test your web services via the standard Java API for RESTful Services (JAX-RS)

This library is available at Maven Central Repository. Add this dependency to your pom.xml

<dependency>
    <groupId>org.valid4j</groupId>
    <artifactId>http-matchers</artifactId>
    <version>1.1</version>
</dependency>

The JAX-RS client API (javax.ws.rs.client) can be downloaded from here, but is usually bundled with the client implementation you are using, e.g Jersey.

<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.0.1</version>
</dependency>

Usage guide

// Invoke your web service using plain JAX-RS. E.g:
Client client = ClientBuilder.newClient();
Response response = client.target("http://example.org/hello").request("text/plain").get();

// Statically import the library entry point:
import static org.valid4j.matchers.http.HttpResponseMatchers.*;

Verify status of response

// Verify the status code of response
assertThat(response, hasStatusCode(Family.SUCCESSFUL));
assertThat(response, hasStatusCode(200));
assertThat(response, hasStatusCodeOf(Status.OK));

// Verify status code and reason of response
assertThat(response, hasStatus(Status.OK));
assertThat(response, hasStatus(400, "Bad Request");

Verify headers of response

// Verify headers of response (by-existence, by-value, by-iterable)
assertThat(response, hasHeader(HttpHeaders.EXPIRES));
assertThat(response, hasHeader("Content-Encoding", equalTo("gzip")));
assertThat(response, hasHeader("Age", equalTo(42)));
assertThat(response, hasHeaderValues(HttpHeaders.CACHE_CONTROL,
            contains(equalTo("no-cache"), equalTo("no-store"))));

// Verify representation metadata of the response
assertThat(response, hasContentType(MediaType.APPLICATION_JSON_TYPE));
assertThat(response, hasContentType(compatibleWith(MediaType.APPLICATION_JSON_TYPE)));
assertThat(response, hasContentEncoding("gzip"));
assertThat(response, hasContentLocation(URI.create("http://example.com/123")));
assertThat(response, hasContentLength(lessThan(2345)));

// Verify language of response
assertThat(response, ofLanguage("en-GB"));
assertThat(response, ofLanguage(Locale.UK));
assertThat(response, ofLanguage(equalTo(Locale.UK)));

// Verify location of the response
URI location = URI.create("http://example.com/123");
assertThat(response, hasLocation(equalTo(location)));

Verify message body (a.k.a entity) of response

// Verify the existence of a message body in the response
assertThat(response, hasEntity());

// Verify the message body content (aka entity) of the response
// NOTE: This will buffer the entity so make sure to close the response afterwards
assertThat(response, hasEntity(equalTo("content")));

// Map the body to a specific Java type (using a MessageBodyReader) before matching. E.g:
assertThat(response, hasEntity(String.class, equalTo("...")));
assertThat(response, hasEntity(MyBody.class, myBodyMatcher)));

For example, in order to verify a regular JSON payload, you could combine the matchers from com.jayway.jsonpath:json-path-assert in this way:

assertThat(response, hasEntity(String.class, hasJsonPath("$.path.to.attribute", equalTo("value"))));

// Or use an alternative syntax...
assertThat(response, hasEntity(isJsonString(withJsonPath("$.path.to.attribute", equalTo("value")))));

Or you could "cheat" a bit and parse the entity as a GenericType, and match on that:

assertThat(response, hasEntity(
     new GenericType<Map<String, String>>() {},
     allOf(hasEntry("key1", "value1"), hasEntry("key2", "value2"))));

Verify cookies of response

// Verify cookie existence
assertThat(response, hasCookie("my-cookie"));

// ...or other cookie properties
import static org.valid4j.matchers.http.NewCookieMatchers.*;
assertThat(response, hasCookie("my-cookie", allOf(
    withCookieValue("..."),
    withCookiePath("/cookie/path"),
    withCookieDomain("example.org"),
    withCookieVersion(greaterThan(2)),
    withCookieMaxAge(lessThan(600)),
    isCookieSecure(),
    not(isCookieHttpOnly())
)));

...

Project license

This software is licensed under Apache Software License, Version 2.0

org.valid4j

Versions

Version
1.1
1.0