-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathintegration_test.rs
More file actions
119 lines (101 loc) · 3.55 KB
/
Copy pathintegration_test.rs
File metadata and controls
119 lines (101 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/// Integration tests for RustView server HTTP endpoints.
use rustview::server;
use rustview::ui::Ui;
fn test_app(ui: &mut Ui) {
let name = ui.text_input("Name", "World");
ui.write(format!("Hello, {}!", name));
let n = ui.int_slider("Count", 0..=100, 50);
ui.progress(n as f64 / 100.0);
if ui.checkbox("Show error", false) {
ui.error("Test error");
}
if ui.button("Click me") {
ui.write("Button clicked!");
}
ui.markdown("# Test Markdown");
}
#[tokio::test]
async fn test_index_returns_html() {
let router = server::build_router(test_app);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
tokio::spawn(async move {
axum::serve(listener, router).await.unwrap();
});
// Give server time to start
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
let resp = reqwest::get(format!("http://{}/", addr)).await.unwrap();
assert_eq!(resp.status(), 200);
let body = resp.text().await.unwrap();
assert!(body.contains("<!DOCTYPE html>"));
assert!(body.contains("RustView App"));
assert!(body.contains("rustview-root"));
assert!(body.contains("Hello, World!"));
assert!(body.contains("SESSION_ID"));
// Should have widgets
assert!(body.contains("data-widget-type"));
assert!(body.contains("text_input"));
assert!(body.contains("slider"));
assert!(body.contains("checkbox"));
assert!(body.contains("button"));
// Should have CSS
assert!(body.contains("rustview-widget"));
// Should have JS shim
assert!(body.contains("EventSource"));
}
#[tokio::test]
async fn test_event_endpoint_accepts_json() {
let router = server::build_router(test_app);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
tokio::spawn(async move {
axum::serve(listener, router).await.unwrap();
});
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
// First get the page to create a session
let body = reqwest::get(format!("http://{}/", addr))
.await
.unwrap()
.text()
.await
.unwrap();
// Extract session ID from HTML
let sid_start = body.find("SESSION_ID = \"").unwrap() + 14;
let sid_end = body[sid_start..].find('"').unwrap() + sid_start;
let session_id = &body[sid_start..sid_end];
// Send a widget event
let client = reqwest::Client::new();
let resp = client
.post(format!("http://{}/event", addr))
.json(&serde_json::json!({
"sid": session_id,
"widget_id": "w-Name-1",
"value": "Alice"
}))
.send()
.await
.unwrap();
assert_eq!(resp.status(), 200);
}
#[tokio::test]
async fn test_event_invalid_session_returns_not_found() {
let router = server::build_router(test_app);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
tokio::spawn(async move {
axum::serve(listener, router).await.unwrap();
});
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
let client = reqwest::Client::new();
let resp = client
.post(format!("http://{}/event", addr))
.json(&serde_json::json!({
"sid": "00000000-0000-0000-0000-000000000000",
"widget_id": "test-widget",
"value": "test"
}))
.send()
.await
.unwrap();
assert_eq!(resp.status(), 404);
}