this repo has no description
1mod common; 2 3use axum::{ 4 routing::any, 5 Router, 6 extract::Request, 7 http::StatusCode, 8}; 9use tokio::net::TcpListener; 10use reqwest::Client; 11use std::sync::Arc; 12 13async fn spawn_mock_upstream() -> (String, tokio::sync::mpsc::Receiver<(String, String, Option<String>)>) { 14 let (tx, rx) = tokio::sync::mpsc::channel(10); 15 let tx = Arc::new(tx); 16 17 let app = Router::new().fallback(any(move |req: Request| { 18 let tx = tx.clone(); 19 async move { 20 let method = req.method().to_string(); 21 let uri = req.uri().to_string(); 22 let auth = req.headers().get("Authorization") 23 .and_then(|h| h.to_str().ok()) 24 .map(|s| s.to_string()); 25 26 let _ = tx.send((method, uri, auth)).await; 27 (StatusCode::OK, "Mock Response") 28 } 29 })); 30 31 let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); 32 let addr = listener.local_addr().unwrap(); 33 34 tokio::spawn(async move { 35 axum::serve(listener, app).await.unwrap(); 36 }); 37 38 (format!("http://{}", addr), rx) 39} 40 41#[tokio::test] 42async fn test_proxy_via_header() { 43 let app_url = common::base_url().await; 44 let (upstream_url, mut rx) = spawn_mock_upstream().await; 45 let client = Client::new(); 46 47 let res = client.get(format!("{}/xrpc/com.example.test", app_url)) 48 .header("atproto-proxy", &upstream_url) 49 .header("Authorization", "Bearer test-token") 50 .send() 51 .await 52 .unwrap(); 53 54 assert_eq!(res.status(), StatusCode::OK); 55 56 let (method, uri, auth) = rx.recv().await.expect("Upstream should receive request"); 57 assert_eq!(method, "GET"); 58 assert_eq!(uri, "/xrpc/com.example.test"); 59 assert_eq!(auth, Some("Bearer test-token".to_string())); 60} 61 62#[tokio::test] 63async fn test_proxy_via_env_var() { 64 let (upstream_url, mut rx) = spawn_mock_upstream().await; 65 66 unsafe { std::env::set_var("APPVIEW_URL", &upstream_url); } 67 68 let app_url = common::base_url().await; 69 let client = Client::new(); 70 71 let res = client.get(format!("{}/xrpc/com.example.envtest", app_url)) 72 .send() 73 .await 74 .unwrap(); 75 76 assert_eq!(res.status(), StatusCode::OK); 77 78 let (method, uri, _) = rx.recv().await.expect("Upstream should receive request"); 79 assert_eq!(method, "GET"); 80 assert_eq!(uri, "/xrpc/com.example.envtest"); 81} 82 83#[tokio::test] 84async fn test_proxy_missing_config() { 85 unsafe { std::env::remove_var("APPVIEW_URL"); } 86 87 let app_url = common::base_url().await; 88 let client = Client::new(); 89 90 let res = client.get(format!("{}/xrpc/com.example.fail", app_url)) 91 .send() 92 .await 93 .unwrap(); 94 95 assert_eq!(res.status(), StatusCode::BAD_GATEWAY); 96}