MCPcopy Create free account
hub / github.com/base/base / scripted

Function scripted

acceptance/src/runner.rs:563–585  ·  view source on GitHub ↗

Serves scripted HTTP replies in order, repeating the last reply indefinitely.

(replies: Vec<(u16, String)>)

Source from the content-addressed store, hash-verified

561
562 /// Serves scripted HTTP replies in order, repeating the last reply indefinitely.
563 async fn scripted(replies: Vec<(u16, String)>) -> String {
564 let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
565 let address = listener.local_addr().unwrap();
566 tokio::spawn(async move {
567 let mut replies = VecDeque::from(replies);
568 loop {
569 let Ok((mut stream, _)) = listener.accept().await else { break };
570 let (status, body) = if replies.len() > 1 {
571 replies.pop_front().unwrap()
572 } else {
573 replies[0].clone()
574 };
575 let mut request = [0; 8192];
576 let _ = stream.read(&mut request).await;
577 let response = format!(
578 "HTTP/1.1 {status} Scripted\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
579 body.len()
580 );
581 let _ = stream.write_all(response.as_bytes()).await;
582 }
583 });
584 format!("http://{address}")
585 }
586
587 fn head(number: u64, timestamp: u64) -> (u16, String) {
588 let result = json!({

Calls 4

local_addrMethod · 0.80
lenMethod · 0.45
cloneMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected