Skip to content

Commit

Permalink
Fixed tests for server_fn (#2167)
Browse files Browse the repository at this point in the history
* Fixed server_fn tests

* Changed type_name to TypeId

* Fixed handling of leading slashes for server_fn endpoint
  • Loading branch information
rakshith-ravi authored Jan 8, 2024
1 parent 1ce1127 commit d8514c1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 28 deletions.
52 changes: 27 additions & 25 deletions leptos_macro/tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ 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() {
#[server]
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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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 changes: 2 additions & 2 deletions leptos_macro/tests/ui/server.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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")]
Expand Down
2 changes: 1 addition & 1 deletion server_fn/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 10 additions & 0 deletions server_fn_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() == "\"\""
Expand All @@ -391,6 +400,7 @@ pub fn server_macro_impl(
} else {
#server_fn_path::const_format::concatcp!(
#prefix,
"/",
#fn_path
)
}
Expand Down

0 comments on commit d8514c1

Please sign in to comment.