Translate network lifecycle parity tests
Some checks failed
Rust API gates / api-gates (push) Has been cancelled

This commit is contained in:
2026-08-08 15:03:57 +02:00
parent 4e5d79e647
commit 069e6366b2
9 changed files with 2473 additions and 2077 deletions

View File

@@ -4,7 +4,9 @@
//! surfaces. Their behavior is implemented only when the owning API slice is
//! ported.
use std::collections::BTreeMap;
use std::marker::PhantomData;
use std::sync::Arc;
pub trait Collection<T> {}
@@ -64,7 +66,23 @@ pub struct XmlTextReader(pub String);
pub struct XmlTextWriter(pub String);
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub struct CancellationToken;
pub struct CancellationToken {
cancelled: bool,
}
impl CancellationToken {
pub const NONE: Self = Self { cancelled: false };
#[must_use]
pub const fn cancelled() -> Self {
Self { cancelled: true }
}
#[must_use]
pub const fn is_cancellation_requested(self) -> bool {
self.cancelled
}
}
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub struct Subscription;
@@ -77,7 +95,28 @@ pub struct DictionaryEntry(pub Object, pub Object);
pub struct RateLimitLease;
pub struct CancellationTokenSource;
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub struct CancellationTokenSource {
cancelled: bool,
}
impl CancellationTokenSource {
#[must_use]
pub const fn new() -> Self {
Self { cancelled: false }
}
pub const fn cancel(&mut self) {
self.cancelled = true;
}
#[must_use]
pub const fn token(self) -> CancellationToken {
CancellationToken {
cancelled: self.cancelled,
}
}
}
pub trait Close {}
@@ -91,17 +130,53 @@ pub struct TimeProvider;
pub struct MediaTypeHeaderValue(pub String);
pub struct HttpMessageHandler;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct HttpRequest {
pub method: String,
pub uri: Uri,
pub headers: BTreeMap<String, String>,
pub content_type: Option<String>,
pub body: Vec<u8>,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct HttpResponse {
pub status_code: u16,
pub headers: BTreeMap<String, String>,
pub content_type: Option<String>,
pub body: Vec<u8>,
}
impl HttpResponse {
#[must_use]
pub const fn is_success_status_code(&self) -> bool {
self.status_code >= 200 && self.status_code < 300
}
}
type HttpHandler = dyn Fn(HttpRequest) -> HttpResponse + Send + Sync;
#[derive(Clone)]
pub struct HttpMessageHandler(Arc<HttpHandler>);
impl HttpMessageHandler {
pub fn new(handler: impl Fn(HttpRequest) -> HttpResponse + Send + Sync + 'static) -> Self {
Self(Arc::new(handler))
}
#[must_use]
pub fn send(&self, request: HttpRequest) -> HttpResponse {
(self.0)(request)
}
}
pub struct HttpClient;
pub struct HttpResponse;
pub struct HttpMethod(pub String);
pub struct AuthenticationHeaderValue(pub String);
pub trait IProgress<T> {}
pub trait IProgress<T>: Send + Sync {}
pub struct IEnumerator;
@@ -148,3 +223,34 @@ pub struct UnicodeCategory(pub i32);
pub struct TextEncoding;
pub struct SocketException;
#[cfg(test)]
mod tests {
use super::{CancellationTokenSource, HttpMessageHandler, HttpRequest, HttpResponse, Uri};
use std::collections::BTreeMap;
#[test]
fn cancellation_and_http_fixture_state_are_observable() {
let mut source = CancellationTokenSource::new();
assert!(!source.token().is_cancellation_requested());
source.cancel();
assert!(source.token().is_cancellation_requested());
let handler = HttpMessageHandler::new(|request| HttpResponse {
status_code: 201,
headers: request.headers,
content_type: request.content_type,
body: request.body,
});
let response = handler.send(HttpRequest {
method: "PUT".into(),
uri: Uri("http://example.test/".into()),
headers: BTreeMap::from([("Location".into(), "slcaps://fixture".into())]),
content_type: Some("application/llsd+xml".into()),
body: b"test".to_vec(),
});
assert!(response.is_success_status_code());
assert_eq!(response.body, b"test");
assert_eq!(response.headers["Location"], "slcaps://fixture");
}
}

View File

@@ -53,6 +53,8 @@ pub enum Error {
Argument,
/// An index or destination buffer boundary was exceeded.
IndexOutOfRange,
/// An operation observed a requested cancellation.
Cancelled,
}
impl Error {
@@ -61,7 +63,7 @@ impl Error {
pub const fn csharp_member(self) -> Option<&'static str> {
match self {
Self::NotImplemented(error) => Some(error.csharp_member()),
Self::ArgumentNull | Self::Argument | Self::IndexOutOfRange => None,
Self::ArgumentNull | Self::Argument | Self::IndexOutOfRange | Self::Cancelled => None,
}
}
}
@@ -79,6 +81,7 @@ impl fmt::Display for Error {
Self::ArgumentNull => formatter.write_str("required argument was null"),
Self::Argument => formatter.write_str("argument was invalid"),
Self::IndexOutOfRange => formatter.write_str("index was out of range"),
Self::Cancelled => formatter.write_str("operation was cancelled"),
}
}
}