94 lines
3.1 KiB
Rust
94 lines
3.1 KiB
Rust
//! Snapshot tests for curl and Postman import over real-world fixtures.
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use reqlite_core::httpfile::{render_request, HttpFile};
|
|
use reqlite_core::import::{curl, postman};
|
|
|
|
fn fixtures(sub: &str) -> PathBuf {
|
|
Path::new(env!("CARGO_MANIFEST_DIR"))
|
|
.join("../../tests/fixtures")
|
|
.join(sub)
|
|
}
|
|
|
|
#[test]
|
|
fn curl_commands() {
|
|
let mut files: Vec<_> = std::fs::read_dir(fixtures("curl"))
|
|
.unwrap()
|
|
.map(|e| e.unwrap().path())
|
|
.collect();
|
|
files.sort();
|
|
assert!(files.len() >= 10);
|
|
for f in files {
|
|
let cmd = std::fs::read_to_string(&f).unwrap();
|
|
assert!(curl::looks_like_curl(&cmd), "{}", f.display());
|
|
let req = curl::from_curl(&cmd).unwrap_or_else(|e| panic!("{}: {e}", f.display()));
|
|
let http = render_request(&req, "\n");
|
|
// What we write must parse back to the same request.
|
|
let back = HttpFile::parse(http.clone());
|
|
assert!(back.request(0).unwrap().same_content(&req), "{}", f.display());
|
|
// Pasting into the single-line URL field may drop the line breaks.
|
|
let flat = curl::from_curl(&cmd.replace(['\r', '\n'], "")).unwrap();
|
|
assert_eq!(flat, req, "{} pasted as one line", f.display());
|
|
let name = f.file_stem().unwrap().to_string_lossy().into_owned();
|
|
insta::assert_snapshot!(format!("curl-{name}"), http);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn rejects_non_curl() {
|
|
assert_eq!(
|
|
curl::from_curl("wget http://x").unwrap_err(),
|
|
curl::CurlImportError::NotCurl
|
|
);
|
|
assert_eq!(
|
|
curl::from_curl("curl -H 'A: b'").unwrap_err(),
|
|
curl::CurlImportError::NoUrl
|
|
);
|
|
assert!(matches!(
|
|
curl::from_curl("curl 'unterminated").unwrap_err(),
|
|
curl::CurlImportError::Tokenize(_)
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn postman_collection_with_environment() {
|
|
let coll = std::fs::read_to_string(fixtures("postman/shop.postman_collection.json")).unwrap();
|
|
let env = std::fs::read_to_string(fixtures("postman/shop-dev.postman_environment.json")).unwrap();
|
|
let imp = postman::import(&coll, &[&env]).unwrap();
|
|
|
|
let mut out = String::new();
|
|
for (path, text) in &imp.files {
|
|
out.push_str(&format!("==> {}\n{text}\n", path.display()));
|
|
// Every generated file is valid and lossless.
|
|
let f = HttpFile::parse(text.clone());
|
|
assert_eq!(&f.write(), text);
|
|
assert!(f.request_count() > 0);
|
|
}
|
|
out.push_str(&format!(
|
|
"==> http-client.env.json\n{}\n",
|
|
imp.public_env.as_deref().unwrap_or("")
|
|
));
|
|
out.push_str(&format!(
|
|
"==> http-client.private.env.json\n{}\n",
|
|
imp.private_env.as_deref().unwrap_or("")
|
|
));
|
|
out.push_str("==> warnings\n");
|
|
for w in &imp.warnings {
|
|
out.push_str(&format!("{w}\n"));
|
|
}
|
|
insta::assert_snapshot!("postman-shop", out);
|
|
}
|
|
|
|
#[test]
|
|
fn postman_rejects_other_json() {
|
|
assert!(matches!(
|
|
postman::import("{}", &[]),
|
|
Err(postman::PostmanError::NotCollection)
|
|
));
|
|
assert!(matches!(
|
|
postman::import("nope", &[]),
|
|
Err(postman::PostmanError::Json(_))
|
|
));
|
|
}
|