backend proxy tests almost work!

This commit is contained in:
Jonathan Kelley 2024-02-18 09:45:27 -08:00
parent 14b4af4bbc
commit d732269b8f
No known key found for this signature in database
GPG key ID: 1FBB50F7EB0A08BE
5 changed files with 52 additions and 51 deletions

View file

@ -119,6 +119,7 @@ serde = "1.0.61"
axum_session = "0.12.1"
axum_session_auth = "0.12.1"
axum-extra = "0.9.2"
reqwest = "0.11.24"
# This is a "virtual package"
# It is not meant to be published, but is used so "cargo run --example XYZ" works properly

View file

@ -49,7 +49,7 @@ walkdir = "2"
# tools download
dirs = "5.0.1"
reqwest = { version = "0.11", features = [
reqwest = { workspace = true, features = [
"rustls-tls",
"stream",
"trust-dns",

View file

@ -1,6 +1,4 @@
use crate::assets::AssetConfigDropGuard;
#[cfg(feature = "plugin")]
use crate::plugin::PluginManager;
use crate::server::fullstack;
use dioxus_cli_config::Platform;
@ -55,7 +53,7 @@ impl Build {
crate_config.set_cargo_args(self.build.cargo_args.clone());
// #[cfg(feature = "plugin")]
// let _ = PluginManager::on_build_start(&crate_config, &platform);
// let _ = crate::plugin::PluginManager::on_build_start(&crate_config, &platform);
let build_result = match platform {
Platform::Web => {
@ -116,7 +114,7 @@ impl Build {
file.write_all(temp.as_bytes())?;
// #[cfg(feature = "plugin")]
// let _ = PluginManager::on_build_finish(&crate_config, &platform);
// let _ = crate::plugin::PluginManager::on_build_finish(&crate_config, &platform);
Ok(())
}

View file

@ -102,30 +102,43 @@ mod test {
use super::*;
use axum::{extract::Path, Router};
use axum_server::Server;
use axum_server::{Handle, Server};
fn setup_servers(
mut config: WebProxyConfig,
) -> (
tokio::task::JoinHandle<()>,
tokio::task::JoinHandle<()>,
String,
) {
async fn setup_servers(mut config: WebProxyConfig) -> String {
let backend_router = Router::new().route(
"/*path",
any(|path: Path<String>| async move { format!("backend: {}", path.0) }),
);
let backend_server =
Server::bind("127.0.0.1:0".parse().unwrap()).serve(backend_router.into_make_service());
let backend_addr = backend_server.local_addr();
let backend_handle = tokio::spawn(async move { backend_server.await.unwrap() });
config.backend = format!("http://{}{}", backend_addr, config.backend);
// The API backend server
let backend_handle_handle = Handle::new();
let backend_handle_handle_ = backend_handle_handle.clone();
tokio::spawn(async move {
Server::bind("127.0.0.1:0".parse().unwrap())
.handle(backend_handle_handle_)
.serve(backend_router.into_make_service())
.await
.unwrap();
});
// Set the user's config to this dummy API we just built so we can test it
let backend_addr = backend_handle_handle.listening().await.unwrap();
config.backend = dbg!(format!("http://{}{}", backend_addr, config.backend));
// Now set up our actual filesystem server
let router = super::add_proxy(Router::new(), &config);
let server =
Server::bind("127.0.0.1:0".parse().unwrap()).serve(router.unwrap().into_make_service());
let server_addr = server.local_addr();
let server_handle = tokio::spawn(async move { server.await.unwrap() });
(backend_handle, server_handle, server_addr.to_string())
let server_handle_handle = Handle::new();
let server_handle_handle_ = server_handle_handle.clone();
tokio::spawn(async move {
Server::bind("127.0.0.1:0".parse().unwrap())
.handle(server_handle_handle_)
.serve(router.unwrap().into_make_service())
.await
.unwrap();
});
// Expose *just* the fileystem web server's address
server_handle_handle.listening().await.unwrap().to_string()
}
async fn test_proxy_requests(path: String) {
@ -137,48 +150,38 @@ mod test {
// So in day to day usage, use `http://localhost:8000/api` instead!
backend: path,
};
let (backend_handle, server_handle, server_addr) = setup_servers(config).await;
let resp = Client::new()
.get(format!("http://{}/api", server_addr).parse().unwrap())
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let server_addr = setup_servers(config).await;
assert_eq!(
axum::body::to_bytes(resp.into_body(), usize::MAX)
reqwest::get(format!("http://{}/api", server_addr))
.await
.unwrap()
.text()
.await
.unwrap(),
"backend: /api"
"backend: api"
);
let resp = Client::new()
.get(format!("http://{}/api/", server_addr).parse().unwrap())
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(
axum::body::to_bytes(resp.into_body(), usize::MAX)
reqwest::get(dbg!(format!("http://{}/api/", server_addr)))
.await
.unwrap()
.text()
.await
.unwrap(),
"backend: /api/"
);
let resp = Client::new()
.get(
format!("http://{}/api/subpath", server_addr)
.parse()
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(
axum::body::to_bytes(resp.into_body(), usize::MAX)
reqwest::get(format!("http://{}/api/subpath", server_addr))
.await
.unwrap()
.text()
.await
.unwrap(),
"backend: /api/subpath"
);
backend_handle.abort();
server_handle.abort();
}
#[tokio::test]

View file

@ -68,7 +68,6 @@ use http::header::*;
use server_fn::error::NoCustomError;
use server_fn::error::ServerFnErrorSerde;
use std::sync::Arc;
use std::sync::RwLock;
use crate::{
prelude::*, render::SSRState, serve_config::ServeConfig, server_context::DioxusServerContext,
@ -478,7 +477,7 @@ async fn handle_server_fns_inner(
get_local_pool().spawn_pinned(move || async move {
let (parts, body) = req.into_parts();
let req = Request::from_parts(parts.clone(), body);
let res = if let Some(mut service) =
server_fn::axum::get_server_fn_service(&path_string)