#[test]
fn unknown_code_fallback_preserves_the_raw_string_with_an_id() {
let payload =
br#"{"kind":"error","id":"op-7","code":"future_code_xyz","message":"from newer peer"}"#;
let frame = decode_payload(payload).unwrap();
match frame {
Frame::Error {
id,
code,
unrecognized_code,
..
} => {
assert_eq!(code, crate::error::WireErrorCode::Internal);
assert_eq!(id, Some(OperationId::from("op-7")));
assert_eq!(unrecognized_code.as_deref(), Some("future_code_xyz"));
}
other => panic!("expected an error frame, got {other:?}"),
}
}
#[test]
fn closed_set_code_never_populates_unrecognized_code() {
let payload =
br#"{"kind":"error","id":"op-9","code":"deadline_exceeded","message":"too slow"}"#;
match decode_payload(payload).unwrap() {
Frame::Error {
unrecognized_code, ..
} => assert!(unrecognized_code.is_none()),
other => panic!("expected an error frame, got {other:?}"),
}
}
#[test]
fn decode_scope_rejection_detail_is_reclassified_without_the_shared_prefix() {
let payload = br#"{"kind":"error","id":"op-1","code":"frame_too_large","message":"too big"}"#;
match decode_payload(payload).unwrap_err() {
CodecError::InconsistentErrorScope { detail } => {
assert!(
!detail.contains(INCONSISTENT_SCOPE_ERROR_PREFIX),
"detail must not repeat the shared prefix: {detail}"
);
assert!(detail.starts_with("connection-terminal code"));
}
other => panic!("expected InconsistentErrorScope, got {other:?}"),
}
}
#[test]
fn encode_rejects_a_fallback_frame_with_an_id() {
let payload =
br#"{"kind":"error","id":"op-7","code":"future_code_xyz","message":"from newer peer"}"#;
let frame = decode_payload(payload).unwrap();
match encode_frame(&frame).unwrap_err() {
CodecError::FallbackFrameNotEncodable { code } => {
assert_eq!(code, "future_code_xyz");
}
other => panic!("expected FallbackFrameNotEncodable, got {other:?}"),
}
}
#[test]
fn encode_rejects_a_fallback_frame_without_an_id() {
let frame = Frame::Error {
id: None,
code: crate::error::WireErrorCode::Internal,
message: "from newer peer".to_string(),
unrecognized_code: Some("future_code_xyz".to_string()),
};
match encode_frame(&frame).unwrap_err() {
CodecError::FallbackFrameNotEncodable { code } => {
assert_eq!(code, "future_code_xyz");
}
other => panic!("expected FallbackFrameNotEncodable, got {other:?}"),
}
}
#[test]
fn encode_accepts_an_honest_internal_error_frame() {
let frame = Frame::Error {
id: Some(OperationId::from("op-1")),
code: crate::error::WireErrorCode::Internal,
message: "boom".to_string(),
unrecognized_code: None,
};
let wire = encode_frame(&frame).expect("honest internal must encode");
let decoded = decode_frame(&wire, DEFAULT_MAX_FRAME_BYTES).unwrap();
assert_eq!(decoded, frame);
}