Added custom auth support
windows / build (push) Successful in 5m0s

This commit is contained in:
Torbjörn Molin committed 2026-11-09 12:56:54 +01:00
1 parent ae055be9cc
commit 1e7f8678eb
9 files changed
+377 -97

No files matched your search

+67 -8
View File
@@ -8,7 +8,7 @@ use reqlite_core::collection::{Collection, Node};
use reqlite_core::env::EnvFile;
use reqlite_core::http::{BodyStore, Timings};
use reqlite_core::folder::{AuthMode, FolderAuth};
use reqlite_core::folder::{AuthHeader, AuthMode, FolderAuth};
use super::*;
@@ -1180,6 +1180,69 @@ fn requests_inherit_folder_auth_unless_they_opt_out() {
assert_eq!(sent_auth(&mut s, own).as_deref(), Some("Bearer root"));
}
fn sent_header(s: &mut AppState, id: TabId, name: &str) -> Option<String> {
let fx = update(s, Action::Send(id));
let Some(Effect::SendRequest { request, .. }) = fx.first() else {
panic!("not sent: {:?}", s.tab(id).unwrap().error)
};
let v = request
.headers
.iter()
.find(|(n, _)| n.eq_ignore_ascii_case(name))
.map(|(_, v)| v.clone());
update(s, Action::Cancel(id));
v
}
#[test]
fn folder_and_request_auth_can_use_a_custom_header() {
let mut s = state();
let root = with_collection(
&mut s,
&[("a.http", "### Plain\nGET http://x/a\n\n### Own\nGET http://x/b\n")],
);
let open = |s: &mut AppState, index| {
update(
s,
Action::OpenRequest {
file: root.join("a.http"),
index,
},
);
active(s)
};
let plain = open(&mut s, 0);
let own = open(&mut s, 1);
let key = |v: &str| AuthHeader::custom("X-API-Key", v);
let fx = set_folder_auth(&mut s, root.clone(), FolderAuth::Header(key("k-root")));
assert_eq!(
writes(&fx),
vec![(
root.join(".reqlite/folder.json"),
"{\n \"auth\": {\n \"header\": \"X-API-Key\",\n \"value\": \"k-root\"\n }\n}\n".into()
)]
);
assert_eq!(sent_header(&mut s, plain, "x-api-key").as_deref(), Some("k-root"));
assert_eq!(sent_auth(&mut s, plain), None);
// A request's own custom header replaces the folder's.
update(&mut s, Action::AuthEdited(own, AuthMode::Own(key("k-own"))));
assert!(s
.tab(own)
.unwrap()
.source
.contains("# @auth-header X-API-Key\nGET http://x/b\nX-API-Key: k-own\n"));
assert_eq!(sent_header(&mut s, own, "x-api-key").as_deref(), Some("k-own"));
// Switching to Bearer drops the custom header and the directive.
update(&mut s, Action::AuthEdited(own, AuthMode::Own("Bearer t".into())));
let src = &s.tab(own).unwrap().source;
assert!(!src.contains("X-API-Key") && !src.contains("@auth-header"), "{src}");
assert_eq!(sent_auth(&mut s, own).as_deref(), Some("Bearer t"));
assert_eq!(sent_header(&mut s, own, "x-api-key"), None);
}
#[test]
fn folder_settings_files_are_reloaded_when_they_change() {
let mut s = state();
@@ -1213,7 +1276,7 @@ fn consolidate(s: &mut AppState, dir: PathBuf, target: Option<&str>) -> Consolid
s,
Action::ConsolidateAuth {
dir,
target: target.map(str::to_string),
target: target.map(Into::into),
},
);
match &s.dialog {
@@ -1229,7 +1292,7 @@ fn consolidating_adopts_the_most_common_auth_as_the_collection_default() {
let mut s = state();
let root = with_collection(&mut s, &[("a.http", MIXED)]);
let plan = consolidate(&mut s, root.clone(), None);
assert_eq!(plan.target.as_deref(), Some("Bearer {{token}}"));
assert_eq!(plan.target, Some("Bearer {{token}}".into()));
assert!(plan.set_folder);
assert_eq!(
plan.choices,
@@ -1295,11 +1358,7 @@ fn consolidating_keeps_subfolder_overrides_and_skips_unsaved_tabs() {
update(&mut s, Action::UrlEdited(id, "http://x/edited".into()));
let plan = consolidate(&mut s, root.clone(), Some("Basic ignored"));
assert_eq!(
plan.target.as_deref(),
Some("Bearer root"),
"the folder's own auth wins"
);
assert_eq!(plan.target, Some("Bearer root".into()), "the folder's own auth wins");
assert!(!plan.set_folder && plan.choices.is_empty());
let own: Vec<(&str, bool)> = plan
.own