Skip to content

Commit 0ba6224

Browse files
committed
cargo-test-support: allow processing of the body of a request for test http registries
1 parent 247ca7f commit 0ba6224

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

crates/cargo-test-support/src/registry.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ use cargo_util::{registry::make_dep_path, Sha256};
55
use flate2::write::GzEncoder;
66
use flate2::Compression;
77
use std::collections::{BTreeMap, HashMap};
8+
use std::fmt;
89
use std::fs::{self, File};
9-
use std::io::{BufRead, BufReader, Write};
10+
use std::io::{BufRead, BufReader, Read, Write};
1011
use std::net::{SocketAddr, TcpListener, TcpStream};
1112
use std::path::{Path, PathBuf};
1213
use std::thread;
@@ -466,15 +467,28 @@ impl Drop for HttpServerHandle {
466467
}
467468

468469
/// Request to the test http server
469-
#[derive(Debug)]
470470
pub struct Request {
471471
pub url: Url,
472472
pub method: String,
473+
pub body: Option<Vec<u8>>,
473474
pub authorization: Option<String>,
474475
pub if_modified_since: Option<String>,
475476
pub if_none_match: Option<String>,
476477
}
477478

479+
impl fmt::Debug for Request {
480+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
481+
// body is not included as it can produce long debug outputs
482+
f.debug_struct("Request")
483+
.field("url", &self.url)
484+
.field("method", &self.method)
485+
.field("authorization", &self.authorization)
486+
.field("if_modified_since", &self.if_modified_since)
487+
.field("if_none_match", &self.if_none_match)
488+
.finish()
489+
}
490+
}
491+
478492
/// Response from the test http server
479493
pub struct Response {
480494
pub code: u32,
@@ -539,6 +553,7 @@ impl HttpServer {
539553
let mut if_modified_since = None;
540554
let mut if_none_match = None;
541555
let mut authorization = None;
556+
let mut content_len = None;
542557
loop {
543558
line.clear();
544559
if buf.read_line(&mut line).unwrap() == 0 {
@@ -556,15 +571,26 @@ impl HttpServer {
556571
"if-modified-since" => if_modified_since = Some(value),
557572
"if-none-match" => if_none_match = Some(value),
558573
"authorization" => authorization = Some(value),
574+
"content-length" => content_len = Some(value),
559575
_ => {}
560576
}
561577
}
578+
579+
let mut body = None;
580+
if let Some(con_len) = content_len {
581+
let len = con_len.parse::<u64>().unwrap();
582+
let mut content = vec![0u8; len as usize];
583+
buf.read_exact(&mut content).unwrap();
584+
body = Some(content)
585+
}
586+
562587
let req = Request {
563588
authorization,
564589
if_modified_since,
565590
if_none_match,
566591
method,
567592
url,
593+
body,
568594
};
569595
println!("req: {:#?}", req);
570596
let response = self.route(&req);

0 commit comments

Comments
 (0)