mirror of
https://github.com/DioxusLabs/dioxus
synced 2024-11-30 08:00:21 +00:00
backend proxy tests almost work!
This commit is contained in:
parent
14b4af4bbc
commit
d732269b8f
5 changed files with 52 additions and 51 deletions
|
@ -119,6 +119,7 @@ serde = "1.0.61"
|
||||||
axum_session = "0.12.1"
|
axum_session = "0.12.1"
|
||||||
axum_session_auth = "0.12.1"
|
axum_session_auth = "0.12.1"
|
||||||
axum-extra = "0.9.2"
|
axum-extra = "0.9.2"
|
||||||
|
reqwest = "0.11.24"
|
||||||
|
|
||||||
# This is a "virtual package"
|
# This is a "virtual package"
|
||||||
# It is not meant to be published, but is used so "cargo run --example XYZ" works properly
|
# It is not meant to be published, but is used so "cargo run --example XYZ" works properly
|
||||||
|
|
|
@ -49,7 +49,7 @@ walkdir = "2"
|
||||||
|
|
||||||
# tools download
|
# tools download
|
||||||
dirs = "5.0.1"
|
dirs = "5.0.1"
|
||||||
reqwest = { version = "0.11", features = [
|
reqwest = { workspace = true, features = [
|
||||||
"rustls-tls",
|
"rustls-tls",
|
||||||
"stream",
|
"stream",
|
||||||
"trust-dns",
|
"trust-dns",
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
use crate::assets::AssetConfigDropGuard;
|
use crate::assets::AssetConfigDropGuard;
|
||||||
#[cfg(feature = "plugin")]
|
|
||||||
use crate::plugin::PluginManager;
|
|
||||||
use crate::server::fullstack;
|
use crate::server::fullstack;
|
||||||
use dioxus_cli_config::Platform;
|
use dioxus_cli_config::Platform;
|
||||||
|
|
||||||
|
@ -55,7 +53,7 @@ impl Build {
|
||||||
crate_config.set_cargo_args(self.build.cargo_args.clone());
|
crate_config.set_cargo_args(self.build.cargo_args.clone());
|
||||||
|
|
||||||
// #[cfg(feature = "plugin")]
|
// #[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 {
|
let build_result = match platform {
|
||||||
Platform::Web => {
|
Platform::Web => {
|
||||||
|
@ -116,7 +114,7 @@ impl Build {
|
||||||
file.write_all(temp.as_bytes())?;
|
file.write_all(temp.as_bytes())?;
|
||||||
|
|
||||||
// #[cfg(feature = "plugin")]
|
// #[cfg(feature = "plugin")]
|
||||||
// let _ = PluginManager::on_build_finish(&crate_config, &platform);
|
// let _ = crate::plugin::PluginManager::on_build_finish(&crate_config, &platform);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -102,30 +102,43 @@ mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
use axum::{extract::Path, Router};
|
use axum::{extract::Path, Router};
|
||||||
use axum_server::Server;
|
use axum_server::{Handle, Server};
|
||||||
|
|
||||||
fn setup_servers(
|
async fn setup_servers(mut config: WebProxyConfig) -> String {
|
||||||
mut config: WebProxyConfig,
|
|
||||||
) -> (
|
|
||||||
tokio::task::JoinHandle<()>,
|
|
||||||
tokio::task::JoinHandle<()>,
|
|
||||||
String,
|
|
||||||
) {
|
|
||||||
let backend_router = Router::new().route(
|
let backend_router = Router::new().route(
|
||||||
"/*path",
|
"/*path",
|
||||||
any(|path: Path<String>| async move { format!("backend: {}", path.0) }),
|
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());
|
// The API backend server
|
||||||
let backend_addr = backend_server.local_addr();
|
let backend_handle_handle = Handle::new();
|
||||||
let backend_handle = tokio::spawn(async move { backend_server.await.unwrap() });
|
let backend_handle_handle_ = backend_handle_handle.clone();
|
||||||
config.backend = format!("http://{}{}", backend_addr, config.backend);
|
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 router = super::add_proxy(Router::new(), &config);
|
||||||
let server =
|
let server_handle_handle = Handle::new();
|
||||||
Server::bind("127.0.0.1:0".parse().unwrap()).serve(router.unwrap().into_make_service());
|
let server_handle_handle_ = server_handle_handle.clone();
|
||||||
let server_addr = server.local_addr();
|
tokio::spawn(async move {
|
||||||
let server_handle = tokio::spawn(async move { server.await.unwrap() });
|
Server::bind("127.0.0.1:0".parse().unwrap())
|
||||||
(backend_handle, server_handle, server_addr.to_string())
|
.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) {
|
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!
|
// So in day to day usage, use `http://localhost:8000/api` instead!
|
||||||
backend: path,
|
backend: path,
|
||||||
};
|
};
|
||||||
let (backend_handle, server_handle, server_addr) = setup_servers(config).await;
|
|
||||||
let resp = Client::new()
|
let server_addr = setup_servers(config).await;
|
||||||
.get(format!("http://{}/api", server_addr).parse().unwrap())
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
axum::body::to_bytes(resp.into_body(), usize::MAX)
|
reqwest::get(format!("http://{}/api", server_addr))
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.text()
|
||||||
.await
|
.await
|
||||||
.unwrap(),
|
.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!(
|
assert_eq!(
|
||||||
axum::body::to_bytes(resp.into_body(), usize::MAX)
|
reqwest::get(dbg!(format!("http://{}/api/", server_addr)))
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.text()
|
||||||
.await
|
.await
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
"backend: /api/"
|
"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!(
|
assert_eq!(
|
||||||
axum::body::to_bytes(resp.into_body(), usize::MAX)
|
reqwest::get(format!("http://{}/api/subpath", server_addr))
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.text()
|
||||||
.await
|
.await
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
"backend: /api/subpath"
|
"backend: /api/subpath"
|
||||||
);
|
);
|
||||||
backend_handle.abort();
|
|
||||||
server_handle.abort();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
|
|
|
@ -68,7 +68,6 @@ use http::header::*;
|
||||||
use server_fn::error::NoCustomError;
|
use server_fn::error::NoCustomError;
|
||||||
use server_fn::error::ServerFnErrorSerde;
|
use server_fn::error::ServerFnErrorSerde;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::sync::RwLock;
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
prelude::*, render::SSRState, serve_config::ServeConfig, server_context::DioxusServerContext,
|
prelude::*, render::SSRState, serve_config::ServeConfig, server_context::DioxusServerContext,
|
||||||
|
|
Loading…
Reference in a new issue