Class TestHttpServer

java.lang.Object
cloud.opencode.base.test.http.TestHttpServer
All Implemented Interfaces:
AutoCloseable

public final class TestHttpServer extends Object implements AutoCloseable
Test HTTP Server — Lightweight in-process HTTP server for unit testing. 测试 HTTP 服务器 — 用于单元测试的轻量级进程内 HTTP 服务器。

Uses jdk.httpserver (included in JDK), no external dependencies. Implements AutoCloseable for use in try-with-resources.

使用 jdk.httpserver(JDK 内置),无外部依赖。

Features | 主要功能:

  • In-process HTTP server for testing - 用于测试的进程内HTTP服务器
  • Request recording and route matching - 请求录制和路由匹配

Usage Examples | 使用示例:

try (TestHttpServer server = TestHttpServer.start()) {
    server.when(RequestMatcher.get("/api")).thenRespond(MockResponse.ok());
    String url = server.url("/api");
}

Security | 安全性:

  • Thread-safe: Yes (concurrent data structures) - 线程安全: 是(并发数据结构)
Since:
JDK 25, opencode-base-test V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • start

      public static TestHttpServer start()
    • start

      public static TestHttpServer start(int port)
    • when

    • url

      public String url(String path)
    • port

      public int port()
    • verify

      public RequestVerification verify()
      Creates a request verification builder for asserting recorded requests. 创建请求验证构建器,用于断言已录制的请求。

      Usage Examples | 使用示例:

      server.verify().that(RequestMatcher.get("/api")).wasCalled(2);
      server.verify().that(RequestMatcher.post("/api")).wasNeverCalled();
      
      Returns:
      a new request verification builder | 新的请求验证构建器
      Since:
      V1.0.3
    • recordedRequests

      public List<RecordedRequest> recordedRequests()
    • recordedRequest

      public RecordedRequest recordedRequest(String path)
    • reset

      public void reset()
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable