Skip to content

Commit

Permalink
Support store public request body in the HTTP mock server
Browse files Browse the repository at this point in the history
  • Loading branch information
Rustin170506 committed Feb 19, 2023
1 parent 7359094 commit 74ad9fa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
8 changes: 8 additions & 0 deletions crates/cargo-test-support/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ impl RegistryBuilder {
let server = HttpServer::new(
registry_path.clone(),
dl_path,
api_path.clone(),
token.clone(),
self.auth_required,
self.custom_responders,
Expand Down Expand Up @@ -585,6 +586,7 @@ pub struct HttpServer {
listener: TcpListener,
registry_path: PathBuf,
dl_path: PathBuf,
api_path: PathBuf,
addr: SocketAddr,
token: Token,
auth_required: bool,
Expand All @@ -604,6 +606,7 @@ impl HttpServer {
pub fn new(
registry_path: PathBuf,
dl_path: PathBuf,
api_path: PathBuf,
token: Token,
auth_required: bool,
api_responders: HashMap<
Expand All @@ -617,6 +620,7 @@ impl HttpServer {
listener,
registry_path,
dl_path,
api_path,
addr,
token,
auth_required,
Expand Down Expand Up @@ -1007,6 +1011,10 @@ impl HttpServer {

pub fn check_authorized_publish(&self, req: &Request) -> Response {
if let Some(body) = &req.body {
let path = self.api_path.join("api/v1/crates/new");
t!(fs::create_dir_all(path.parent().unwrap()));
t!(fs::write(&path, body));

// Get the metadata of the package
let (len, remaining) = body.split_at(4);
let json_len = u32::from_le_bytes(len.try_into().unwrap());
Expand Down
33 changes: 28 additions & 5 deletions tests/testsuite/alt_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,14 @@ fn publish_to_alt_registry() {

#[cargo_test]
fn publish_with_crates_io_dep() {
registry::alt_init();
// crates.io registry.
let _dummy_reg = registry::init();
// Alternative registry.
let _alt_reg = RegistryBuilder::new()
.http_api()
.http_index()
.alternative_named("alternative")
.build();
let p = project()
.file(
"Cargo.toml",
Expand All @@ -450,10 +457,26 @@ fn publish_with_crates_io_dep() {

Package::new("bar", "0.0.1").publish();

// Login so that we have the token available
p.cargo("login --registry alternative TOKEN").run();

p.cargo("publish --registry alternative").run();
p.cargo("publish --registry alternative")
.with_stderr(
"\
[UPDATING] `alternative` index
[WARNING] [..]
[..]
[PACKAGING] foo v0.0.1 [..]
[UPDATING] `dummy-registry` index
[VERIFYING] foo v0.0.1 [..]
[DOWNLOADING] [..]
[DOWNLOADED] bar v0.0.1 (registry `dummy-registry`)
[COMPILING] bar v0.0.1
[COMPILING] foo v0.0.1 [..]
[FINISHED] [..]
[PACKAGED] [..]
[UPLOADING] foo v0.0.1 [..]
[UPDATING] `alternative` index
",
)
.run();

validate_alt_upload(
r#"{
Expand Down

0 comments on commit 74ad9fa

Please sign in to comment.