ext/web W7: finish network extension polish

Ship Cache-Control, 301 redirects, custom 404/500, sitemap/robots, and
init unique/index so the web capability roadmap is complete for P0–P2.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-08-28 11:25:33 +08:00
1 parent 6808d56c9d
commit 323449c2c3
13 files changed
+920 -34

No files matched your search

+82
View File
@@ -0,0 +1,82 @@
---
title: web W7 finish smoke — unique index, sitemap, cache-control (offline)
import web:ext/web/web.mq.md
import plugin:lib/plugin.mq.md
import sys:lib/sys.mq.md
---
# main
*p = > plugin.native_path name="web"*
1. `p`
> plugin.load path=`p`
2. *
> sys.exit code=1
*store = > web.db url="sqlite:web-fixtures/data/w7-smoke.db"*
> `store`.exec sql="DROP TABLE IF EXISTS posts"
`fields` =
| 字段 | 类型 | 可空 | 唯一 | 索引 |
|------|------|------|------|------|
| id | int | 否 | | |
| slug | text | 否 | 是 | 是 |
| title | text | 否 | | |
> `store`.init name=posts fields=`fields`
*idx = > `store`.query sql="SELECT name FROM sqlite_master WHERE type='index' AND tbl_name='posts'"*
*nidx = > len value=idx[^rows]*
1. `nidx` >= 1
> print text=unique-ok
2. *
> print text=unique-fail
`items` =
| loc | 更新 |
|-----|------|
| / | 2026-08-28 |
| /about | 2026-08-28 |
*sm = > web_sitemap_build base="https://example.com" items=`items`*
*xml = sm[^xml]*
1. `xml`
> print text=sitemap-ok
2. *
> print text=sitemap-fail
*pg = > web.page title="w7"*
*notfound = > web.page title="Missing"*
*app = > web.app page=`pg` port=18120*
*app = > `app`.configure cache_control="public, max-age=60" access_log=False*
*app = > `app`.redirect from="/old" to="/new" permanent=True*
*app = > `app`.error_page status=404 page=`notfound`*
*app = > `app`.sitemap path="/sitemap.xml" base="https://example.com" items=`items`*
*app = > `app`.robots sitemap="https://example.com/sitemap.xml"*
*rd = app[^redirects]*
*has = rd[^/old]*
1. `has`
> print text=redirect-ok
2. *
> print text=redirect-fail
*rb = app[^robots_body]*
1. `rb`
> print text=robots-ok
2. *
> print text=robots-fail
*mw = app[^middleware]*
*cc = mw[^cache_control]*
1. `cc` == "public, max-age=60"
> print text=cache-control-ok
2. *
> print text=cache-control-fail
*p404 = app[^page_404]*
1. `p404`
> print text=error-page-ok
2. *
> print text=error-page-fail
+31
View File
@@ -0,0 +1,31 @@
---
title: web W7 live server — 404, redirect, sitemap, robots, cache-control
import web:ext/web/web.mq.md
import plugin:lib/plugin.mq.md
import sys:lib/sys.mq.md
---
# main
*p = > plugin.native_path name="web"*
1. `p`
> plugin.load path=`p`
2. *
> sys.exit code=1
`items` =
| loc | note |
|-----|------|
| / | home |
| /about | about |
*pg = > web.page title="w7-live"*
*nf = > web.page title="Not Found Page"*
*app = > web.app page=`pg` port=18121*
*app = > `app`.configure cache_control="public, max-age=120"*
*app = > `app`.redirect from="/legacy" to="/" permanent=True*
*app = > `app`.error_page status=404 page=`nf`*
*app = > `app`.sitemap path="/sitemap.xml" base="http://127.0.0.1:18121" items=`items`*
*app = > `app`.robots sitemap="http://127.0.0.1:18121/sitemap.xml"*
> `app`.listen
+135
View File
@@ -1978,6 +1978,141 @@ access-log-ok",
);
}
#[test]
fn ext_web_w7_finish_smoke() {
let status = Command::new("cargo")
.args(["build", "-p", "marqdo_plugin_web"])
.current_dir(env!("CARGO_MANIFEST_DIR"))
.status()
.expect("build web plugin");
assert!(status.success(), "failed to build marqdo_plugin_web");
assert_out(
"tests/ext/web-w7-finish-smoke.mq.md",
"unique-ok
sitemap-ok
redirect-ok
robots-ok
cache-control-ok
error-page-ok",
);
}
#[test]
fn ext_web_w7_live() {
let status = Command::new("cargo")
.args(["build", "-p", "marqdo_plugin_web"])
.current_dir(env!("CARGO_MANIFEST_DIR"))
.status()
.expect("build web plugin");
assert!(status.success(), "failed to build marqdo_plugin_web");
let script = "tests/ext/web-w7-live-server.mq.md";
let bin = env!("CARGO_BIN_EXE_marqdo");
let mut child = Command::new(bin)
.args(["run", script])
.current_dir(env!("CARGO_MANIFEST_DIR"))
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("spawn w7 live server");
let base = "http://127.0.0.1:18121";
let ready = std::time::Instant::now();
loop {
if ready.elapsed().as_secs() > 10 {
let _ = child.kill();
panic!("w7 live server did not start in time");
}
let out = Command::new("curl")
.args([
"-s",
"-o",
"/dev/null",
"-w",
"%{http_code}",
&format!("{base}/"),
])
.output()
.expect("curl probe");
if String::from_utf8_lossy(&out.stdout) == "200" {
break;
}
std::thread::sleep(std::time::Duration::from_millis(300));
}
// Permanent redirect
let out = Command::new("curl")
.args([
"-s",
"-o",
"/dev/null",
"-w",
"%{http_code}|%{redirect_url}",
&format!("{base}/legacy"),
])
.output()
.expect("curl redirect");
let redir = String::from_utf8_lossy(&out.stdout);
assert!(
redir.starts_with("301|") && redir.contains("/"),
"redirect got {redir}"
);
// Custom 404
let out = Command::new("curl")
.args(["-s", "-w", "\n%{http_code}", &format!("{base}/no-such")])
.output()
.expect("curl 404");
let body = String::from_utf8_lossy(&out.stdout);
assert!(
body.contains("Not Found Page") && body.trim_end().ends_with("404"),
"404 body={body}"
);
// Sitemap
let out = Command::new("curl")
.args(["-s", &format!("{base}/sitemap.xml")])
.output()
.expect("curl sitemap");
let sm = String::from_utf8_lossy(&out.stdout);
assert!(
sm.contains("<urlset") && sm.contains("/about"),
"sitemap={sm}"
);
// robots.txt
let out = Command::new("curl")
.args(["-s", &format!("{base}/robots.txt")])
.output()
.expect("curl robots");
let rb = String::from_utf8_lossy(&out.stdout);
assert!(
rb.contains("User-agent:") && rb.contains("Sitemap:"),
"robots={rb}"
);
// Cache-Control
let out = Command::new("curl")
.args([
"-s",
"-D",
"-",
"-o",
"/dev/null",
&format!("{base}/"),
])
.output()
.expect("curl headers");
let headers = String::from_utf8_lossy(&out.stdout).to_ascii_lowercase();
assert!(
headers.contains("cache-control: public, max-age=120"),
"headers={headers}"
);
let _ = child.kill();
let _ = child.wait();
}
#[test]
fn ext_web_ws_broadcast_live() {
use futures_util::{SinkExt, StreamExt};