pub fn mock_remote_crate<'server>(
  name: &str,
  version: &str,
  mock_server: &'server MockServer,
) -> MockRemoteCrateInfo<'server> {
  // Crate info mock response
  let mock_metadata = mock_server.mock(|when, then| {
    when.method(GET).path(format!("/api/v1/crates/{}", name));
    // Note that `crate[versions]` is an arbitrary value that must only match a `versions[id]`
    then.status(200).json_body(json!({
        "crate": {
            "id": name,
            "name": name,
            "versions": [
                123456
            ],
        },
        "versions": [
            {
                "id": 123456,
                "crate": name,
                "num": version,
                "dl_path": format!("/api/v1/crates/{}/{}/download", name, version),
            }
        ],
    }));
  });

  // Create archive contents
  let dir = tempfile::TempDir::new().unwrap();
  let tar_path = dir.as_ref().join(format!("{}.tar.gz", name));
  {
    create_dir_all(dir.as_ref().join("archive")).unwrap();
    File::create(dir.as_ref().join("archive/test")).unwrap();
    write(
      dir.as_ref().join("archive/Cargo.toml"),
      named_toml_contents(name, version),
    )
    .unwrap();
    write(
      dir.as_ref().join("archive/Cargo.lock"),
      named_lock_contents(name, version),
    )
    .unwrap();

    let tar_gz: File = File::create(&tar_path).unwrap();
    let enc = flate2::write::GzEncoder::new(tar_gz, Compression::default());
    let mut tar = tar::Builder::new(enc);
    tar
      .append_dir_all(package_ident(name, version), dir.as_ref().join("archive"))
      .unwrap();
  }

  // Create download mock response
  let mock_download = mock_server.mock(|when, then| {
    when
      .method(GET)
      .path(format!("/api/v1/crates/{}/{}/download", name, version));
    then
      .status(200)
      .header("content-type", "application/x-tar")
      .body_from_file(&tar_path.display().to_string());
  });

  MockRemoteCrateInfo {
    data_dir: dir,
    endpoints: vec![mock_metadata, mock_download],
  }
}
/// Configures the given mock_server (representing a crates.io remote) to return
/// mock responses for the given crate and version .
Configures the given mock_server (representing a crates.io remote) to return
mock responses for the given crate and version .
Configures the given mock_server ( representing a crates . io remote ) to return mock responses for the given crate and version .
pub fn mock_remote_crate<'server>(
  name: &str,
  version: &str,
  mock_server: &'server MockServer,
) -> MockRemoteCrateInfo<'server> {
  // Crate info mock response
  let mock_metadata = mock_server.mock(|when, then| {
    when.method(GET).path(format!("/api/v1/crates/{}", name));
    // Note that `crate[versions]` is an arbitrary value that must only match a `versions[id]`
    then.status(200).json_body(json!({
        "crate": {
            "id": name,
            "name": name,
            "versions": [
                123456
            ],
        },
        "versions": [
            {
                "id": 123456,
                "crate": name,
                "num": version,
                "dl_path": format!("/api/v1/crates/{}/{}/download", name, version),
            }
        ],
    }));
  });
  // Create archive contents
  let dir = tempfile::TempDir::new().unwrap();
  let tar_path = dir.as_ref().join(format!("{}.tar.gz", name));
  {
    create_dir_all(dir.as_ref().join("archive")).unwrap();
    File::create(dir.as_ref().join("archive/test")).unwrap();
    write(
      dir.as_ref().join("archive/Cargo.toml"),
      named_toml_contents(name, version),
    )
    .unwrap();
    write(
      dir.as_ref().join("archive/Cargo.lock"),
      named_lock_contents(name, version),
    )
    .unwrap();
    let tar_gz: File = File::create(&tar_path).unwrap();
    let enc = flate2::write::GzEncoder::new(tar_gz, Compression::default());
    let mut tar = tar::Builder::new(enc);
    tar
      .append_dir_all(package_ident(name, version), dir.as_ref().join("archive"))
      .unwrap();
  }
  // Create download mock response
  let mock_download = mock_server.mock(|when, then| {
    when
      .method(GET)
      .path(format!("/api/v1/crates/{}/{}/download", name, version));
    then
      .status(200)
      .header("content-type", "application/x-tar")
      .body_from_file(&tar_path.display().to_string());
  });
  MockRemoteCrateInfo {
    data_dir: dir,
    endpoints: vec![mock_metadata, mock_download],
  }
}
pub fn mock_remote_crate < ' server > ( name : & str , version : & str , mock_server : & ' server MockServer , ) -> MockRemoteCrateInfo < ' server > { // Crate info mock response let mock_metadata = mock_server . mock ( | when , then | { when . method ( GET ) . path ( format ! ( " " name ) ) ; // Note that `crate[versions]` is an arbitrary value that must only match a `versions[id]` then . status ( 200 ) . json_body ( json ! ( { " " { " " name " " name " " [ 123456 ] } " " [ { " " 123456 " " name " " version " " format ( " " name version ) } ] } ) ) ; } ) ; // Create archive contents let dir = tempfile :: TempDir :: new ( ) . unwrap ( ) ; let tar_path = dir . as_ref ( ) . join ( format ! ( " " name ) ) ; { create_dir_all ( dir . as_ref ( ) . join ( " " ) ) . unwrap ( ) ; File :: create ( dir . as_ref ( ) . join ( " " ) ) . unwrap ( ) ; write ( dir . as_ref ( ) . join ( " " ) , named_toml_contents ( name , version ) , ) . unwrap ( ) ; write ( dir . as_ref ( ) . join ( " " ) , named_lock_contents ( name , version ) , ) . unwrap ( ) ; let tar_gz : File = File :: create ( & tar_path ) . unwrap ( ) ; let enc = flate2 :: write :: GzEncoder :: new ( tar_gz , Compression :: default ( ) ) ; let mut tar = tar :: Builder :: new ( enc ) ; tar . append_dir_all ( package_ident ( name , version ) , dir . as_ref ( ) . join ( " " ) ) . unwrap ( ) ; } // Create download mock response let mock_download = mock_server . mock ( | when , then | { when . method ( GET ) . path ( format ! ( " " name version ) ) ; then . status ( 200 ) . header ( " " , " " ) . body_from_file ( & tar_path . display ( ) . to_string ( ) ) ; } ) ; MockRemoteCrateInfo { data_dir : dir , endpoints : vec ! [ mock_metadata mock_download ] , } }
Configures the given mock_server (representing a crates.io remote) to return
mock responses for the given crate and version .
Configures the given mock_server ( representing a crates . io remote ) to return mock responses for the given crate and version .
name
&str
version
&str
mock_server
&'server MockServer
name
&str
version
&str
mock_server
&'server MockServer