Skip to content

Commit 21d4313

Browse files
committed
Headers should override emulation defaults per request
1 parent 08c232a commit 21d4313

2 files changed

Lines changed: 25 additions & 15 deletions

File tree

ext/wreq_rb/src/client.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,21 @@ fn apply_request_options(
445445
mut req: wreq::RequestBuilder,
446446
opts: &RHash,
447447
) -> Result<wreq::RequestBuilder, magnus::Error> {
448+
if let Some(val) = hash_get_value(opts, "emulation")? {
449+
let ruby = unsafe { Ruby::get_unchecked() };
450+
if val.is_kind_of(ruby.class_false_class()) {
451+
// emulation: false — no per-request emulation override
452+
} else if val.is_kind_of(ruby.class_true_class()) {
453+
let opt = build_emulation_option(DEFAULT_EMULATION, opts)?;
454+
req = req.emulation(opt);
455+
} else {
456+
let name: String = TryConvert::try_convert(val)?;
457+
let emu = parse_emulation(&name)?;
458+
let opt = build_emulation_option(emu, opts)?;
459+
req = req.emulation(opt);
460+
}
461+
}
462+
448463
if let Some(hdr_hash) = hash_get_hash(opts, "headers")? {
449464
let hmap = hash_to_header_map(&hdr_hash)?;
450465
req = req.headers(hmap);
@@ -499,21 +514,6 @@ fn apply_request_options(
499514
req = req.proxy(proxy);
500515
}
501516

502-
if let Some(val) = hash_get_value(opts, "emulation")? {
503-
let ruby = unsafe { Ruby::get_unchecked() };
504-
if val.is_kind_of(ruby.class_false_class()) {
505-
// emulation: false — no per-request emulation override
506-
} else if val.is_kind_of(ruby.class_true_class()) {
507-
let opt = build_emulation_option(DEFAULT_EMULATION, opts)?;
508-
req = req.emulation(opt);
509-
} else {
510-
let name: String = TryConvert::try_convert(val)?;
511-
let emu = parse_emulation(&name)?;
512-
let opt = build_emulation_option(emu, opts)?;
513-
req = req.emulation(opt);
514-
}
515-
}
516-
517517
Ok(req)
518518
}
519519

test/request_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,14 @@ def test_headers_with_mixed_types
8989
assert_nil body["headers"]["X-Nil"]
9090
assert_equal "ok", body["headers"]["X-Str"]
9191
end
92+
93+
def test_headers_override_emulation_defaults
94+
custom_ua = "MyCustomAgent/1.0"
95+
resp = Wreq.get("https://httpbin.org/headers",
96+
emulation: "chrome_145",
97+
headers: { "User-Agent" => custom_ua })
98+
assert_equal 200, resp.status
99+
body = resp.json
100+
assert_equal custom_ua, body["headers"]["User-Agent"]
101+
end
92102
end

0 commit comments

Comments
 (0)