Initial Dever 0.0.1

This commit is contained in:
Codex committed 2026-11-03 03:32:07 +00:00
commit 81233d098c
1479 files changed
+163782

No files matched your search

+98
View File
@@ -0,0 +1,98 @@
use super::*;
use std::{
io::{BufRead, BufReader, Read, Write},
net::TcpStream,
process::{Child, Command, Stdio},
time::{Duration, Instant},
};
struct Server(Child);
impl Drop for Server {
fn drop(&mut self) {
let _ = self.0.kill();
let _ = self.0.wait();
}
}
#[test]
fn native_stream_and_sse_deliver_before_handler_completes() {
let directory = Directory::new();
let project = Project::load(&fixture().with_file_name("live-http.dever")).unwrap();
let program = project.check().unwrap();
let binary = build::build(
&program,
&project.sources,
"serve",
&directory.0.join("live"),
)
.unwrap();
let mut server = Server(
Command::new(binary)
.current_dir(&directory.0)
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
.spawn()
.unwrap(),
);
let mut stdout = BufReader::new(server.0.stdout.take().unwrap());
let mut line = String::new();
stdout.read_line(&mut line).unwrap();
let port = line.trim().parse::<u16>().unwrap();
for path in ["/stream", "/sse"] {
let mut stream = TcpStream::connect(("127.0.0.1", port)).unwrap();
stream
.set_read_timeout(Some(Duration::from_secs(3)))
.unwrap();
write!(
stream,
"GET {path} HTTP/1.1\r\nHost: fixture.test\r\nConnection: close\r\n\r\n"
)
.unwrap();
let mut stream = BufReader::new(stream);
line.clear();
stream.read_line(&mut line).unwrap();
assert!(line.starts_with("HTTP/1.1 200"), "{line}");
loop {
line.clear();
assert!(stream.read_line(&mut line).unwrap() > 0);
if line == "\r\n" {
break;
}
}
let first = chunk(&mut stream);
let received = Instant::now();
if path == "/stream" {
assert_eq!(first, b"first");
assert_eq!(chunk(&mut stream), b"last");
assert!(received.elapsed() >= Duration::from_millis(100));
assert!(chunk(&mut stream).is_empty());
} else {
assert_eq!(
first,
"event: change\nid: \nretry: 10\ndata: 一\ndata: 二\n\n".as_bytes()
);
let mut count = 0;
loop {
let data = chunk(&mut stream);
if data.is_empty() {
break;
}
assert_eq!(data, b":\n\n");
count += 1;
}
assert!(count >= 2);
}
}
}
fn chunk(stream: &mut BufReader<TcpStream>) -> Vec<u8> {
let mut line = String::new();
assert!(stream.read_line(&mut line).unwrap() > 0);
let size = usize::from_str_radix(line.trim(), 16).unwrap();
let mut bytes = vec![0; size];
stream.read_exact(&mut bytes).unwrap();
let mut ending = [0; 2];
stream.read_exact(&mut ending).unwrap();
assert_eq!(ending, *b"\r\n");
bytes
}
+47
View File
@@ -0,0 +1,47 @@
use super::*;
use std::{
process::Command,
time::{Duration, Instant},
};
#[test]
fn native_http_open_stream_upload_early_close_and_cancel() {
let directory = Directory::new();
let project = Project::load(&fixture().with_file_name("http-stream.dever")).unwrap();
let program = project.check().unwrap();
let binary = build::build(
&program,
&project.sources,
"verify",
&directory.0.join("http-stream"),
)
.unwrap();
let output_path = directory.0.join("stdout");
let error_path = directory.0.join("stderr");
let mut child = Command::new(binary)
.stdout(std::fs::File::create(&output_path).unwrap())
.stderr(std::fs::File::create(&error_path).unwrap())
.spawn()
.unwrap();
let start = Instant::now();
let status = loop {
if let Some(status) = child.try_wait().unwrap() {
break status;
}
if start.elapsed() > Duration::from_secs(20) {
child.kill().unwrap();
child.wait().unwrap();
panic!(
"streaming native test timed out: {}",
std::fs::read_to_string(&error_path).unwrap()
);
}
std::thread::sleep(Duration::from_millis(10));
};
assert!(
status.success(),
"{}",
std::fs::read_to_string(&error_path).unwrap()
);
assert_eq!(std::fs::read(output_path).unwrap(), b"true\n");
}
+28
View File
@@ -0,0 +1,28 @@
use super::*;
#[test]
fn native_websocket_original_client_server_stream_and_faults() {
let directory = Directory::new();
let project = Project::load(&fixture().with_file_name("websocket.dever")).unwrap();
let program = project.check().unwrap();
for entry in ["verify", "opened", "invalid"] {
let binary =
build::build(&program, &project.sources, entry, &directory.0.join(entry)).unwrap();
let output = std::process::Command::new(binary).output().unwrap();
if entry == "invalid" {
assert!(!output.status.success());
let error = String::from_utf8_lossy(&output.stderr);
assert!(
error.contains("ConnectResult.Failed") && error.contains("websocket.dever:"),
"{error}"
);
} else {
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
assert_eq!(output.stdout, b"true\n");
}
}
}