feder_runtime_server/
note.rs1use axum::{
17 Json,
18 extract::{Path, State},
19 http::{StatusCode, header},
20 response::{IntoResponse, Response},
21};
22
23use crate::app::AppState;
24
25pub async fn note(
26 State(app_state): State<AppState>,
27 Path(id): Path<String>,
28) -> Result<Response, StatusCode> {
29 let expected_suffix = format!("/notes/{id}");
31 let note = app_state
32 .notes
33 .iter()
34 .find(|note| note.id.as_str().ends_with(&expected_suffix))
35 .cloned()
36 .ok_or(StatusCode::NOT_FOUND)?;
37
38 Ok((
39 [(header::CONTENT_TYPE, "application/activity+json")],
40 Json(note),
41 )
42 .into_response())
43}
44
45#[cfg(test)]
46mod tests {
47 use axum::{
48 body::{Body, to_bytes},
49 http::{Request, StatusCode, header},
50 };
51 use serde_json::Value;
52 use tower::ServiceExt;
53
54 use crate::{app::build_router, config::RuntimeConfig};
55
56 #[tokio::test]
57 async fn returns_public_note() {
58 let app = build_router(&RuntimeConfig::default_local());
59
60 let response = app
61 .oneshot(
62 Request::builder()
63 .uri("/notes/1")
64 .body(Body::empty())
65 .expect("valid request"),
66 )
67 .await
68 .expect("response");
69
70 assert_eq!(response.status(), StatusCode::OK);
71 assert_eq!(
72 response.headers().get(header::CONTENT_TYPE).unwrap(),
73 "application/activity+json"
74 );
75
76 let body = to_bytes(response.into_body(), 2048)
77 .await
78 .expect("read response body");
79 let json: Value = serde_json::from_slice(&body).expect("valid json");
80
81 assert_eq!(json["@context"], "https://www.w3.org/ns/activitystreams");
82 assert_eq!(json["type"], "Note");
83 assert_eq!(json["id"], "http://127.0.0.1:3000/notes/1");
84 assert_eq!(json["attributedTo"], "http://127.0.0.1:3000/users/alice");
85 assert_eq!(
86 json["content"],
87 "Hello, World! This is Feder, a portable AP core for many runtimes."
88 );
89 }
90
91 #[tokio::test]
92 async fn rejects_unknown_note() {
93 let app = build_router(&RuntimeConfig::default_local());
94
95 let response = app
96 .oneshot(
97 Request::builder()
98 .uri("/notes/missing")
99 .body(Body::empty())
100 .expect("valid request"),
101 )
102 .await
103 .expect("response");
104
105 assert_eq!(response.status(), StatusCode::NOT_FOUND);
106 }
107}