Giter Club home page Giter Club logo

http-matchers's Introduction

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

http-matchers's People

Contributors

helsing avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

mederel

http-matchers's Issues

Verifying entity in unit tests doesn't work

I'm trying to test an endpoint in an unittest but I'm getting the following exception when using the hasEntity() matcher:
java.lang.IllegalStateException: Method not supported on an outbound message.
at org.glassfish.jersey.message.internal.OutboundJaxrsResponse.readEntity(OutboundJaxrsResponse.java:145)
at org.valid4j.matchers.http.HasEntityWithValueMatcher.matchesSafely(HasEntityWithValueMatcher.java:28)
at org.valid4j.matchers.http.HasEntityWithValueMatcher.matchesSafely(HasEntityWithValueMatcher.java:9)
at org.hamcrest.TypeSafeDiagnosingMatcher.matches(TypeSafeDiagnosingMatcher.java:55)
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:12)
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)
at my.project.endpoint.internal.v1_0.TestEndpoint_v1_0.getDeviceTypeNames(TestEndpoint_v1_0.java:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.