mockwebserver4cj:基于 Cangjie 生态的 HTTP 测试服务项目

可用于为 HTTP 客户端(如 httpclient4cj)提供模拟测试环境,支持 HTTP/1.1 多种请求方法、响应配置、连接管理等核心功能,还实现了 HTTPS 监听与握手控制,便于 E2E 测试与协议行为验证。【此简介由AI生成】

Branch1Tags0
FilesLast commitLast update
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago

mockwebserver4cj

mockwebserver4cj is a Cangjie test server inspired by Kotlin MockWebServer. It is intended to pair with httpclient4cj as the real HTTP client.

Current HTTP/1 status

  • MockWebServer, MockResponse, RecordedRequest, Dispatcher, QueueDispatcher
  • HTTP/1.1 GET, POST, HEAD, generic method, chunked request parsing, chunked response writing with trailers, and same-connection exchanges
  • enqueue, takeRequest, takeRequest(Duration), url(path), address-specific start(address, port), shutdown() alias, Resource close support, lifecycle guards, and close wakeup
  • Response status, headers(Headers), trailers(Headers), inTunnel(), body, chunked body, interim 100 Continue responses, doNotReadRequestBody(), request/response start/body/end socket effects, headers/body/trailers delay, throttle, and close-socket effect
  • httpclient4cj E2E plus raw TCP HTTP/1 tests for redirects, blocking enqueue, timeout recovery, absolute-form request URLs, CONNECT tunnel upgrade on the same socket, chunked framing, chunked trailers, keep-alive behavior, request throttling, request disconnect halfway, and response disconnect halfway

Current HTTPS / upgrade status

  • HTTPS listener, handshake capture, SNI recording, and fail-handshake injection are implemented and covered by src/httpclient4cj_https_e2e_test.cj
  • Proxy-style absolute-form request recording works for both HTTP and HTTPS request URL reconstruction
  • Raw socket upgrade / WebSocket-style 101 Switching Protocols handoff is implemented through MockResponse.builder().webSocketUpgrade(...) and SocketHandler

Known blocked areas

  • Mutual TLS client-auth verification on this Windows/OpenSSL/stdx setup: httpsWithClientAuth still times out during server-side TLS handshake, and an isolated raw TlsSocket mutual-auth repro also hangs outside mockwebserver4cj
  • HTTP/2 remains partial: cleartext h2_prior_knowledge request/response transport exists, but push delivery and broader HTTP/2 coverage are still incomplete

Example

import httpclient4cj
import httpclient4cj.*
import mockwebserver4cj.*
import std.time.*

main() {
    let server = MockWebServer()
    server.enqueue(MockResponse.builder().body("pong").build())
    server.start()
    try {
        let client = HttpClient.builder()
            .connectTimeout(Duration.second)
            .readTimeout(Duration.second)
            .writeTimeout(Duration.second)
            .build()
        let request = Request.builder().url(server.url("/ping")).build()
        let response = client.newCall(request).execute()
        println(response.getBody().getOrThrow().getString())
        println(server.takeRequest().target)
    } finally {
        server.close()
    }
}

Verification

cjpm build
cjpm test
cjfmt -d src

Introduction

可用于为 HTTP 客户端(如 httpclient4cj)提供模拟测试环境,支持 HTTP/1.1 多种请求方法、响应配置、连接管理等核心功能,还实现了 HTTPS 监听与握手控制,便于 E2E 测试与协议行为验证。【此简介由AI生成】

Customize your domain