use std::time::Duration;
use bytes::Bytes;
use ulo_transport::ErrorKind;
use crate::Broker;
use crate::cases::app::{BYTES, ECHO, Fixture, Mounts, failed, refused_at_startup, streams};
const LARGE: usize = 8 * 1024 * 1024;
pub async fn binary<B: Broker>() {
let fixture = Fixture::<B>::start().await;
let payload = Bytes::from_static(&[0x00, 0x01, 0xfe, 0xff]);
let outcome = fixture.rpc().request::<_, Bytes>(BYTES, &payload).timeout(Duration::from_secs(5)).await;
let capabilities = fixture.capabilities();
if capabilities.binary {
assert_eq!(outcome.expect("the binary call is answered"), payload);
} else {
let error = failed(outcome, ErrorKind::BadRequest);
assert_eq!(error.reason(), Some("binary_unsupported"));
refused_at_startup(&fixture.broker, Mounts { streaming: streams(&capabilities), binary: true, every_instance: false }).await;
}
fixture.stop().await;
}
pub async fn oversized<B: Broker>() {
let fixture = Fixture::<B>::start().await;
match fixture.capabilities().max_frame {
Some(limit) => {
let text = "x".repeat(usize::try_from(limit).unwrap_or(usize::MAX - 1) + 1);
let outcome = fixture.rpc().request::<_, String>(ECHO, &text).timeout(Duration::from_secs(30)).await;
let error = failed(outcome, ErrorKind::BadRequest);
assert_eq!(error.reason(), Some("payload_too_large"));
}
None => {
let text = "x".repeat(LARGE);
match fixture.rpc().request::<_, String>(ECHO, &text).timeout(Duration::from_secs(30)).await {
Ok(echoed) => assert_eq!(echoed.len(), LARGE),
Err(error) => {
assert_eq!(error.kind(), ErrorKind::BadRequest, "an oversized payload fails as `bad_request`: {error:?}");
assert_eq!(error.reason(), Some("payload_too_large"));
}
}
}
}
fixture.stop().await;
}