use reqwest::{Client, Method};
use serde::Serialize;
use std::sync::Arc;
use url::Url;
use skyauth::client::AtprotoOAuthClient;
use skyauth::dpop::{extract_dpop_nonce, normalize_htu, DPoPNonceCache};
use skyauth::session::OAuthSession;
use crate::error::{Result, SkybaseError};
use crate::repo::types::{
validate_rkey, CreateRecordRequest, CreateRecordResult, DeleteRecordRequest, PutRecordRequest,
PutRecordResult, RecordView, XrpcErrorResponse,
};
const MAX_ERROR_BODY_BYTES: usize = 65_536;
const MAX_SUCCESS_BODY_BYTES: usize = 1_048_576;
#[derive(Clone)]
pub struct PdsRepoClient {
session: Arc<OAuthSession>,
oauth_client: Option<Arc<AtprotoOAuthClient>>,
http_client: Client,
nonce_cache: DPoPNonceCache,
endpoint_override: Option<String>,
}
impl PdsRepoClient {
pub fn try_new(session: Arc<OAuthSession>, client: Arc<AtprotoOAuthClient>) -> Result<Self> {
let nonce_cache = client.nonce_cache().clone();
let http_client = Client::builder()
.redirect(reqwest::redirect::Policy::none())
.build()
.map_err(SkybaseError::Network)?;
Ok(Self {
session,
oauth_client: Some(client),
http_client,
nonce_cache,
endpoint_override: None,
})
}
pub fn try_from_session(session: Arc<OAuthSession>) -> Result<Self> {
let http_client = Client::builder()
.redirect(reqwest::redirect::Policy::none())
.build()
.map_err(SkybaseError::Network)?;
Ok(Self {
session,
oauth_client: None,
http_client,
nonce_cache: DPoPNonceCache::new(),
endpoint_override: None,
})
}
pub fn new(session: Arc<OAuthSession>, client: Arc<AtprotoOAuthClient>) -> Result<Self> {
Self::try_new(session, client)
}
pub fn from_session(session: Arc<OAuthSession>) -> Result<Self> {
Self::try_from_session(session)
}
pub fn from_credentials(
pds_endpoint: impl Into<String>,
repo_did: impl Into<String>,
access_token: impl Into<String>,
) -> Result<Self> {
let endpoint = pds_endpoint.into();
let did = repo_did.into();
let token = access_token.into();
let session = OAuthSession::new(
did,
token,
None,
"DPoP",
None,
None,
skyauth::dpop::DPoPKey::generate(),
Some(endpoint),
None,
None,
)
.map_err(SkybaseError::Auth)?;
Self::try_from_session(Arc::new(session))
}
#[must_use]
pub fn with_endpoint(mut self, endpoint: impl Into<String>) -> Self {
self.endpoint_override = Some(endpoint.into());
self
}
#[must_use]
pub fn with_http_client(mut self, client: Client) -> Self {
self.http_client = client;
self
}
#[must_use]
pub fn with_nonce_cache(mut self, nonce_cache: DPoPNonceCache) -> Self {
self.nonce_cache = nonce_cache;
self
}
#[must_use]
pub fn session(&self) -> &Arc<OAuthSession> {
&self.session
}
#[must_use]
pub fn did(&self) -> &str {
self.session.sub()
}
pub fn pds_endpoint(&self) -> Result<&str> {
if let Some(ref ep) = self.endpoint_override {
return Ok(ep.as_str());
}
self.session.pds_endpoint().ok_or_else(|| {
SkybaseError::Repo("No PDS endpoint found in OAuthSession or client".into())
})
}
#[must_use]
pub fn oauth_client(&self) -> Option<&Arc<AtprotoOAuthClient>> {
self.oauth_client.as_ref()
}
#[must_use]
pub fn nonce_cache(&self) -> &DPoPNonceCache {
&self.nonce_cache
}
#[must_use]
pub fn http_client(&self) -> &Client {
&self.http_client
}
pub async fn create_record<T: Serialize>(
&self,
collection: &str,
rkey: Option<&str>,
record: &T,
validate: bool,
) -> Result<CreateRecordResult> {
if let Some(rk) = rkey {
validate_rkey(rk)?;
}
let did = self.did();
let payload = CreateRecordRequest {
repo: did,
collection,
rkey,
validate,
record,
swap_commit: None,
};
let body_bytes = serde_json::to_vec(&payload).map_err(SkybaseError::Serialization)?;
let url = self.build_xrpc_url("com.atproto.repo.createRecord")?;
let resp = self
.send_with_nonce_retry(Method::POST, &url, Some(body_bytes))
.await?;
let resp_bytes = read_bounded_bytes(resp, MAX_SUCCESS_BODY_BYTES).await?;
let res_json: serde_json::Value =
serde_json::from_slice(&resp_bytes).map_err(SkybaseError::Serialization)?;
let uri = res_json
.get("uri")
.and_then(|v| v.as_str())
.filter(|s| !s.trim().is_empty())
.ok_or_else(|| {
SkybaseError::Repo("Missing or empty 'uri' in createRecord response".into())
})?
.to_string();
let cid = res_json
.get("cid")
.and_then(|v| v.as_str())
.filter(|s| !s.trim().is_empty())
.ok_or_else(|| {
SkybaseError::Repo("Missing or empty 'cid' in createRecord response".into())
})?
.to_string();
Ok(CreateRecordResult { uri, cid })
}
pub async fn delete_record(&self, collection: &str, rkey: &str) -> Result<()> {
validate_rkey(rkey)?;
let did = self.did();
let payload = DeleteRecordRequest {
repo: did,
collection,
rkey,
swap_record: None,
swap_commit: None,
};
let body_bytes = serde_json::to_vec(&payload).map_err(SkybaseError::Serialization)?;
let url = self.build_xrpc_url("com.atproto.repo.deleteRecord")?;
let _resp = self
.send_with_nonce_retry(Method::POST, &url, Some(body_bytes))
.await?;
Ok(())
}
pub async fn put_record<T: Serialize>(
&self,
collection: &str,
rkey: &str,
record: &T,
validate: bool,
) -> Result<PutRecordResult> {
validate_rkey(rkey)?;
let did = self.did();
let payload = PutRecordRequest {
repo: did,
collection,
rkey,
validate,
record,
swap_record: None,
swap_commit: None,
};
let body_bytes = serde_json::to_vec(&payload).map_err(SkybaseError::Serialization)?;
let url = self.build_xrpc_url("com.atproto.repo.putRecord")?;
let resp = self
.send_with_nonce_retry(Method::POST, &url, Some(body_bytes))
.await?;
let resp_bytes = read_bounded_bytes(resp, MAX_SUCCESS_BODY_BYTES).await?;
let res_json: serde_json::Value =
serde_json::from_slice(&resp_bytes).map_err(SkybaseError::Serialization)?;
let uri = res_json
.get("uri")
.and_then(|v| v.as_str())
.filter(|s| !s.trim().is_empty())
.ok_or_else(|| {
SkybaseError::Repo("Missing or empty 'uri' in putRecord response".into())
})?
.to_string();
let cid = res_json
.get("cid")
.and_then(|v| v.as_str())
.filter(|s| !s.trim().is_empty())
.ok_or_else(|| {
SkybaseError::Repo("Missing or empty 'cid' in putRecord response".into())
})?
.to_string();
Ok(PutRecordResult { uri, cid })
}
pub async fn get_record(&self, repo: &str, collection: &str, rkey: &str) -> Result<RecordView> {
validate_rkey(rkey)?;
let url_str = self.build_xrpc_url("com.atproto.repo.getRecord")?;
let mut parsed_url = Url::parse(&url_str)
.map_err(|e| SkybaseError::Repo(format!("Invalid URL '{url_str}': {e}")))?;
parsed_url
.query_pairs_mut()
.append_pair("repo", repo)
.append_pair("collection", collection)
.append_pair("rkey", rkey);
let resp = self
.send_with_nonce_retry(Method::GET, parsed_url.as_str(), None)
.await?;
let resp_bytes = read_bounded_bytes(resp, MAX_SUCCESS_BODY_BYTES).await?;
let record_view: RecordView =
serde_json::from_slice(&resp_bytes).map_err(SkybaseError::Serialization)?;
Ok(record_view)
}
pub async fn upload_blob(&self, mime_type: &str, bytes: Vec<u8>) -> Result<serde_json::Value> {
let url = self.build_xrpc_url("com.atproto.repo.uploadBlob")?;
let resp = self
.send_with_nonce_retry_content_type(Method::POST, &url, mime_type, Some(bytes))
.await?;
let resp_bytes = read_bounded_bytes(resp, MAX_SUCCESS_BODY_BYTES).await?;
let res_json: serde_json::Value =
serde_json::from_slice(&resp_bytes).map_err(SkybaseError::Serialization)?;
res_json
.get("blob")
.cloned()
.ok_or_else(|| SkybaseError::Repo("Missing 'blob' in uploadBlob response".into()))
}
fn build_xrpc_url(&self, nsid: &str) -> Result<String> {
let endpoint = self.pds_endpoint()?;
let trimmed_endpoint = endpoint.trim_end_matches('/');
let mut url = Url::parse(trimmed_endpoint).map_err(|e| {
SkybaseError::Repo(format!(
"Invalid PDS endpoint URL '{trimmed_endpoint}': {e}"
))
})?;
let base_path = url.path().trim_end_matches('/');
let full_path = if base_path.is_empty() {
format!("/xrpc/{nsid}")
} else {
format!("{base_path}/xrpc/{nsid}")
};
url.set_path(&full_path);
Ok(url.to_string())
}
async fn send_with_nonce_retry(
&self,
method: Method,
url_str: &str,
body_bytes: Option<Vec<u8>>,
) -> Result<reqwest::Response> {
self.send_with_nonce_retry_content_type(method, url_str, "application/json", body_bytes)
.await
}
async fn send_with_nonce_retry_content_type(
&self,
method: Method,
url_str: &str,
content_type: &str,
body_bytes: Option<Vec<u8>>,
) -> Result<reqwest::Response> {
if self.session.is_expired() {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |d| d.as_secs());
let exp = self
.session
.expires_at()
.and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
.map_or(now, |d| d.as_secs());
return Err(SkybaseError::Auth(
skyauth::error::TokenError::Expired { exp, now }.into(),
));
}
let parsed_url = Url::parse(url_str)
.map_err(|e| SkybaseError::Repo(format!("Invalid request URL '{url_str}': {e}")))?;
let server_origin = parsed_url.origin().ascii_serialization();
let htu = normalize_htu(url_str).map_err(SkybaseError::from)?;
let mut attempts: u32 = 0;
loop {
attempts = attempts.saturating_add(1);
let current_nonce = self.nonce_cache.get_nonce(&server_origin);
let dpop_proof = self
.session
.create_dpop_proof(method.as_str(), &htu, current_nonce.as_deref())
.map_err(SkybaseError::from)?;
let mut req = self
.http_client
.request(method.clone(), parsed_url.clone())
.header("Authorization", self.session.dpop_auth_header())
.header("DPoP", dpop_proof)
.header("Content-Type", content_type)
.header("Accept", "application/json");
if let Some(ref bytes) = body_bytes {
req = req.body(bytes.clone());
}
let resp = req.send().await.map_err(SkybaseError::Network)?;
let new_nonce = resp
.headers()
.get("DPoP-Nonce")
.or_else(|| resp.headers().get("dpop-nonce"))
.and_then(|h| h.to_str().ok())
.and_then(|val| extract_dpop_nonce(Some(val)));
if let Some(ref nonce_val) = new_nonce {
self.nonce_cache
.set_nonce(&server_origin, nonce_val.clone());
}
let status = resp.status();
if status.is_success() {
return Ok(resp);
}
if status == reqwest::StatusCode::UNAUTHORIZED
|| status == reqwest::StatusCode::BAD_REQUEST
{
let headers = resp.headers().clone();
let is_challenge_header = is_rs_dpop_nonce_challenge(&headers);
let (is_challenge, err_bytes) = if is_challenge_header {
(true, Vec::new())
} else {
let err_bytes = read_error_bytes_bounded(resp, MAX_ERROR_BODY_BYTES).await;
let json_val: Option<serde_json::Value> =
serde_json::from_slice(&err_bytes).ok();
let is_error_field = is_use_dpop_nonce_error(json_val.as_ref());
(is_error_field, err_bytes)
};
if is_challenge {
if attempts >= 2 {
return Err(SkybaseError::Repo(
"DPoP nonce challenge retry limit exceeded (max 1 retry)".into(),
));
}
if new_nonce.is_none() {
return Err(SkybaseError::Repo(
"PDS issued use_dpop_nonce challenge but omitted DPoP-Nonce header"
.into(),
));
}
tracing::debug!(
origin = %server_origin,
attempt = attempts,
"Received DPoP nonce challenge; retrying with fresh nonce"
);
continue;
}
let err_msg = format_xrpc_error(status, &err_bytes);
return Err(SkybaseError::Repo(err_msg));
}
let err_bytes = read_error_bytes_bounded(resp, MAX_ERROR_BODY_BYTES).await;
let err_msg = format_xrpc_error(status, &err_bytes);
return Err(SkybaseError::Repo(err_msg));
}
}
}
fn is_rs_dpop_nonce_challenge(headers: &reqwest::header::HeaderMap) -> bool {
headers
.get_all(reqwest::header::WWW_AUTHENTICATE)
.iter()
.filter_map(|v| v.to_str().ok())
.any(|challenge| {
let challenge = challenge.trim();
let Some(space) = challenge.find(' ') else {
return false;
};
let (scheme, rest) = challenge.split_at(space);
if !scheme.eq_ignore_ascii_case("DPoP") {
return false;
}
let rest = rest.trim_start();
rest.split(',').any(|param| {
let param = param.trim();
let Some((key, value)) = param.split_once('=') else {
return false;
};
if !key.trim().eq_ignore_ascii_case("error") {
return false;
}
let value = value.trim().trim_matches('"');
value.eq_ignore_ascii_case("use_dpop_nonce")
})
})
}
fn is_use_dpop_nonce_error(json: Option<&serde_json::Value>) -> bool {
json.and_then(|j| j.get("error"))
.and_then(|e| e.as_str())
.is_some_and(|err| err.eq_ignore_ascii_case("use_dpop_nonce"))
}
async fn read_bounded_bytes(mut resp: reqwest::Response, max_bytes: usize) -> Result<Vec<u8>> {
let mut buffer = Vec::new();
while let Some(chunk) = resp.chunk().await.map_err(SkybaseError::Network)? {
if buffer.len().saturating_add(chunk.len()) > max_bytes {
return Err(SkybaseError::Repo(format!(
"HTTP response body exceeded maximum limit of {max_bytes} bytes"
)));
}
buffer.extend_from_slice(&chunk);
}
Ok(buffer)
}
async fn read_error_bytes_bounded(mut resp: reqwest::Response, max_bytes: usize) -> Vec<u8> {
let mut buffer = Vec::new();
while let Ok(Some(chunk)) = resp.chunk().await {
if buffer.len().saturating_add(chunk.len()) > max_bytes {
let remaining = max_bytes.saturating_sub(buffer.len());
buffer.extend_from_slice(&chunk[..remaining]);
break;
}
buffer.extend_from_slice(&chunk);
}
buffer
}
fn format_xrpc_error(status: reqwest::StatusCode, bytes: &[u8]) -> String {
if let Ok(xrpc_err) = serde_json::from_slice::<XrpcErrorResponse>(bytes) {
let code = xrpc_err.error.unwrap_or_else(|| status.to_string());
let msg = xrpc_err
.message
.unwrap_or_else(|| "Unknown error".to_string());
format!("PDS request failed with status {status}: {code} - {msg}")
} else {
let text = String::from_utf8_lossy(bytes);
let trimmed = text.trim();
if trimmed.is_empty() {
format!("HTTP request failed with status: {status}")
} else {
format!("HTTP request failed with status {status}: {trimmed}")
}
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic, missing_docs)]
mod tests {
use super::*;
use reqwest::header::{HeaderMap, HeaderValue, WWW_AUTHENTICATE};
use serde_json::json;
#[test]
fn test_client_from_credentials() {
let client = PdsRepoClient::from_credentials(
"https://pds.example.com",
"did:plc:alice",
"access_token_123",
)
.expect("client creation from credentials should succeed");
assert_eq!(client.did(), "did:plc:alice");
assert_eq!(
client.pds_endpoint().expect("endpoint should be present"),
"https://pds.example.com"
);
assert_eq!(client.session().dpop_auth_header(), "DPoP access_token_123");
}
#[test]
fn test_client_missing_endpoint_fails_closed() {
let session = OAuthSession::new(
"did:plc:alice",
"token",
None,
"DPoP",
None,
Some(3600),
skyauth::dpop::DPoPKey::generate(),
None, None,
None,
)
.expect("session creation failed");
let client = PdsRepoClient::from_session(Arc::new(session)).expect("client creation");
assert!(client.pds_endpoint().is_err());
}
#[test]
fn test_client_endpoint_override() {
let client =
PdsRepoClient::from_credentials("https://pds.example.com", "did:plc:alice", "token")
.expect("client creation failed")
.with_endpoint("https://custom-pds.example.com");
assert_eq!(
client.pds_endpoint().expect("overridden endpoint"),
"https://custom-pds.example.com"
);
}
#[test]
fn test_is_rs_dpop_nonce_challenge_detection() {
let mut headers = HeaderMap::new();
headers.insert(
WWW_AUTHENTICATE,
HeaderValue::from_static(
"DPoP error=\"use_dpop_nonce\", error_description=\"Nonce expired\"",
),
);
assert!(is_rs_dpop_nonce_challenge(&headers));
let mut headers_other = HeaderMap::new();
headers_other.insert(
WWW_AUTHENTICATE,
HeaderValue::from_static("Bearer error=\"invalid_token\""),
);
assert!(!is_rs_dpop_nonce_challenge(&headers_other));
let headers_empty = HeaderMap::new();
assert!(!is_rs_dpop_nonce_challenge(&headers_empty));
}
#[test]
fn test_is_use_dpop_nonce_error_detection() {
let json_nonce = json!({
"error": "use_dpop_nonce",
"message": "DPoP proof requires nonce"
});
assert!(is_use_dpop_nonce_error(Some(&json_nonce)));
let json_invalid = json!({
"error": "InvalidRequest",
"message": "Missing field"
});
assert!(!is_use_dpop_nonce_error(Some(&json_invalid)));
assert!(!is_use_dpop_nonce_error(None));
}
#[tokio::test]
async fn test_client_create_record_network_roundtrip() {
use wiremock::matchers::{header_exists, method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/xrpc/com.atproto.repo.createRecord"))
.and(header_exists("authorization"))
.and(header_exists("dpop"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"uri": "at://did:plc:test_user/app.bsky.feed.post/post_1",
"cid": "bafyrei_test_cid_1"
})))
.mount(&server)
.await;
let client =
PdsRepoClient::from_credentials(server.uri(), "did:plc:test_user", "test_access_token")
.expect("client creation failed");
let res = client
.create_record(
"app.bsky.feed.post",
Some("post_1"),
&json!({ "text": "Hello, world!" }),
true,
)
.await
.expect("create_record should succeed");
assert_eq!(res.uri, "at://did:plc:test_user/app.bsky.feed.post/post_1");
assert_eq!(res.cid, "bafyrei_test_cid_1");
}
#[tokio::test]
async fn test_client_delete_record_network_roundtrip() {
use wiremock::matchers::{header_exists, method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/xrpc/com.atproto.repo.deleteRecord"))
.and(header_exists("authorization"))
.and(header_exists("dpop"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({})))
.mount(&server)
.await;
let client =
PdsRepoClient::from_credentials(server.uri(), "did:plc:test_user", "test_access_token")
.expect("client creation failed");
let res = client
.delete_record("app.bsky.feed.post", "post_to_delete")
.await;
assert!(res.is_ok(), "delete_record should succeed");
}
#[tokio::test]
async fn test_client_dpop_nonce_retry_roundtrip() {
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/xrpc/com.atproto.repo.createRecord"))
.respond_with(
ResponseTemplate::new(401)
.insert_header("DPoP-Nonce", "fresh_nonce_123")
.set_body_json(json!({
"error": "use_dpop_nonce",
"message": "DPoP proof requires nonce"
})),
)
.up_to_n_times(1)
.mount(&server)
.await;
Mock::given(method("POST"))
.and(path("/xrpc/com.atproto.repo.createRecord"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"uri": "at://did:plc:alice/app.bsky.feed.post/retried_post",
"cid": "bafyrei_retried_cid"
})))
.mount(&server)
.await;
let client = PdsRepoClient::from_credentials(server.uri(), "did:plc:alice", "token_xyz")
.expect("client creation failed");
let res = client
.create_record(
"app.bsky.feed.post",
Some("retried_post"),
&json!({ "text": "Retried successfully" }),
true,
)
.await
.expect("should transparently succeed on nonce retry");
assert_eq!(
res.uri,
"at://did:plc:alice/app.bsky.feed.post/retried_post"
);
assert_eq!(res.cid, "bafyrei_retried_cid");
let origin = url::Url::parse(&server.uri())
.unwrap()
.origin()
.ascii_serialization();
assert_eq!(
client.nonce_cache().get_nonce(&origin).as_deref(),
Some("fresh_nonce_123")
);
}
#[tokio::test]
async fn test_client_dpop_nonce_retry_limit_exceeded() {
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/xrpc/com.atproto.repo.createRecord"))
.respond_with(
ResponseTemplate::new(401)
.insert_header("DPoP-Nonce", "infinite_nonce")
.set_body_json(json!({
"error": "use_dpop_nonce",
"message": "Always challenging"
})),
)
.mount(&server)
.await;
let client = PdsRepoClient::from_credentials(server.uri(), "did:plc:alice", "token_xyz")
.expect("client creation failed");
let res = client
.create_record(
"app.bsky.feed.post",
Some("p1"),
&json!({ "text": "Infinite loop test" }),
true,
)
.await;
assert!(res.is_err());
let err_str = res.unwrap_err().to_string();
assert!(
err_str.contains("retry limit exceeded"),
"Error must indicate retry limit exceeded, got: {err_str}"
);
}
#[tokio::test]
async fn test_client_dpop_nonce_missing_header_fails() {
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/xrpc/com.atproto.repo.createRecord"))
.respond_with(ResponseTemplate::new(401).set_body_json(json!({
"error": "use_dpop_nonce",
"message": "Requires nonce but forgot header"
})))
.mount(&server)
.await;
let client = PdsRepoClient::from_credentials(server.uri(), "did:plc:alice", "token_xyz")
.expect("client creation failed");
let res = client
.create_record(
"app.bsky.feed.post",
Some("p1"),
&json!({ "text": "Missing header test" }),
true,
)
.await;
assert!(res.is_err());
let err_str = res.unwrap_err().to_string();
assert!(
err_str.contains("omitted DPoP-Nonce header"),
"Error must indicate missing nonce header, got: {err_str}"
);
}
#[tokio::test]
async fn test_client_non_challenge_401_fails_immediately() {
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/xrpc/com.atproto.repo.createRecord"))
.respond_with(ResponseTemplate::new(401).set_body_json(json!({
"error": "invalid_token",
"message": "Token expired or revoked"
})))
.mount(&server)
.await;
let client = PdsRepoClient::from_credentials(server.uri(), "did:plc:alice", "token_xyz")
.expect("client creation failed");
let res = client
.create_record(
"app.bsky.feed.post",
Some("p1"),
&json!({ "text": "Non-challenge test" }),
true,
)
.await;
assert!(res.is_err());
let err_str = res.unwrap_err().to_string();
assert!(
err_str.contains("invalid_token"),
"Error must contain invalid_token, got: {err_str}"
);
}
#[tokio::test]
async fn test_client_xrpc_error_response_formatting() {
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/xrpc/com.atproto.repo.createRecord"))
.respond_with(ResponseTemplate::new(400).set_body_json(json!({
"error": "InvalidRequest",
"message": "Schema validation failed: missing field 'text'"
})))
.mount(&server)
.await;
let client = PdsRepoClient::from_credentials(server.uri(), "did:plc:alice", "token_xyz")
.expect("client creation failed");
let res = client
.create_record("app.bsky.feed.post", Some("p1"), &json!({}), true)
.await;
assert!(res.is_err());
let err_str = res.unwrap_err().to_string();
assert!(
err_str.contains("InvalidRequest - Schema validation failed"),
"Error must format XRPC error code and message, got: {err_str}"
);
}
#[tokio::test]
async fn test_client_invalid_rkey_preflight_rejection() {
let client = PdsRepoClient::from_credentials(
"https://pds.example.com",
"did:plc:alice",
"token_xyz",
)
.expect("client creation failed");
let create_res = client
.create_record(
"app.bsky.feed.post",
Some("invalid..key"),
&json!({ "text": "bad rkey" }),
true,
)
.await;
assert!(create_res.is_err());
let delete_res = client
.delete_record("app.bsky.feed.post", "invalid/slash")
.await;
assert!(delete_res.is_err());
}
#[tokio::test]
async fn test_client_create_record_rejects_empty_uri_or_cid() {
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/xrpc/com.atproto.repo.createRecord"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"uri": " ",
"cid": "bafyrei_valid_cid"
})))
.mount(&server)
.await;
let client = PdsRepoClient::from_credentials(server.uri(), "did:plc:alice", "token_xyz")
.expect("client creation failed");
let res = client
.create_record("app.bsky.feed.post", None, &json!({ "text": "hi" }), true)
.await;
assert!(res.is_err());
assert!(res
.unwrap_err()
.to_string()
.contains("Missing or empty 'uri'"));
}
#[tokio::test]
async fn test_client_create_record_rejects_oversized_response_body() {
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
let server = MockServer::start().await;
let huge_text = "x".repeat(MAX_SUCCESS_BODY_BYTES + 1024);
let huge_body = format!(
r#"{{"uri":"at://did:plc:alice/app.bsky.feed.post/1","cid":"bafy","extra":"{huge_text}"}}"#
);
Mock::given(method("POST"))
.and(path("/xrpc/com.atproto.repo.createRecord"))
.respond_with(ResponseTemplate::new(200).set_body_raw(huge_body, "application/json"))
.mount(&server)
.await;
let client = PdsRepoClient::from_credentials(server.uri(), "did:plc:alice", "token_xyz")
.expect("client creation failed");
let res = client
.create_record("app.bsky.feed.post", None, &json!({ "text": "hi" }), true)
.await;
assert!(res.is_err());
assert!(res
.unwrap_err()
.to_string()
.contains("exceeded maximum limit"));
}
#[test]
fn test_client_try_constructors() {
let session = Arc::new(
OAuthSession::new(
"did:plc:alice",
"token",
None,
"DPoP",
None,
Some(3600),
skyauth::dpop::DPoPKey::generate(),
Some("https://pds.example.com".into()),
None,
None,
)
.expect("session creation failed"),
);
let client = PdsRepoClient::try_from_session(session)
.expect("try_from_session should succeed with valid parameters");
assert_eq!(client.did(), "did:plc:alice");
}
}