|
| 1 | +use bytes::Bytes; |
1 | 2 | use cot::config::ProjectConfig;
|
2 | 3 | use cot::project::WithConfig;
|
3 | 4 | use cot::request::Request;
|
4 | 5 | use cot::response::{Response, ResponseExt};
|
5 | 6 | use cot::router::{Route, Router};
|
6 | 7 | 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}; |
12 | 9 |
|
13 | 10 | #[cot::test]
|
14 | 11 | #[cfg_attr(miri, ignore)] // unsupported operation: can't call foreign function `sqlite3_open_v2`
|
15 | 12 | 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 | + |
16 | 17 | struct App1;
|
17 | 18 | impl App for App1 {
|
18 | 19 | fn name(&self) -> &'static str {
|
@@ -56,3 +57,62 @@ async fn cot_project_router_sub_path() {
|
56 | 57 | let response = client.get("/app/hello").await.unwrap();
|
57 | 58 | assert_eq!(response.status(), StatusCode::OK);
|
58 | 59 | }
|
| 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