uk.staygrounded:http-stubby

http-stubby is a tool designed to assist in testing applications that interact over the HTTP(S) protocol

License

License

GroupId

GroupId

uk.staygrounded
ArtifactId

ArtifactId

http-stubby
Last Version

Last Version

1.2
Release Date

Release Date

Type

Type

jar
Description

Description

uk.staygrounded:http-stubby
http-stubby is a tool designed to assist in testing applications that interact over the HTTP(S) protocol
Project URL

Project URL

https://github.com/chrisjholly/http-stubby
Source Code Management

Source Code Management

http://github.com/chrisjholly/http-stubby/tree/master

Download http-stubby

How to add to project

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

Dependencies

compile (4)

Group / Artifact Type Version
org.slf4j : slf4j-api jar 1.7.24
commons-io : commons-io jar 2.4
commons-lang : commons-lang jar 2.6
org.hamcrest : hamcrest-all jar 1.3

test (6)

Group / Artifact Type Version
org.apache.logging.log4j : log4j-slf4j-impl jar 2.7
org.apache.logging.log4j : log4j-api jar 2.7
org.apache.logging.log4j : log4j-core jar 2.7
junit : junit jar 4.11
org.mockito : mockito-core jar 1.9.5
org.apache.httpcomponents : httpclient jar 4.5.2

Project Modules

There are no modules declared in this project.

http-stubby

http-stubby is a tool designed to assist in testing applications that interact over the HTTP(S) protocol. It starts a real HTTP server and supports building responses (status code, body, headers) to be primed programmatically.

Features

  • Supports HTTP/HTTPS
    • HTTPS can be initialised using default https configuration or by supplying an SSLContext
  • Prime responses based on request criteria
    • Multiple responses can be primed with different request criteria
    • Built-in request criteria includes:
      • URI matching (uriEquals, uriContains, uriStartsWith, uriEndsWith)
      • HTTP method matching (GET, HEAD, POST, PUT, DELETE)
      • Header matching (headerExists, headerEqualTo, headerContains)
    • Configure responses with status code, body(JSON, XML etc) and headers
    • Configurable default response when no matching response is found
  • Exposes history of all request/responses to/from to http-stubby
  • Ability to register custom HttpRequestResponseEventListener

How to?

Starting http-stubby

HttpStubbyServer stubbyServer = HttpStubbyServer.stubbyServerWith(HttpServerFactory.httpConfiguration(nextAvailablePortNumber()));
stubbyServer.start();

with HTTPS:

HttpStubbyServer stubbyServer = stubbyServerWith(HttpServerFactory.httpsConfiguration(nextAvailablePortNumber()));
stubbyServer.start();

or

HttpStubbyServer stubbyServer = stubbyServerWith(HttpServerFactory.httpsConfiguration(nextAvailablePortNumber(), SSLContext.getDefault()));
stubbyServer.start();

with RequestResponseHandlerListener:

HttpStubbyServer stubbyServer = HttpStubbyServer.stubbyServerWith(HttpServerFactory.httpConfiguration(nextAvailablePortNumber()));
stubbyServer.registerHttpRequestResponseEventListener(new HttpRequestResponseEventListener() {
            @Override
            public void newRequest(HttpRequest httpRequest) {
                
            }

            @Override
            public void newResponse(HttpResponse httpResponse) {

            }
        });
stubbyServer.start();

Priming responses

Static response:

HttpStubbyServer stubbyServer = ...
stubbyServer.willReturn(responseOf(HttpStatus.Code.OK)
        .withBody("<response><id>123</id></response>"));

Dynamic response:

HttpStubbyServer stubbyServer = ...
stubbyServer.willReturn(responseOf(HttpStatus.Code.OK)
            .withBody(new Callable<String>() {
                private final AtomicInteger uniqueNumber = new AtomicInteger(1);

                public String call() {
                    return String.valueOf(uniqueNumber.getAndIncrement());
                }
            }));

With Delay:

HttpStubbyServer stubbyServer = ...
stubbyServer.willReturn(responseOf(HttpStatus.Code.OK)
        .withBody("<response><id>123</id></response>"))
        .withLatency(Duration.ofSeconds(2))
);

With Default Response:

HttpStubbyServer stubbyServer = ...
stubbyServer.willReturnWhenNoResponseFound(responseOf(HttpStatus.Code.NOT_FOUND)
        .withBody("no responses match));

Using criteria to prime a response for your request:

Request Method Type (GET, POST, PUT, DELETE, HEAD):

HttpStubbyServer stubbyServer = ...
stubbyServer.willReturn(responseOf(HttpStatus.Code.OK), RequestMethodMatcher.forAGetRequest());
stubbyServer.willReturn(responseOf(HttpStatus.Code.OK), RequestMethodMatcher.forAPostRequest());
stubbyServer.willReturn(responseOf(HttpStatus.Code.OK), RequestMethodMatcher.forAPutRequest());
stubbyServer.willReturn(responseOf(HttpStatus.Code.OK), RequestMethodMatcher.forADeleteRequest());
stubbyServer.willReturn(responseOf(HttpStatus.Code.OK), RequestMethodMatcher.forAHeadRequest());

Request URI matching:

HttpStubbyServer stubbyServer = ...
stubbyServer.willReturn(responseOf(OK), RequestUriMatcher.uriEqualTo("http://localhost:12345/api/login"));
stubbyServer.willReturn(responseOf(OK), RequestUriMatcher.uriEqualToIgnoringCase("http://LOCALHOST:12345/API/login"));
stubbyServer.willReturn(responseOf(OK), RequestUriMatcher.uriStartsWith("http://localhost:12345/api"));
stubbyServer.willReturn(responseOf(OK), RequestUriMatcher.uriEndsWith("login"));
stubbyServer.willReturn(responseOf(OK), RequestUriMatcher.uriContains("api"));

Request Header matching:

HttpStubbyServer stubbyServer = ...
stubbyServer.willReturn(responseOf(OK), RequestHeaderMatcher.requestHeaderContains("Authorization", "123abc"));
stubbyServer.willReturn(responseOf(OK), RequestHeaderExistsMatcher.requestHeaderExists("Authorization"));
stubbyServer.willReturn(responseOf(OK), RequestHeaderExistsMatcher.requestHeaderDoesNotExist("Security-Header"));

Access request/response history

HttpStubbyServer stubbyServer = ...
stubbyServer.httpRequestResponseHistory().lastRequest();
stubbyServer.httpRequestResponseHistory().lastResponse();

Versions

Version
1.2
1.0