MoonBit HTTP interaction recording, replay, and offline contract testing library
moon add chenqi-arch/moonbit-project@0.1.0import { "chenqi-arch/moonbit-project" @moonvcr, }
let cassette = @moonvcr.Cassette::decode(cassette_text)
let session = @moonvcr.Session::with_defaults(
cassette,
@moonvcr.SessionMode::StrictOffline,
)
let result = session.send(
request,
offline_transport,
)moon check
moon test
moon fmt --checklet cassette = Cassette::decode(cassette_text)
let session = Session::with_defaults(cassette, Replay)
let offline_transport = (_ : Request) => {
Err(TransportError::Failed("network disabled"))
}
let response = session.send(request, offline_transport)let session = Session::with_defaults(Cassette::empty(), Record)
let transport = (request : Request) => {
// 在这里调用你的 HTTP 客户端,并转换成 Response。
http_client_send(request)
}
let response = session.send(request, transport)
let cassette_text = Cassette::encode(session.cassette())let scripted = ScriptedTransport::new([
{ status: 200, headers: [], body: Text("ok"), },
])
let session = Session::with_defaults(Cassette::empty(), Record)
let response = session.send(request, (request) => scripted.send(request))moon run cmd/moonvcr-demopub(all) struct MatchConfig {
include_headers : Array[String]
ignore_headers : Array[String]
body_mode : BodyMatchMode
} derive(Eq, Debug)pub(all) struct Mismatch {
candidate_index : Int
kind : MismatchKind
expected : String
actual : String
} derive(Eq, Debug)fn ScriptedTransport::send(self : ScriptedTransport, _request : Request) -> Result[Response, TransportError]pub(all) struct Session {
cassette : Cassette
config : MatchConfig
redaction : RedactionConfig
mode : SessionMode
consumed : Array[Bool]
} derive(Debug)fn Session::new_with_redaction(cassette : Cassette, mode : SessionMode, config : MatchConfig, redaction : RedactionConfig) -> Sessionfn Session::send(self : Session, request : Request, transport : (Request) -> Result[Response, TransportError]) -> Result[Response, SessionError]pub(all) enum SessionError {
ReplayMiss(Array[MatchCandidate])
TransportFailed(TransportError)
RedactionFailed(RedactionError)
} derive(Eq, Debug)let CURRENT_FORMAT_VERSION : Intlet REDACTION_MARKER : Stringfn diagnose_mismatch(request : Request, cassette : Cassette, config : MatchConfig, consumed : Array[Bool]) -> MismatchReportfn diagnose_mismatch_with_redaction(request : Request, cassette : Cassette, config : MatchConfig, consumed : Array[Bool], redaction : RedactionConfig) -> MismatchReportfn match_candidates(request : Request, cassette : Cassette, config : MatchConfig, consumed : Array[Bool]) -> Array[MatchCandidate]fn match_request(request : Request, cassette : Cassette, config : MatchConfig, consumed : Array[Bool]) -> MatchResultfn normalize_url(url : String) -> Stringfn redact_interaction(interaction : Interaction, config : RedactionConfig) -> Result[Interaction, RedactionError]fn redact_response(response : Response, config : RedactionConfig) -> Result[Response, RedactionError]Install
Download zipMoonBit HTTP interaction recording, replay, and offline contract testing library