what-core 1.7.5

Core framework for What - an HTML-first web framework powered by Rust
Documentation
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
mod common;

use axum::http::StatusCode;
use common::*;
use serde_json::json;
use what_core::server::create_router;

// =========================================================================
// Variable tests
// =========================================================================

#[tokio::test]
async fn simple_variable_replacement() {
    let proj = TestProject::new();
    proj.add_page(
        "index.html",
        "<what>\ntitle: Hello World\n</what>\n<h1>#title#</h1>",
    );
    let (_dir, state) = proj.build_state();
    let router = create_router(state);

    let resp = get(&router, "/").await;
    assert_eq!(resp.status, StatusCode::OK);
    resp.assert_contains("<h1>Hello World</h1>");
}

#[tokio::test]
async fn variable_with_default() {
    let proj = TestProject::new();
    proj.add_page("index.html", r##"<p>#missing|default:"fallback"#</p>"##);
    let (_dir, state) = proj.build_state();
    let router = create_router(state);

    let resp = get(&router, "/").await;
    assert_eq!(resp.status, StatusCode::OK);
    resp.assert_contains("fallback");
}

#[tokio::test]
async fn code_block_protection() {
    let proj = TestProject::new();
    proj.add_page(
        "index.html",
        "<what>\nname: Alice\n</what>\n<p>#name#</p><code>#name#</code>",
    );
    let (_dir, state) = proj.build_state();
    let router = create_router(state);

    let resp = get(&router, "/").await;
    assert_eq!(resp.status, StatusCode::OK);
    resp.assert_contains("<p>Alice</p>");
    // Inside <code>, the variable should be preserved as-is
    resp.assert_contains("#name#");
}

// =========================================================================
// Loop tests
// =========================================================================

#[tokio::test]
async fn loop_with_array() {
    let proj = TestProject::new();
    proj.add_page(
        "index.html",
        r##"<ul><loop data="#items#" as="item"><li>#item.name#</li></loop></ul>"##,
    );
    let (_dir, state) = proj.build_state();
    state
        .store
        .create("items", json!({"name": "Alpha"}))
        .await
        .unwrap();
    state
        .store
        .create("items", json!({"name": "Beta"}))
        .await
        .unwrap();
    let router = create_router(state);

    let resp = get(&router, "/").await;
    assert_eq!(resp.status, StatusCode::OK);
    resp.assert_contains("<li>Alpha</li>");
    resp.assert_contains("<li>Beta</li>");
}

#[tokio::test]
async fn loop_with_alias() {
    let proj = TestProject::new();
    proj.add_page(
        "index.html",
        r##"<loop data="#posts#" as="post"><h2>#post.title#</h2></loop>"##,
    );
    let (_dir, state) = proj.build_state();
    state
        .store
        .create("posts", json!({"title": "First Post"}))
        .await
        .unwrap();
    state
        .store
        .create("posts", json!({"title": "Second Post"}))
        .await
        .unwrap();
    let router = create_router(state);

    let resp = get(&router, "/").await;
    assert_eq!(resp.status, StatusCode::OK);
    resp.assert_contains("<h2>First Post</h2>");
    resp.assert_contains("<h2>Second Post</h2>");
}

#[tokio::test]
async fn loop_index_variables() {
    let proj = TestProject::new();
    proj.add_page(
        "index.html",
        r##"<loop data="#items#" as="item"><span>#index#-#index1#-#first#-#last#</span></loop>"##,
    );
    let (_dir, state) = proj.build_state();
    state
        .store
        .set_collection(
            "items",
            vec![json!({"v": "a"}), json!({"v": "b"}), json!({"v": "c"})],
        )
        .await
        .unwrap();
    let router = create_router(state);

    let resp = get(&router, "/").await;
    assert_eq!(resp.status, StatusCode::OK);
    // First item: index=0, index1=1, first=true, last=false
    resp.assert_contains("0-1-true-false");
    // Last item: index=2, index1=3, first=false, last=true
    resp.assert_contains("2-3-false-true");
}

#[tokio::test]
async fn loop_empty_array() {
    let proj = TestProject::new();
    proj.add_page(
        "index.html",
        r##"<loop data="#items#" as="item"><li>#item.name#</li></loop>"##,
    );
    let (_dir, state) = proj.build_state();
    state.store.set_collection("items", vec![]).await.unwrap();
    let router = create_router(state);

    let resp = get(&router, "/").await;
    assert_eq!(resp.status, StatusCode::OK);
    // Empty collection produces a comment
    resp.assert_not_contains("<li>");
}

#[tokio::test]
async fn loop_over_object() {
    let proj = TestProject::new();
    proj.add_page(
        "index.html",
        r##"<loop data="#settings#" as="val"><span>#key#=#value#</span></loop>"##,
    );
    let (_dir, state) = proj.build_state();
    state
        .store
        .set("settings", json!({"color": "blue", "size": "large"}))
        .await
        .unwrap();
    let router = create_router(state);

    let resp = get(&router, "/").await;
    assert_eq!(resp.status, StatusCode::OK);
    resp.assert_contains("color=blue");
    resp.assert_contains("size=large");
}

// =========================================================================
// Conditional tests
// =========================================================================

#[tokio::test]
async fn if_true_shows_content() {
    let proj = TestProject::new();
    proj.add_page("index.html", r##"<if cond="#show#">Visible</if>"##);
    let (_dir, state) = proj.build_state();
    state.store.set("show", json!(true)).await.unwrap();
    let router = create_router(state);

    let resp = get(&router, "/").await;
    assert_eq!(resp.status, StatusCode::OK);
    resp.assert_contains("Visible");
}

#[tokio::test]
async fn if_false_hides_content() {
    let proj = TestProject::new();
    proj.add_page(
        "index.html",
        r##"<if cond="#show#">Hidden</if><p>After</p>"##,
    );
    let (_dir, state) = proj.build_state();
    state.store.set("show", json!(false)).await.unwrap();
    let router = create_router(state);

    let resp = get(&router, "/").await;
    assert_eq!(resp.status, StatusCode::OK);
    resp.assert_not_contains("Hidden");
    resp.assert_contains("After");
}

#[tokio::test]
async fn if_else_branch() {
    let proj = TestProject::new();
    proj.add_page("index.html", r##"<if cond="#show#">Yes<else/>No</if>"##);
    let (_dir, state) = proj.build_state();
    state.store.set("show", json!(false)).await.unwrap();
    let router = create_router(state);

    let resp = get(&router, "/").await;
    assert_eq!(resp.status, StatusCode::OK);
    resp.assert_contains("No");
    resp.assert_not_contains("Yes");
}

#[tokio::test]
async fn elseif_branch() {
    let proj = TestProject::new();
    proj.add_page(
        "index.html",
        r##"<if cond='#status# == "ok"'>OK<elseif cond='#status# == "err"'/>ERR<else/>UNKNOWN</if>"##,
    );
    let (_dir, state) = proj.build_state();
    state.store.set("status", json!("err")).await.unwrap();
    let router = create_router(state);

    let resp = get(&router, "/").await;
    assert_eq!(resp.status, StatusCode::OK);
    resp.assert_contains("ERR");
    resp.assert_not_contains("OK");
    resp.assert_not_contains("UNKNOWN");
}

#[tokio::test]
async fn unless_shows_when_false() {
    let proj = TestProject::new();
    proj.add_page("index.html", r##"<unless cond="#error#">All OK</unless>"##);
    let (_dir, state) = proj.build_state();
    state.store.set("error", json!(null)).await.unwrap();
    let router = create_router(state);

    let resp = get(&router, "/").await;
    assert_eq!(resp.status, StatusCode::OK);
    resp.assert_contains("All OK");
}

#[tokio::test]
async fn negation_condition() {
    let proj = TestProject::new();
    proj.add_page("index.html", r##"<if cond="!#hidden#">Shown</if>"##);
    let (_dir, state) = proj.build_state();
    state.store.set("hidden", json!(false)).await.unwrap();
    let router = create_router(state);

    let resp = get(&router, "/").await;
    assert_eq!(resp.status, StatusCode::OK);
    resp.assert_contains("Shown");
}

#[tokio::test]
async fn comparison_condition() {
    let proj = TestProject::new();
    proj.add_page(
        "index.html",
        r##"<if cond='#role# == "admin"'>Admin Panel</if>"##,
    );
    let (_dir, state) = proj.build_state();
    state.store.set("role", json!("admin")).await.unwrap();
    let router = create_router(state);

    let resp = get(&router, "/").await;
    assert_eq!(resp.status, StatusCode::OK);
    resp.assert_contains("Admin Panel");
}

// =========================================================================
// Include tests
// =========================================================================

#[tokio::test]
async fn include_partial() {
    let proj = TestProject::new();
    proj.add_page(
        "index.html",
        r##"<h1>Home</h1><include src="partials/header.html"/>"##,
    );
    let (_dir, state) = proj.build_state();
    std::fs::create_dir_all(_dir.path().join("partials")).unwrap();
    std::fs::write(
        _dir.path().join("partials/header.html"),
        "<nav>Navigation</nav>",
    )
    .unwrap();
    let router = create_router(state);

    let resp = get(&router, "/").await;
    assert_eq!(resp.status, StatusCode::OK);
    resp.assert_contains("<h1>Home</h1>");
    resp.assert_contains("<nav>Navigation</nav>");
}

#[tokio::test]
async fn include_with_attributes() {
    let proj = TestProject::new();
    proj.add_page(
        "index.html",
        r##"<include src="partials/heading.html" text="Welcome"/>"##,
    );
    let (_dir, state) = proj.build_state();
    std::fs::create_dir_all(_dir.path().join("partials")).unwrap();
    std::fs::write(
        _dir.path().join("partials/heading.html"),
        "<what>\nattribute.text = \"Default\"\n</what>\n<h2>#text#</h2>",
    )
    .unwrap();
    let router = create_router(state);

    let resp = get(&router, "/").await;
    assert_eq!(resp.status, StatusCode::OK);
    resp.assert_contains("<h2>Welcome</h2>");
}

#[tokio::test]
async fn include_missing_file() {
    let proj = TestProject::new();
    proj.add_page(
        "index.html",
        r##"<include src="partials/nonexistent.html"/><p>After</p>"##,
    );
    let (_dir, state) = proj.build_state();
    let router = create_router(state);

    let resp = get(&router, "/").await;
    assert_eq!(resp.status, StatusCode::OK);
    // Missing include produces a comment, not an error
    resp.assert_contains("<!-- include not found");
    resp.assert_contains("<p>After</p>");
}

// =========================================================================
// Component tests
// =========================================================================

#[tokio::test]
async fn component_renders() {
    let proj = TestProject::new();
    proj.add_component(
        "card.html",
        "<what>\nprops = \"title\"\ndefaults.title = \"Untitled\"\n</what>\n<div class=\"card\"><h3>#title#</h3></div>",
    );
    proj.add_page("index.html", r##"<what-card title="Test Card"/>"##);
    let (_dir, state) = proj.build_state();
    let router = create_router(state);

    let resp = get(&router, "/").await;
    assert_eq!(resp.status, StatusCode::OK);
    resp.assert_contains("Test Card");
    resp.assert_contains("card");
}

#[tokio::test]
async fn component_with_slot() {
    let proj = TestProject::new();
    proj.add_component(
        "box.html",
        "<what>\nprops = \"title\"\n</what>\n<div class=\"box\"><h3>#title#</h3><slot/></div>",
    );
    proj.add_page(
        "index.html",
        r##"<what-box title="My Box"><p>Inner content</p></what-box>"##,
    );
    let (_dir, state) = proj.build_state();
    let router = create_router(state);

    let resp = get(&router, "/").await;
    assert_eq!(resp.status, StatusCode::OK);
    resp.assert_contains("My Box");
    resp.assert_contains("<p>Inner content</p>");
}

#[tokio::test]
async fn component_defaults() {
    let proj = TestProject::new();
    proj.add_component(
        "badge.html",
        "<what>\nprops = \"label\"\ndefaults.label = \"Default Label\"\n</what>\n<span class=\"badge\">#label#</span>",
    );
    proj.add_page("index.html", r##"<what-badge/>"##);
    let (_dir, state) = proj.build_state();
    let router = create_router(state);

    let resp = get(&router, "/").await;
    assert_eq!(resp.status, StatusCode::OK);
    resp.assert_contains("Default Label");
}

#[tokio::test]
async fn component_prop_override() {
    let proj = TestProject::new();
    proj.add_component(
        "badge.html",
        "<what>\nprops = \"label\"\ndefaults.label = \"Default Label\"\n</what>\n<span class=\"badge\">#label#</span>",
    );
    proj.add_page("index.html", r##"<what-badge label="Custom"/>"##);
    let (_dir, state) = proj.build_state();
    let router = create_router(state);

    let resp = get(&router, "/").await;
    assert_eq!(resp.status, StatusCode::OK);
    resp.assert_contains("Custom");
    resp.assert_not_contains("Default Label");
}

#[tokio::test]
async fn component_with_loop() {
    let proj = TestProject::new();
    proj.add_component(
        "list.html",
        "<what>\nprops = \"items\"\n</what>\n<ul><loop data=\"#items#\" as=\"item\"><li>#item.name#</li></loop></ul>",
    );
    proj.add_page(
        "index.html",
        r##"<what-list items='[{"name":"One"},{"name":"Two"}]'/>"##,
    );
    let (_dir, state) = proj.build_state();
    let router = create_router(state);

    let resp = get(&router, "/").await;
    assert_eq!(resp.status, StatusCode::OK);
    resp.assert_contains("<li>One</li>");
    resp.assert_contains("<li>Two</li>");
}

// =========================================================================
// Processing order tests
// =========================================================================

#[tokio::test]
async fn include_before_loop() {
    let proj = TestProject::new();
    // The page includes a partial that contains a loop
    proj.add_page(
        "index.html",
        r##"<include src="partials/user_list.html"/>"##,
    );
    let (_dir, state) = proj.build_state();
    std::fs::create_dir_all(_dir.path().join("partials")).unwrap();
    std::fs::write(
        _dir.path().join("partials/user_list.html"),
        r##"<ul><loop data="#users#" as="u"><li>#u.name#</li></loop></ul>"##,
    )
    .unwrap();
    state
        .store
        .set_collection(
            "users",
            vec![json!({"name": "Alice"}), json!({"name": "Bob"})],
        )
        .await
        .unwrap();
    let router = create_router(state);

    let resp = get(&router, "/").await;
    assert_eq!(resp.status, StatusCode::OK);
    resp.assert_contains("<li>Alice</li>");
    resp.assert_contains("<li>Bob</li>");
}

#[tokio::test]
async fn conditionals_after_components() {
    let proj = TestProject::new();
    // Component output contains an <if> tag with comparison condition.
    // Component render() replaces #active# with the prop value "yes",
    // then process_custom_tags_html runs process_conditionals_html
    // which evaluates the comparison 'yes == "yes"' -> true.
    proj.add_component(
        "toggle.html",
        "<what>\nprops = \"active\"\ndefaults.active = \"no\"\n</what>\n<if cond='#active# == \"yes\"'><span>Active</span></if>",
    );
    proj.add_page("index.html", r##"<what-toggle active="yes"/>"##);
    let (_dir, state) = proj.build_state();
    let router = create_router(state);

    let resp = get(&router, "/").await;
    assert_eq!(resp.status, StatusCode::OK);
    resp.assert_contains("<span>Active</span>");
}

// =========================================================================
// Directive stripping tests
// =========================================================================

#[tokio::test]
async fn what_directives_stripped() {
    let proj = TestProject::new();
    proj.add_page(
        "index.html",
        "<what>\ntitle: My Page\nauth: all\n</what>\n<h1>#title#</h1>",
    );
    let (_dir, state) = proj.build_state();
    let router = create_router(state);

    let resp = get(&router, "/").await;
    assert_eq!(resp.status, StatusCode::OK);
    resp.assert_not_contains("<what>");
    resp.assert_not_contains("</what>");
    resp.assert_contains("<h1>My Page</h1>");
}

#[tokio::test]
async fn what_component_tags_preserved() {
    // The <what-card> tag name should NOT be confused with <what> directives
    // After processing, it becomes the rendered component output
    let proj = TestProject::new();
    proj.add_component(
        "card.html",
        "<what>\nprops = \"title\"\n</what>\n<div class=\"card\">#title#</div>",
    );
    proj.add_page("index.html", r##"<what-card title="Hello"/>"##);
    let (_dir, state) = proj.build_state();
    let router = create_router(state);

    let resp = get(&router, "/").await;
    assert_eq!(resp.status, StatusCode::OK);
    // Component should have been rendered, producing actual content
    resp.assert_contains("Hello");
    resp.assert_contains("card");
}

#[tokio::test]
async fn page_title_from_directive() {
    let proj = TestProject::new();
    proj.add_page(
        "index.html",
        "<what>\ntitle: Dashboard\n</what>\n<h1>#title#</h1>",
    );
    let (_dir, state) = proj.build_state();
    let router = create_router(state);

    let resp = get(&router, "/").await;
    assert_eq!(resp.status, StatusCode::OK);
    resp.assert_contains("<h1>Dashboard</h1>");
}

// =========================================================================
// Partial route tests (/w-partial/*)
// =========================================================================

#[tokio::test]
async fn partial_route_renders_content() {
    let proj = TestProject::new();
    proj.add_page("index.html", "<h1>Home</h1>");
    // Add a partial inside site/partials/
    let dir = proj.root().to_path_buf();
    let cd = what_core::server::content_dir_name(proj.root());
    let partials = dir.join(cd).join("partials");
    std::fs::create_dir_all(&partials).unwrap();
    std::fs::write(partials.join("time.html"), "<span>The time is now</span>").unwrap();

    let (_dir, state) = proj.build_state();
    let router = create_router(state);

    let resp = get(&router, "/w-partial/time").await;
    assert_eq!(resp.status, StatusCode::OK);
    resp.assert_contains("The time is now");
}

#[tokio::test]
async fn partial_route_returns_noindex_header() {
    let proj = TestProject::new();
    proj.add_page("index.html", "<h1>Home</h1>");
    let dir = proj.root().to_path_buf();
    let cd = what_core::server::content_dir_name(proj.root());
    let partials = dir.join(cd).join("partials");
    std::fs::create_dir_all(&partials).unwrap();
    std::fs::write(partials.join("hello.html"), "<p>Hello</p>").unwrap();

    let (_dir, state) = proj.build_state();
    let router = create_router(state);

    let resp = get(&router, "/w-partial/hello").await;
    assert_eq!(resp.status, StatusCode::OK);
    assert_eq!(resp.header("x-robots-tag"), Some("noindex, nofollow"));
}

#[tokio::test]
async fn partial_route_not_found() {
    let proj = TestProject::new();
    proj.add_page("index.html", "<h1>Home</h1>");
    let (_dir, state) = proj.build_state();
    let router = create_router(state);

    let resp = get(&router, "/w-partial/nonexistent").await;
    assert_eq!(resp.status, StatusCode::NOT_FOUND);
}

#[tokio::test]
async fn partial_route_blocks_path_traversal() {
    let proj = TestProject::new();
    proj.add_page("index.html", "<h1>Home</h1>");
    let dir = proj.root().to_path_buf();
    let cd = what_core::server::content_dir_name(proj.root());
    let partials = dir.join(cd).join("partials");
    std::fs::create_dir_all(&partials).unwrap();
    std::fs::write(partials.join("ok.html"), "<p>Ok</p>").unwrap();

    let (_dir, state) = proj.build_state();
    let router = create_router(state);

    // Attempt path traversal
    let resp = get(&router, "/w-partial/../index").await;
    assert_ne!(resp.status, StatusCode::OK);
}

#[tokio::test]
async fn ssrf_guard_blocks_private_fetch_by_default() {
    // A page fetching a metadata/private address must not reach it; the page
    // still renders (the fetch just yields no data) and nothing leaks.
    let proj = TestProject::new();
    proj.add_page(
        "index.html",
        "<what>\nfetch.meta = \"http://169.254.169.254/latest/meta-data/\"\n</what>\n<div id=\"out\">[#meta#]</div>",
    );
    let (_dir, state) = proj.build_state();
    let router = create_router(state);

    let resp = get(&router, "/").await;
    assert_eq!(resp.status, StatusCode::OK);
    // No metadata content leaked into the page.
    resp.assert_not_contains("ami-id");
    resp.assert_not_contains("security-credentials");
}