mod shared {
use crux_core::{macros::effect, render::RenderOperation};
use crux_http::protocol::HttpRequest;
#[effect]
pub enum Effect {
Http(HttpRequest),
Render(RenderOperation),
}
pub enum Event {
Done(#[expect(dead_code)] crux_http::Result<crux_http::Response<Vec<u8>>>),
}
}
use crux_core::Command;
use crux_http::{command::Http, protocol::HttpRequest};
use shared::{Effect, Event};
const URL: &str = "https://example.com/posts";
fn posted(body: &serde_json::Value) -> HttpRequest {
let mut cmd: Command<Effect, Event> = Http::post(URL)
.body_json(body)
.expect("the body serialises")
.build()
.then_send(Event::Done);
cmd.effects()
.next()
.expect("an HTTP request")
.expect_http()
.operation
}
#[test]
fn body_json_mirrors_the_request_the_capability_builds() {
let body = serde_json::json!({ "title": "New Post", "body": "Hello!" });
assert_eq!(
posted(&body),
HttpRequest::post(URL).body_json(&body).build()
);
}
#[test]
fn json_sets_only_the_body() {
let body = serde_json::json!({ "title": "New Post" });
let expected = HttpRequest::post(URL).json(&body).build();
assert!(expected.headers.is_empty());
assert_eq!(
expected.body,
serde_json::to_vec(&body).expect("serialisable")
);
assert_ne!(
posted(&body),
expected,
"differs by the content-type header"
);
}