mirror of
https://github.com/leptos-rs/leptos
synced 2024-11-10 06:44:17 +00:00
Fixed tests for server_fn
(#2167)
* Fixed server_fn tests * Changed type_name to TypeId * Fixed handling of leading slashes for server_fn endpoint
This commit is contained in:
parent
853c080707
commit
f6ce82c9d1
4 changed files with 40 additions and 28 deletions
|
@ -3,7 +3,8 @@ use cfg_if::cfg_if;
|
|||
|
||||
cfg_if! {
|
||||
if #[cfg(not(feature = "ssr"))] {
|
||||
use leptos::{server, server_fn::Encoding, ServerFnError};
|
||||
use leptos::{server, server_fn::{codec, ServerFn}, ServerFnError};
|
||||
use std::any::TypeId;
|
||||
|
||||
#[test]
|
||||
fn server_default() {
|
||||
|
@ -11,9 +12,11 @@ cfg_if! {
|
|||
pub async fn my_server_action() -> Result<(), ServerFnError> {
|
||||
Ok(())
|
||||
}
|
||||
assert_eq!(MyServerAction::PREFIX, "/api");
|
||||
assert_eq!(&MyServerAction::URL[0..16], "my_server_action");
|
||||
assert_eq!(MyServerAction::ENCODING, Encoding::Url);
|
||||
assert_eq!(
|
||||
<MyServerAction as ServerFn>::PATH.trim_end_matches(char::is_numeric),
|
||||
"/api/my_server_action"
|
||||
);
|
||||
assert_eq!(TypeId::of::<<MyServerAction as ServerFn>::InputEncoding>(), TypeId::of::<codec::PostUrl>());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -22,9 +25,8 @@ cfg_if! {
|
|||
pub async fn my_server_action() -> Result<(), ServerFnError> {
|
||||
Ok(())
|
||||
}
|
||||
assert_eq!(FooBar::PREFIX, "/foo/bar");
|
||||
assert_eq!(FooBar::URL, "my_path");
|
||||
assert_eq!(FooBar::ENCODING, Encoding::Cbor);
|
||||
assert_eq!(<FooBar as ServerFn>::PATH, "/foo/bar/my_path");
|
||||
assert_eq!(TypeId::of::<<FooBar as ServerFn>::InputEncoding>(), TypeId::of::<codec::Cbor>());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -33,9 +35,8 @@ cfg_if! {
|
|||
pub async fn my_server_action() -> Result<(), ServerFnError> {
|
||||
Ok(())
|
||||
}
|
||||
assert_eq!(FooBar::PREFIX, "/foo/bar");
|
||||
assert_eq!(FooBar::URL, "my_path");
|
||||
assert_eq!(FooBar::ENCODING, Encoding::Cbor);
|
||||
assert_eq!(<FooBar as ServerFn>::PATH, "/foo/bar/my_path");
|
||||
assert_eq!(TypeId::of::<<FooBar as ServerFn>::InputEncoding>(), TypeId::of::<codec::Cbor>());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -44,9 +45,8 @@ cfg_if! {
|
|||
pub async fn my_server_action() -> Result<(), ServerFnError> {
|
||||
Ok(())
|
||||
}
|
||||
assert_eq!(FooBar::PREFIX, "/api");
|
||||
assert_eq!(FooBar::URL, "my_path");
|
||||
assert_eq!(FooBar::ENCODING, Encoding::Url);
|
||||
assert_eq!(<FooBar as ServerFn>::PATH, "/api/my_path");
|
||||
assert_eq!(TypeId::of::<<FooBar as ServerFn>::InputEncoding>(), TypeId::of::<codec::PostUrl>());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -55,9 +55,11 @@ cfg_if! {
|
|||
pub async fn my_server_action() -> Result<(), ServerFnError> {
|
||||
Ok(())
|
||||
}
|
||||
assert_eq!(FooBar::PREFIX, "/api");
|
||||
assert_eq!(&FooBar::URL[0..16], "my_server_action");
|
||||
assert_eq!(FooBar::ENCODING, Encoding::Url);
|
||||
assert_eq!(
|
||||
<FooBar as ServerFn>::PATH.trim_end_matches(char::is_numeric),
|
||||
"/api/my_server_action"
|
||||
);
|
||||
assert_eq!(TypeId::of::<<FooBar as ServerFn>::InputEncoding>(), TypeId::of::<codec::PostUrl>());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -66,9 +68,8 @@ cfg_if! {
|
|||
pub async fn my_server_action() -> Result<(), ServerFnError> {
|
||||
Ok(())
|
||||
}
|
||||
assert_eq!(MyServerAction::PREFIX, "/foo/bar");
|
||||
assert_eq!(&MyServerAction::URL[0..16], "my_server_action");
|
||||
assert_eq!(MyServerAction::ENCODING, Encoding::Url);
|
||||
assert_eq!(<MyServerAction as ServerFn>::PATH.trim_end_matches(char::is_numeric), "/foo/bar/my_server_action");
|
||||
assert_eq!(TypeId::of::<<MyServerAction as ServerFn>::InputEncoding>(), TypeId::of::<codec::PostUrl>());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -77,9 +78,11 @@ cfg_if! {
|
|||
pub async fn my_server_action() -> Result<(), ServerFnError> {
|
||||
Ok(())
|
||||
}
|
||||
assert_eq!(MyServerAction::PREFIX, "/api");
|
||||
assert_eq!(&MyServerAction::URL[0..16], "my_server_action");
|
||||
assert_eq!(MyServerAction::ENCODING, Encoding::GetJSON);
|
||||
assert_eq!(
|
||||
<MyServerAction as ServerFn>::PATH.trim_end_matches(char::is_numeric),
|
||||
"/api/my_server_action"
|
||||
);
|
||||
assert_eq!(TypeId::of::<<MyServerAction as ServerFn>::InputEncoding>(), TypeId::of::<codec::GetUrl>());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -88,9 +91,8 @@ cfg_if! {
|
|||
pub async fn my_server_action() -> Result<(), ServerFnError> {
|
||||
Ok(())
|
||||
}
|
||||
assert_eq!(MyServerAction::PREFIX, "/api");
|
||||
assert_eq!(MyServerAction::URL, "/path/to/my/endpoint");
|
||||
assert_eq!(MyServerAction::ENCODING, Encoding::Url);
|
||||
assert_eq!(<MyServerAction as ServerFn>::PATH, "/api/path/to/my/endpoint");
|
||||
assert_eq!(TypeId::of::<<MyServerAction as ServerFn>::InputEncoding>(), TypeId::of::<codec::PostUrl>());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ error: positional argument follows keyword argument
|
|||
3 | #[server(endpoint = "my_path", FooBar)]
|
||||
| ^^^^^^
|
||||
|
||||
error: keyword argument repeated: endpoint
|
||||
error: keyword argument repeated: `endpoint`
|
||||
--> tests/ui/server.rs:8:30
|
||||
|
|
||||
8 | #[server(endpoint = "first", endpoint = "second")]
|
||||
|
@ -40,7 +40,7 @@ error: unexpected extra argument
|
|||
32 | #[server(FooBar, "/foo/bar", "Cbor", "my_path", "extra")]
|
||||
| ^^^^^^^
|
||||
|
||||
error: Encoding Not Found
|
||||
error: Encoding not found.
|
||||
--> tests/ui/server.rs:37:21
|
||||
|
|
||||
37 | #[server(encoding = "wrong")]
|
||||
|
|
|
@ -64,7 +64,7 @@ reqwest = { version = "0.11", default-features = false, optional = true, feature
|
|||
] }
|
||||
|
||||
[features]
|
||||
default = ["url", "json"]
|
||||
default = ["url", "json", "cbor"]
|
||||
actix = ["ssr", "dep:actix-web", "dep:send_wrapper"]
|
||||
axum = [
|
||||
"ssr",
|
||||
|
|
|
@ -369,6 +369,15 @@ pub fn server_macro_impl(
|
|||
));
|
||||
};
|
||||
|
||||
// Remove any leading slashes, even if they exist (we'll add them below)
|
||||
let fn_path = Literal::string(
|
||||
fn_path
|
||||
.to_string()
|
||||
.trim_start_matches('\"')
|
||||
.trim_start_matches('/')
|
||||
.trim_end_matches('\"'),
|
||||
);
|
||||
|
||||
// generate path
|
||||
let fn_path_starts_with_slash = fn_path.to_string().starts_with("\"/");
|
||||
let fn_path = if fn_path_starts_with_slash || fn_path.to_string() == "\"\""
|
||||
|
@ -391,6 +400,7 @@ pub fn server_macro_impl(
|
|||
} else {
|
||||
#server_fn_path::const_format::concatcp!(
|
||||
#prefix,
|
||||
"/",
|
||||
#fn_path
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue