Initial impl
This commit is contained in:
commit
4b729c5d32
133 files changed
+20475
No files matched your search
@@ -0,0 +1,93 @@
|
||||
//! 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(_))
|
||||
));
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
//! A cheap, deterministic stand-in for fuzzing that runs on stable: build
|
||||
//! many `.http` files from realistic line fragments and check the parser's
|
||||
//! invariants on each.
|
||||
|
||||
use reqlite_core::httpfile::HttpFile;
|
||||
use reqlite_core::Header;
|
||||
|
||||
const FRAGMENTS: &[&str] = &[
|
||||
"###",
|
||||
"### Title",
|
||||
"### spaced title ",
|
||||
"",
|
||||
"",
|
||||
" ",
|
||||
"# comment",
|
||||
"// comment",
|
||||
"# @name x",
|
||||
"# @timeout 5",
|
||||
"# @no-redirect",
|
||||
"# @weird thing",
|
||||
"@host = example.com",
|
||||
"@ bad",
|
||||
"GET http://{{host}}/a",
|
||||
"POST /b HTTP/1.1",
|
||||
"PUT https://x/y?z=1",
|
||||
"https://bare.example",
|
||||
"DELETE",
|
||||
" ?a=1",
|
||||
" &b=2",
|
||||
" /more",
|
||||
"Content-Type: application/json",
|
||||
"Content-Type: multipart/form-data; boundary=B",
|
||||
"X-A: 1",
|
||||
"# X-Off: 2",
|
||||
"not a header",
|
||||
"{",
|
||||
" \"a\": 1",
|
||||
"}",
|
||||
"< ./file.json",
|
||||
"<@ ./tpl.json",
|
||||
"--B",
|
||||
"Content-Disposition: form-data; name=\"f\"",
|
||||
"--B--",
|
||||
"> {%",
|
||||
" client.log(1);",
|
||||
"%}",
|
||||
"> ./h.js",
|
||||
"<> ./prev.json",
|
||||
"{{var}}",
|
||||
"\u{feff}bom",
|
||||
"ünïcödé: ✓",
|
||||
"\t\ttabs",
|
||||
];
|
||||
|
||||
/// xorshift64*: deterministic, no dependencies.
|
||||
struct Rng(u64);
|
||||
|
||||
impl Rng {
|
||||
fn next(&mut self) -> u64 {
|
||||
self.0 ^= self.0 >> 12;
|
||||
self.0 ^= self.0 << 25;
|
||||
self.0 ^= self.0 >> 27;
|
||||
self.0.wrapping_mul(0x2545_f491_4f6c_dd1d)
|
||||
}
|
||||
fn below(&mut self, n: usize) -> usize {
|
||||
(self.next() % n as u64) as usize
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invariants_hold_for_generated_files() {
|
||||
// REQLITE_FUZZ_CASES=200000 for a longer search.
|
||||
let cases: usize = std::env::var("REQLITE_FUZZ_CASES")
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.unwrap_or(3000);
|
||||
let seed = std::env::var("REQLITE_FUZZ_SEED")
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.unwrap_or(0x5eed_1234_abcd_0001);
|
||||
let mut rng = Rng(seed);
|
||||
for case in 0..cases {
|
||||
let lines = rng.below(40);
|
||||
let eol = if rng.below(4) == 0 { "\r\n" } else { "\n" };
|
||||
let mut text = String::new();
|
||||
for _ in 0..lines {
|
||||
text.push_str(FRAGMENTS[rng.below(FRAGMENTS.len())]);
|
||||
text.push_str(eol);
|
||||
}
|
||||
if rng.below(3) == 0 {
|
||||
text.truncate(text.trim_end_matches(['\r', '\n']).len());
|
||||
}
|
||||
|
||||
let f = HttpFile::parse(text.clone());
|
||||
assert_eq!(f.write(), text, "case {case}: round trip\n{text:?}");
|
||||
for i in 0..f.request_count() {
|
||||
let r = f.request(i).unwrap().clone();
|
||||
assert_eq!(
|
||||
f.replace_request(i, &r).unwrap(),
|
||||
text,
|
||||
"case {case}: identity edit #{i}\n{text:?}"
|
||||
);
|
||||
|
||||
// A real edit changes only this request's span.
|
||||
let mut edited = r.clone();
|
||||
edited.headers.push(Header::new("X-Fuzz", "1"));
|
||||
let out = f.replace_request(i, &edited).unwrap();
|
||||
assert_eq!(
|
||||
&out[..r.source.start],
|
||||
&text[..r.source.start],
|
||||
"case {case}: prefix\n{text:?}"
|
||||
);
|
||||
assert!(out.ends_with(&text[r.source.end..]), "case {case}: suffix\n{text:?}");
|
||||
let g = HttpFile::parse(out);
|
||||
assert!(
|
||||
g.requests().any(|q| q.header("X-Fuzz") == Some("1")),
|
||||
"case {case}: edit visible\n{text:?}"
|
||||
);
|
||||
|
||||
let src = f.request_source(i).unwrap().to_string();
|
||||
assert_eq!(
|
||||
f.replace_request_text(i, &src).unwrap(),
|
||||
text,
|
||||
"case {case}: source round trip\n{text:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
---
|
||||
source: crates/core/tests/import.rs
|
||||
expression: http
|
||||
---
|
||||
GET https://api.github.com/repos/rust-lang/rust/issues?state=open&per_page=5
|
||||
accept: application/vnd.github+json
|
||||
accept-language: en-US,en;q=0.9,sv;q=0.8
|
||||
cache-control: no-cache
|
||||
origin: https://github.com
|
||||
priority: u=1, i
|
||||
sec-fetch-mode: cors
|
||||
user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36
|
||||
@@ -0,0 +1,10 @@
|
||||
---
|
||||
source: crates/core/tests/import.rs
|
||||
expression: http
|
||||
---
|
||||
POST https://example.com/api/graphql
|
||||
accept: */*
|
||||
content-type: application/json
|
||||
Cookie: session=abc123; theme=dark
|
||||
|
||||
{"query":"query { me { name } }","note":"it's here\nnext line"}
|
||||
@@ -0,0 +1,6 @@
|
||||
---
|
||||
source: crates/core/tests/import.rs
|
||||
expression: http
|
||||
---
|
||||
GET https://example.com/search?q=a%20b
|
||||
accept: text/html
|
||||
@@ -0,0 +1,10 @@
|
||||
---
|
||||
source: crates/core/tests/import.rs
|
||||
expression: http
|
||||
---
|
||||
POST https://example.com/api/items?page=2&size=10
|
||||
accept: application/json
|
||||
content-type: application/json
|
||||
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36
|
||||
|
||||
{"name":"Widget 50%","price":9.5}
|
||||
@@ -0,0 +1,10 @@
|
||||
---
|
||||
source: crates/core/tests/import.rs
|
||||
expression: http
|
||||
---
|
||||
POST https://httpbin.org/post
|
||||
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:140.0) Gecko/20100101 Firefox/140.0
|
||||
Content-Type: application/x-www-form-urlencoded
|
||||
Origin: https://httpbin.org
|
||||
|
||||
a=1&b=two%20words
|
||||
@@ -0,0 +1,11 @@
|
||||
---
|
||||
source: crates/core/tests/import.rs
|
||||
expression: http
|
||||
---
|
||||
GET https://developer.mozilla.org/api/v1/whoami
|
||||
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:140.0) Gecko/20100101 Firefox/140.0
|
||||
Accept: */*
|
||||
Accept-Language: en-US,en;q=0.5
|
||||
Referer: https://developer.mozilla.org/en-US/
|
||||
Connection: keep-alive
|
||||
Sec-Fetch-Mode: cors
|
||||
@@ -0,0 +1,8 @@
|
||||
---
|
||||
source: crates/core/tests/import.rs
|
||||
expression: http
|
||||
---
|
||||
PUT https://httpbin.org/anything
|
||||
Content-Type: application/json
|
||||
|
||||
{"ok":true}
|
||||
@@ -0,0 +1,8 @@
|
||||
---
|
||||
source: crates/core/tests/import.rs
|
||||
expression: http
|
||||
---
|
||||
# @timeout 10
|
||||
# @insecure
|
||||
DELETE https://localhost:8443/api/users/42
|
||||
Authorization: Basic admin s3cret
|
||||
@@ -0,0 +1,8 @@
|
||||
---
|
||||
source: crates/core/tests/import.rs
|
||||
expression: http
|
||||
---
|
||||
PATCH https://api.example.com/users/1
|
||||
Content-Type: application/json
|
||||
|
||||
< payloads/patch.json
|
||||
@@ -0,0 +1,6 @@
|
||||
---
|
||||
source: crates/core/tests/import.rs
|
||||
expression: http
|
||||
---
|
||||
GET https://api.example.com/search?q=rust%20lang&page=2
|
||||
User-Agent: reqlite-test/1.0
|
||||
@@ -0,0 +1,5 @@
|
||||
---
|
||||
source: crates/core/tests/import.rs
|
||||
expression: http
|
||||
---
|
||||
HEAD https://example.com/ HTTP/2
|
||||
@@ -0,0 +1,9 @@
|
||||
---
|
||||
source: crates/core/tests/import.rs
|
||||
expression: http
|
||||
---
|
||||
POST https://api.example.com/users
|
||||
Content-Type: application/json
|
||||
Accept: application/json
|
||||
|
||||
{"name":"Ada"}
|
||||
@@ -0,0 +1,21 @@
|
||||
---
|
||||
source: crates/core/tests/import.rs
|
||||
expression: http
|
||||
---
|
||||
POST https://upload.example.com/v1/photos
|
||||
Content-Type: multipart/form-data; boundary=WebAppBoundary
|
||||
|
||||
--WebAppBoundary
|
||||
Content-Disposition: form-data; name="title"
|
||||
|
||||
Holiday photo
|
||||
--WebAppBoundary
|
||||
Content-Disposition: form-data; name="file"; filename="beach.jpg"
|
||||
Content-Type: image/jpeg
|
||||
|
||||
< ./photos/beach.jpg
|
||||
--WebAppBoundary
|
||||
Content-Disposition: form-data; name="meta"
|
||||
|
||||
< ./meta.json
|
||||
--WebAppBoundary--
|
||||
@@ -0,0 +1,94 @@
|
||||
---
|
||||
source: crates/core/tests/import.rs
|
||||
expression: out
|
||||
---
|
||||
==> products/admin/admin.http
|
||||
### Delete product
|
||||
DELETE {{baseUrl}}/products/1
|
||||
Authorization: Basic admin {{adminPass}}
|
||||
|
||||
### Search (GraphQL)
|
||||
POST {{baseUrl}}/graphql
|
||||
Authorization: Basic admin {{adminPass}}
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"query": "query { products { id name } }",
|
||||
"variables": {
|
||||
"first": 5
|
||||
}
|
||||
}
|
||||
|
||||
==> products/products.http
|
||||
### List products
|
||||
GET {{baseUrl}}/products?limit={{pageSize}}&sort=name
|
||||
Accept: application/json
|
||||
# X-Debug: 1
|
||||
Authorization: Bearer {{token}}
|
||||
|
||||
### Create product
|
||||
POST {{baseUrl}}/products
|
||||
Authorization: Bearer {{token}}
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"name": "Widget",
|
||||
"price": 9.5
|
||||
}
|
||||
|
||||
### Upload image
|
||||
POST {{baseUrl}}/products/1/image
|
||||
Authorization: Bearer {{token}}
|
||||
Content-Type: multipart/form-data; boundary=WebAppBoundary
|
||||
|
||||
--WebAppBoundary
|
||||
Content-Disposition: form-data; name="caption"
|
||||
|
||||
Front view
|
||||
--WebAppBoundary
|
||||
Content-Disposition: form-data; name="image"; filename="widget.png"
|
||||
|
||||
< /home/me/widget.png
|
||||
--WebAppBoundary--
|
||||
|
||||
==> auth/auth.http
|
||||
### Token
|
||||
POST https://auth.example.com/oauth/token
|
||||
Content-Type: application/x-www-form-urlencoded
|
||||
|
||||
grant_type=client_credentials&client_id={{clientId}}
|
||||
|
||||
### API key ping
|
||||
GET {{baseUrl}}/ping
|
||||
X-Api-Key: {{apiKey}}
|
||||
|
||||
### Digest thing
|
||||
GET {{baseUrl}}/digest
|
||||
|
||||
==> shop-api.http
|
||||
### Health
|
||||
GET {{baseUrl}}/health
|
||||
|
||||
==> http-client.env.json
|
||||
{
|
||||
"$shared": {
|
||||
"baseUrl": "https://shop.example.com/api",
|
||||
"pageSize": "20"
|
||||
},
|
||||
"Shop dev": {
|
||||
"baseUrl": "http://localhost:8080/api"
|
||||
}
|
||||
}
|
||||
|
||||
==> http-client.private.env.json
|
||||
{
|
||||
"Shop dev": {
|
||||
"token": "dev-token-123",
|
||||
"adminPass": "hunter2"
|
||||
}
|
||||
}
|
||||
|
||||
==> warnings
|
||||
Skipped scripts on request "Shop API/Products/List products"
|
||||
Skipped scripts on request "Shop API/Auth/Token"
|
||||
Unsupported auth type "digest" on "Shop API/Auth/Digest thing"; add the header by hand
|
||||
Reference in new issue
Block a user