Skip to content

Commit 2066f20

Browse files
committed
test: add test for reverse!() reversing in the current app first
1 parent ca74f41 commit 2066f20

File tree

1 file changed

+65
-5
lines changed

1 file changed

+65
-5
lines changed

cot/tests/project.rs

+65-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1+
use bytes::Bytes;
12
use cot::config::ProjectConfig;
23
use cot::project::WithConfig;
34
use cot::request::Request;
45
use cot::response::{Response, ResponseExt};
56
use cot::router::{Route, Router};
67
use cot::test::Client;
7-
use cot::{App, AppBuilder, Body, Project, ProjectContext, StatusCode};
8-
9-
async fn hello(_request: Request) -> cot::Result<Response> {
10-
Ok(Response::new_html(StatusCode::OK, Body::fixed("OK")))
11-
}
8+
use cot::{reverse, App, AppBuilder, Body, Project, ProjectContext, StatusCode};
129

1310
#[cot::test]
1411
#[cfg_attr(miri, ignore)] // unsupported operation: can't call foreign function `sqlite3_open_v2`
1512
async fn cot_project_router_sub_path() {
13+
async fn hello(_request: Request) -> cot::Result<Response> {
14+
Ok(Response::new_html(StatusCode::OK, Body::fixed("OK")))
15+
}
16+
1617
struct App1;
1718
impl App for App1 {
1819
fn name(&self) -> &'static str {
@@ -56,3 +57,62 @@ async fn cot_project_router_sub_path() {
5657
let response = client.get("/app/hello").await.unwrap();
5758
assert_eq!(response.status(), StatusCode::OK);
5859
}
60+
61+
#[cot::test]
62+
#[cfg_attr(miri, ignore)] // unsupported operation: can't call foreign function `sqlite3_open_v2`
63+
async fn cot_router_reverse_local() {
64+
async fn get_index(request: Request) -> cot::Result<Response> {
65+
Ok(Response::new_html(
66+
StatusCode::OK,
67+
Body::fixed(reverse!(request, "index")?),
68+
))
69+
}
70+
71+
struct App1;
72+
impl App for App1 {
73+
fn name(&self) -> &'static str {
74+
"app1"
75+
}
76+
77+
fn router(&self) -> Router {
78+
Router::with_urls([Route::with_handler_and_name("/index1", get_index, "index")])
79+
}
80+
}
81+
82+
struct App2;
83+
impl App for App2 {
84+
fn name(&self) -> &'static str {
85+
"app2"
86+
}
87+
88+
fn router(&self) -> Router {
89+
Router::with_urls([Route::with_handler_and_name("/index2", get_index, "index")])
90+
}
91+
}
92+
93+
struct TestProject;
94+
impl Project for TestProject {
95+
fn config(&self, _config_name: &str) -> cot::Result<ProjectConfig> {
96+
Ok(ProjectConfig::default())
97+
}
98+
99+
fn register_apps(&self, apps: &mut AppBuilder, _context: &ProjectContext<WithConfig>) {
100+
apps.register_with_views(App1, "");
101+
apps.register_with_views(App2, "");
102+
}
103+
}
104+
105+
let mut client = Client::new(TestProject).await;
106+
107+
let response = client.get("/index1").await.unwrap();
108+
assert_eq!(
109+
response.into_body().into_bytes().await.unwrap(),
110+
Bytes::from("/index1")
111+
);
112+
113+
let response = client.get("/index2").await.unwrap();
114+
assert_eq!(
115+
response.into_body().into_bytes().await.unwrap(),
116+
Bytes::from("/index2")
117+
);
118+
}

0 commit comments

Comments
 (0)