io7m / quixote

Embedded test suite web server

Home Page:https://www.io7m.com/software/quixote

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

quixote

Maven Central Maven Central (snapshot) Codecov

quixote

JVM Platform Status
OpenJDK (Temurin) Current Linux Build (OpenJDK (Temurin) Current, Linux)
OpenJDK (Temurin) LTS Linux Build (OpenJDK (Temurin) LTS, Linux)
OpenJDK (Temurin) Current Windows Build (OpenJDK (Temurin) Current, Windows)
OpenJDK (Temurin) LTS Windows Build (OpenJDK (Temurin) LTS, Windows)

quixote

A tiny embedded HTTP server for unit testing.

Features

  • An embedded HTTP server for simulating external services.
  • Conveniently enqueue responses to arbitrary requests.
  • Verify that requests were received as expected.
  • Zero dependencies.
  • Written in pure Java 17.
  • OSGi ready.
  • JPMS ready.
  • ISC license.
  • High-coverage automated test suite.

Motivation

Any code that makes requests to an external service should be tested to ensure that both the requests it makes are correct, and that it behaves correctly when presented with various responses. The quixote package provides a tiny embedded web server that can be configured to return canned responses to specified requests.

Building

$ mvn clean verify

Usage

Create a QWebServer before each test. The server will listen on the specified port.

@BeforeEach
public void setup()
  throws IOException
{
  this.server =
    QWebServers.createServer(42000);
  this.http =
    HttpClient.newHttpClient();
}

Enqueue responses to requests:

this.server.addResponse()
  .forMethod("GET")
  .forPath("/xyz")
  .withContentType("text/plain")
  .withFixedText("Hello 0.")
  .withStatus(201)
  .withHeader("Header-0", "XYZ");

Have code make requests to the server during tests, and then verify that the server received the requests:

final var requests =
  new LinkedList<>(this.server.requestsReceived());

{
  final var req = requests.remove(0);
  assertEquals("GET", req.method());
  assertEquals("/xyz", req.path());
}

Remember to clean up the server after each test:

@AfterEach
public void tearDown()
  throws IOException
{
  this.server.close();
}

About

Embedded test suite web server

https://www.io7m.com/software/quixote

License:ISC License


Languages

Language:Java 99.8%Language:CSS 0.2%