Class RequestVerification

java.lang.Object
cloud.opencode.base.test.http.RequestVerification

public final class RequestVerification extends Object
Request Verification — Fluent API for verifying recorded HTTP requests. 请求验证 — 用于验证已录制 HTTP 请求的流式 API。

Provides a WireMock-style verification builder that filters recorded requests by matcher predicates and asserts call counts, headers, and body content.

提供类似 WireMock 风格的验证构建器,通过匹配器谓词过滤已录制请求, 并断言调用次数、请求头和请求体内容。

Features | 主要功能:

  • Filter recorded requests by method and path - 按方法和路径过滤已录制请求
  • Assert exact, at-least, at-most call counts - 断言精确、至少、至多调用次数
  • Assert request body content - 断言请求体内容
  • Assert request header values - 断言请求头值
  • Fluent chaining for multiple assertions - 流式链式多断言

Usage Examples | 使用示例:

// Verify GET /api/users was called exactly 2 times
server.verify().that(RequestMatcher.get("/api/users")).wasCalled(2);

// Verify POST with specific body content
server.verify()
    .that(RequestMatcher.post("/api/users"))
    .wasCalled()
    .withBodyContaining("\"name\"");

// Verify a request was never made
server.verify().that(RequestMatcher.delete("/api/admin")).wasNeverCalled();

// Verify header was sent
server.verify()
    .that(RequestMatcher.get("/api/data"))
    .withHeader("authorization", "Bearer token123");

Security | 安全性:

  • Thread-safe: No (intended for single-threaded test assertions) - 线程安全: 否(设计用于单线程测试断言)
  • Null-safe: Yes (null arguments throw NullPointerException) - 空值安全: 是(null 参数抛出 NullPointerException)
Since:
JDK 25, opencode-base-test V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • that

      public RequestVerification that(RequestMatcher matcher)
      Filters recorded requests by the given matcher. Calling this method replaces any previously filtered results. 按给定匹配器过滤已录制请求。调用此方法会替换之前过滤的结果。
      Parameters:
      matcher - the request matcher predicate | 请求匹配器谓词
      Returns:
      this verification for fluent chaining | 当前验证实例(支持链式调用)
      Throws:
      NullPointerException - if matcher is null | 如果 matcher 为 null
    • wasCalled

      public RequestVerification wasCalled()
      Asserts that the matched requests were called at least once. 断言匹配的请求至少被调用了一次。
      Returns:
      this verification for fluent chaining | 当前验证实例(支持链式调用)
      Throws:
      AssertionException - if no matching requests were recorded | 如果没有录制到匹配的请求
    • wasCalled

      public RequestVerification wasCalled(int times)
      Asserts that the matched requests were called exactly the specified number of times. 断言匹配的请求恰好被调用了指定的次数。
      Parameters:
      times - the exact expected call count | 期望的精确调用次数
      Returns:
      this verification for fluent chaining | 当前验证实例(支持链式调用)
      Throws:
      IllegalArgumentException - if times is negative | 如果 times 为负数
      AssertionException - if actual count does not equal expected | 如果实际次数不等于期望次数
    • wasCalledAtLeast

      public RequestVerification wasCalledAtLeast(int times)
      Asserts that the matched requests were called at least the specified number of times. 断言匹配的请求至少被调用了指定的次数。
      Parameters:
      times - the minimum expected call count | 期望的最少调用次数
      Returns:
      this verification for fluent chaining | 当前验证实例(支持链式调用)
      Throws:
      IllegalArgumentException - if times is negative | 如果 times 为负数
      AssertionException - if actual count is less than expected | 如果实际次数少于期望次数
    • wasCalledAtMost

      public RequestVerification wasCalledAtMost(int times)
      Asserts that the matched requests were called at most the specified number of times. 断言匹配的请求最多被调用了指定的次数。
      Parameters:
      times - the maximum expected call count | 期望的最多调用次数
      Returns:
      this verification for fluent chaining | 当前验证实例(支持链式调用)
      Throws:
      IllegalArgumentException - if times is negative | 如果 times 为负数
      AssertionException - if actual count exceeds expected | 如果实际次数超过期望次数
    • wasNeverCalled

      public RequestVerification wasNeverCalled()
      Asserts that no matching requests were recorded. 断言没有录制到匹配的请求。
      Returns:
      this verification for fluent chaining | 当前验证实例(支持链式调用)
      Throws:
      AssertionException - if any matching requests were recorded | 如果录制到了匹配的请求
    • withBody

      public RequestVerification withBody(String expectedBody)
      Asserts that all matched requests have a body equal to the expected body. 断言所有匹配的请求的请求体等于期望的请求体。
      Parameters:
      expectedBody - the expected body content | 期望的请求体内容
      Returns:
      this verification for fluent chaining | 当前验证实例(支持链式调用)
      Throws:
      NullPointerException - if expectedBody is null | 如果 expectedBody 为 null
      AssertionException - if no matched requests exist, or any matched request body does not equal expectedBody | 如果没有匹配的请求,或任意匹配请求的请求体不等于期望值
    • withBodyContaining

      public RequestVerification withBodyContaining(String substring)
      Asserts that all matched requests have a body containing the expected substring. 断言所有匹配的请求的请求体包含期望的子字符串。
      Parameters:
      substring - the expected substring | 期望的子字符串
      Returns:
      this verification for fluent chaining | 当前验证实例(支持链式调用)
      Throws:
      NullPointerException - if substring is null | 如果 substring 为 null
      AssertionException - if no matched requests exist, or any matched request body does not contain the substring | 如果没有匹配的请求,或任意匹配请求的请求体不包含该子字符串
    • withHeader

      public RequestVerification withHeader(String name, String value)
      Asserts that all matched requests have the specified header with the expected value. 断言所有匹配的请求包含指定名称和期望值的请求头。
      Parameters:
      name - the header name (case-insensitive) | 请求头名称(不区分大小写)
      value - the expected header value | 期望的请求头值
      Returns:
      this verification for fluent chaining | 当前验证实例(支持链式调用)
      Throws:
      NullPointerException - if name or value is null | 如果 name 或 value 为 null
      AssertionException - if no matched requests exist, or any matched request does not have the header with the expected value | 如果没有匹配的请求,或任意匹配请求没有包含期望值的请求头
    • getMatchedRequests

      public List<RecordedRequest> getMatchedRequests()
      Returns the list of matched requests for custom assertions. 返回匹配的请求列表,用于自定义断言。
      Returns:
      unmodifiable list of matched requests | 不可变的匹配请求列表