Skip to main content

zendriver_interception/
builder.rs

1//! [`InterceptBuilder`] — fluent rule + pattern registration.
2//!
3//! Two-phase API:
4//! - **Configure**: chain [`block`], [`block_hosts`], [`redirect`], [`respond`],
5//!   [`modify_request`], [`modify_response`] for declarative rules, plus
6//!   [`pattern`] / [`at_request`] / [`at_response`] / [`resource`] to control
7//!   which CDP `Fetch.RequestPattern` entries are sent on `Fetch.enable`.
8//! - **Activate**: [`start`](InterceptBuilder::start) spawns the actor task
9//!   (T6) with the registered rules + patterns, returning an
10//!   [`InterceptHandle`] for RAII teardown. Alternatively,
11//!   [`subscribe`](InterceptBuilder::subscribe) returns a
12//!   `Stream<Item = PausedRequest>` for the manual escape-hatch path —
13//!   callers drive Chrome's interception loop themselves.
14//!
15//! The `tab` field is a borrow of [`SessionHandle`] (not the full `Tab` from
16//! `zendriver` core) — this crate must not depend on `zendriver` (cycle).
17//! `Tab::intercept()` in `zendriver` constructs the builder via
18//! `InterceptBuilder::new(self.session())`.
19//!
20//! [`block`]: InterceptBuilder::block
21//! [`block_hosts`]: InterceptBuilder::block_hosts
22//! [`redirect`]: InterceptBuilder::redirect
23//! [`respond`]: InterceptBuilder::respond
24//! [`modify_request`]: InterceptBuilder::modify_request
25//! [`modify_response`]: InterceptBuilder::modify_response
26//! [`pattern`]: InterceptBuilder::pattern
27//! [`at_request`]: InterceptBuilder::at_request
28//! [`at_response`]: InterceptBuilder::at_response
29//! [`resource`]: InterceptBuilder::resource
30
31use std::sync::Arc;
32
33use futures::stream::{Stream, StreamExt};
34use serde_json::{Value, json};
35use tokio::sync::oneshot;
36use tokio_util::sync::CancellationToken;
37use tracing::warn;
38use zendriver_transport::SessionHandle;
39
40use crate::actor::{
41    InterceptHandle, RequestPausedEvent, build_request_info, build_response_info, run_actor,
42    serialize_pattern,
43};
44use crate::error::InterceptionError;
45use crate::host_matcher::HostMatcher;
46use crate::paused::PausedRequest;
47use crate::rule::Rule;
48use crate::types::{
49    RequestInfo, RequestOverrides, RequestStage, ResourceType, ResponseInfo, ResponseOverrides,
50};
51use crate::url_pattern::UrlPattern;
52
53/// A pending `Fetch.RequestPattern` entry to send on `Fetch.enable`.
54///
55/// CDP's [`Fetch.RequestPattern`] takes an optional `urlPattern`,
56/// `resourceType`, and `requestStage`. We mirror it 1:1 here. The builder
57/// accumulates these via [`InterceptBuilder::pattern`] / `at_request` /
58/// `at_response` / `resource`, mutating the last-pushed entry per chain — so
59/// `builder.pattern("*").at_response().resource(Image)` produces a single
60/// `RequestPattern` with all three fields set.
61///
62/// [`Fetch.RequestPattern`]: https://chromedevtools.github.io/devtools-protocol/tot/Fetch/#type-RequestPattern
63#[derive(Debug, Clone, Default)]
64pub struct RequestPattern {
65    /// URL pattern in CDP wildcard syntax. `None` means "match any URL"
66    /// (CDP default).
67    pub url_pattern: Option<String>,
68    /// Resource type filter (e.g. `Image`, `XHR`). `None` means "all types".
69    pub resource_type: Option<ResourceType>,
70    /// Lifecycle stage at which to pause. `None` means CDP's default
71    /// (`Request`).
72    pub request_stage: Option<RequestStage>,
73}
74
75/// Fluent builder for rule-based interception against a single tab session.
76///
77/// Construct via `Tab::intercept()` (gated `feature = "interception"`, wired
78/// in Task 7). Chain configuration methods to register rules and declare CDP
79/// `Fetch.enable` patterns, then call [`start`](Self::start) (Task 7) to
80/// activate the background actor or [`subscribe`](Self::subscribe) (Task 7)
81/// for the stream-driven escape hatch.
82///
83/// `'tab` ties the builder's lifetime to the tab's session — the borrow lasts
84/// only until `start()` / `subscribe()` consumes the builder.
85//
86// `Debug` works because `Rule` has a hand-written `Debug` impl that renders
87// the closure variant's body as `<closure>`. Inner `Vec<Rule>` derives via
88// that.
89#[derive(Debug)]
90pub struct InterceptBuilder<'tab> {
91    tab: &'tab SessionHandle,
92    patterns: Vec<RequestPattern>,
93    rules: Vec<Rule>,
94    /// Optional proxy/server credentials. When set, `Fetch.enable` is sent
95    /// with `handleAuthRequests: true` and the actor responds to each
96    /// `Fetch.authRequired` event with `Fetch.continueWithAuth` carrying
97    /// these credentials. See cdpdriver/zendriver#208.
98    auth: Option<(String, String)>,
99}
100
101impl<'tab> InterceptBuilder<'tab> {
102    /// Construct a fresh builder bound to `tab`'s session.
103    ///
104    /// `pub` so adapter crates (e.g. `zendriver` core's `Tab::intercept()`
105    /// shim) can construct it from a `&SessionHandle` without going through
106    /// a trait. End users go through `Tab::intercept()` rather than calling
107    /// this directly.
108    ///
109    /// ```no_run
110    /// # async fn ex(tab: &zendriver_transport::SessionHandle)
111    /// #   -> Result<(), zendriver_interception::InterceptionError> {
112    /// use zendriver_interception::InterceptBuilder;
113    ///
114    /// let _handle = InterceptBuilder::new(tab)
115    ///     .block("*/tracker.js")?
116    ///     .start();
117    /// # Ok(()) }
118    /// ```
119    #[must_use]
120    pub fn new(tab: &'tab SessionHandle) -> Self {
121        Self {
122            tab,
123            patterns: Vec::new(),
124            rules: Vec::new(),
125            auth: None,
126        }
127    }
128
129    /// Auto-respond to `Fetch.authRequired` challenges with the given
130    /// credentials.
131    ///
132    /// This is the proxy-auth (and HTTP basic-auth) path: `Fetch.enable` is
133    /// sent with `handleAuthRequests: true` and every `Fetch.authRequired`
134    /// event is answered with `Fetch.continueWithAuth { authChallengeResponse:
135    /// { response: "ProvideCredentials", username, password } }`.
136    ///
137    /// Compose with rules: an `InterceptBuilder` configured with `handle_auth`
138    /// and `block` / `redirect` / `respond` rules handles both paths from the
139    /// same actor. Combine with [`BrowserBuilder::proxy_auth`] in the
140    /// `zendriver` crate if you want the wiring installed automatically on
141    /// every tab.
142    ///
143    /// See cdpdriver/zendriver#208.
144    #[must_use]
145    pub fn handle_auth(mut self, user: impl Into<String>, pass: impl Into<String>) -> Self {
146        self.auth = Some((user.into(), pass.into()));
147        self
148    }
149
150    /// Push a new pattern entry with the given URL pattern string.
151    ///
152    /// Subsequent [`at_request`](Self::at_request) /
153    /// [`at_response`](Self::at_response) / [`resource`](Self::resource) calls
154    /// mutate this newest entry, so a chain like
155    /// `.pattern("*").at_response().resource(ResourceType::XHR)` produces one
156    /// `RequestPattern` with all three fields populated.
157    #[must_use]
158    pub fn pattern(mut self, pattern: impl Into<String>) -> Self {
159        self.patterns.push(RequestPattern {
160            url_pattern: Some(pattern.into()),
161            ..RequestPattern::default()
162        });
163        self
164    }
165
166    /// Pause matching requests at the `Request` stage on the most-recently
167    /// pushed pattern.
168    ///
169    /// If no pattern has been pushed yet, this creates an empty one (matches
170    /// every URL by CDP default) and sets the stage on it.
171    #[must_use]
172    pub fn at_request(mut self) -> Self {
173        self.ensure_pattern().request_stage = Some(RequestStage::Request);
174        self
175    }
176
177    /// Pause matching requests at the `Response` stage on the most-recently
178    /// pushed pattern.
179    #[must_use]
180    pub fn at_response(mut self) -> Self {
181        self.ensure_pattern().request_stage = Some(RequestStage::Response);
182        self
183    }
184
185    /// Restrict the most-recently pushed pattern to a single resource type.
186    #[must_use]
187    pub fn resource(mut self, kind: ResourceType) -> Self {
188        self.ensure_pattern().resource_type = Some(kind);
189        self
190    }
191
192    /// Register a [`Rule::Block`] for `pattern`.
193    ///
194    /// Compiles `pattern` eagerly; an invalid pattern fails the builder chain
195    /// with [`InterceptionError::InvalidPattern`] returned as `Err(Self)` via
196    /// the `Result` wrapper.
197    pub fn block(mut self, pattern: impl Into<String>) -> Result<Self, InterceptionError> {
198        self.rules.push(Rule::Block {
199            pattern: UrlPattern::new(pattern)?,
200        });
201        Ok(self)
202    }
203
204    /// Register a [`Rule::BlockHosts`] backed by `matcher`.
205    ///
206    /// Every request whose host is in `matcher` (exact, or a parent domain on
207    /// a dot boundary) is failed with `BlockedByClient`. Composes with other
208    /// rules in registration order. `zendriver` core's tracker-blocklist
209    /// wiring uses this; most callers reach it via `BrowserBuilder::block_trackers`.
210    #[must_use]
211    pub fn block_hosts(mut self, matcher: Arc<HostMatcher>) -> Self {
212        self.rules.push(Rule::BlockHosts { matcher });
213        self
214    }
215
216    /// Register a [`Rule::Redirect`] that rewrites `from` → `to`.
217    pub fn redirect(
218        mut self,
219        from: impl Into<String>,
220        to: impl Into<String>,
221    ) -> Result<Self, InterceptionError> {
222        self.rules.push(Rule::Redirect {
223            from: UrlPattern::new(from)?,
224            to: to.into(),
225        });
226        Ok(self)
227    }
228
229    /// Register a [`Rule::Respond`] serving a synthesized response.
230    pub fn respond(
231        mut self,
232        pattern: impl Into<String>,
233        status: u16,
234        headers: Vec<(String, String)>,
235        body: Vec<u8>,
236    ) -> Result<Self, InterceptionError> {
237        self.rules.push(Rule::Respond {
238            pattern: UrlPattern::new(pattern)?,
239            status,
240            headers,
241            body,
242        });
243        Ok(self)
244    }
245
246    /// Register a [`Rule::Modify`] driven by a user closure.
247    ///
248    /// The closure runs on the actor task per matching request — it must be
249    /// `Send + Sync` and `'static`. Wrap shared state in `Arc` if needed.
250    pub fn modify_request<F>(
251        mut self,
252        pattern: impl Into<String>,
253        modify: F,
254    ) -> Result<Self, InterceptionError>
255    where
256        F: Fn(&RequestInfo) -> RequestOverrides + Send + Sync + 'static,
257    {
258        self.rules.push(Rule::Modify {
259            pattern: UrlPattern::new(pattern)?,
260            modify: Arc::new(modify),
261        });
262        Ok(self)
263    }
264
265    /// Register a [`Rule::ModifyResponse`] driven by a user closure.
266    ///
267    /// The closure rewrites an upstream response's status/headers (keeping
268    /// Chrome's body) and only fires at the `Response` stage — pair this with
269    /// [`at_response`](Self::at_response) so Chrome actually pauses there.
270    /// Header overrides are *replacement*, not merge (CDP semantics): return
271    /// every header you want forwarded.
272    ///
273    /// Like [`modify_request`](Self::modify_request), the closure runs on the
274    /// actor task per matching response, so it must be `Send + Sync` and
275    /// `'static`. Wrap shared state in `Arc` if needed.
276    pub fn modify_response<F>(
277        mut self,
278        pattern: impl Into<String>,
279        modify: F,
280    ) -> Result<Self, InterceptionError>
281    where
282        F: Fn(&ResponseInfo) -> ResponseOverrides + Send + Sync + 'static,
283    {
284        self.rules.push(Rule::ModifyResponse {
285            pattern: UrlPattern::new(pattern)?,
286            modify: Arc::new(modify),
287        });
288        Ok(self)
289    }
290
291    /// Activate the rule-based interception loop.
292    ///
293    /// Spawns the background actor task with the registered rules and CDP
294    /// `RequestPattern` list, and returns an [`InterceptHandle`] whose
295    /// [`Drop`] (or explicit [`stop`](InterceptHandle::stop)) tears the
296    /// actor down.
297    ///
298    /// If no [`pattern`](Self::pattern) entries were added, a single
299    /// match-all (`"*"`) pattern is sent so Chrome actually pauses requests
300    /// — without it, `Fetch.enable` would attach to nothing and the rule
301    /// list would never fire.
302    #[must_use = "interception stops when the handle is dropped — bind the returned InterceptHandle to keep it alive"]
303    pub fn start(mut self) -> InterceptHandle {
304        if self.patterns.is_empty() && !self.rules.is_empty() {
305            // Rules need Chrome to actually pause requests, so a match-all
306            // pattern is required — without it `Fetch.enable` attaches to
307            // nothing and every rule is a silent no-op.
308            //
309            // But when there are NO rules (the proxy/HTTP-auth-ONLY path),
310            // injecting "*" makes Chrome pause EVERY request and round-trip it
311            // through this single actor over one CDP socket — which distorts the
312            // timing/cadence of the page's own XHRs. Bot sensors (reese84 /
313            // Incapsula) read exactly that. So leave patterns empty here:
314            // `Fetch.enable { patterns: [], handleAuthRequests: true }` still
315            // surfaces `Fetch.authRequired` for 407 challenges (answered via
316            // continueWithAuth) while pausing zero requests.
317            self.patterns.push(RequestPattern {
318                url_pattern: Some("*".into()),
319                ..RequestPattern::default()
320            });
321        }
322        let cancel = CancellationToken::new();
323        let (done_tx, done_rx) = oneshot::channel();
324        let actor_session = self.tab.clone();
325        let actor_cancel = cancel.clone();
326        let actor_rules = self.rules;
327        let actor_patterns = self.patterns;
328        let actor_auth = self.auth;
329        tokio::spawn(async move {
330            run_actor(
331                actor_session,
332                actor_rules,
333                actor_patterns,
334                actor_auth,
335                actor_cancel,
336                done_tx,
337            )
338            .await;
339        });
340        InterceptHandle::new(cancel, done_rx)
341    }
342
343    /// Manual escape-hatch: subscribe to raw [`PausedRequest`] events.
344    ///
345    /// Enables `Fetch` interception with the declared patterns (defaulting
346    /// to a single match-all `"*"` pattern when none were added) and returns
347    /// a [`Stream`] that yields one [`PausedRequest`] per `Fetch.requestPaused`
348    /// CDP event. Callers must dispatch one of `PausedRequest`'s terminal
349    /// methods (`continue_` / `abort` / `respond` / `modify_and_continue`)
350    /// to release each pause — Chrome holds the request open otherwise.
351    ///
352    /// Rules registered via `block` / `redirect` / `respond` / `modify_request`
353    /// are ignored on this path: stream consumers drive every paused request
354    /// themselves. Use [`start`](Self::start) when you want the actor to
355    /// apply rules automatically.
356    ///
357    /// The returned stream owns the underlying CDP subscription. Dropping
358    /// the stream tears the subscription down — Chrome's interception stays
359    /// active until the session is closed, but no further pauses surface to
360    /// the caller.
361    #[must_use = "the returned stream is the only handle on the subscription"]
362    pub fn subscribe(mut self) -> impl Stream<Item = PausedRequest> + Send + use<> {
363        if self.patterns.is_empty() {
364            self.patterns.push(RequestPattern {
365                url_pattern: Some("*".into()),
366                ..RequestPattern::default()
367            });
368        }
369        // Same ordering as the actor: subscribe BEFORE the (fire-and-forget)
370        // enable so we don't drop events Chrome emits between the enable
371        // round-trip and the subscription registration.
372        let raw = self.tab.subscribe::<Value>("Fetch.requestPaused");
373        let session = self.tab.clone();
374        let enable_session = session.clone();
375        let enable_patterns: Vec<Value> = self.patterns.iter().map(serialize_pattern).collect();
376        tokio::spawn(async move {
377            if let Err(e) = enable_session
378                .call(
379                    "Fetch.enable",
380                    json!({
381                        "patterns": enable_patterns,
382                        "handleAuthRequests": false,
383                    }),
384                )
385                .await
386            {
387                warn!(error = %e, "interception: Fetch.enable failed; subscribe() stream will be empty");
388            }
389        });
390        raw.filter_map(move |ev_value| {
391            let session = session.clone();
392            async move {
393                let ev: RequestPausedEvent = match serde_json::from_value(ev_value) {
394                    Ok(ev) => ev,
395                    Err(e) => {
396                        warn!(error = %e, "interception: skipping malformed Fetch.requestPaused event");
397                        return None;
398                    }
399                };
400                let info = build_request_info(&ev);
401                let response = build_response_info(&ev);
402                Some(PausedRequest::new(ev.request_id, info, response, session))
403            }
404        })
405    }
406
407    /// Lazily push an empty pattern if none exists, so the stage/resource
408    /// setters always have a target. Mirrors CDP's "missing fields default to
409    /// match-all" semantics.
410    fn ensure_pattern(&mut self) -> &mut RequestPattern {
411        if self.patterns.is_empty() {
412            self.patterns.push(RequestPattern::default());
413        }
414        self.patterns
415            .last_mut()
416            .expect("ensure_pattern pushed if empty")
417    }
418
419    /// Test-only accessor: number of registered rules. Used by the Task 5
420    /// builder test (and future actor tests) without exposing the rule list
421    /// as public API.
422    #[cfg(test)]
423    pub(crate) fn rules_count(&self) -> usize {
424        self.rules.len()
425    }
426}
427
428#[cfg(test)]
429#[allow(clippy::panic, clippy::unwrap_used)]
430mod tests {
431    use super::*;
432    use std::time::Duration;
433    use zendriver_transport::testing::MockConnection;
434
435    /// Register three rules (block + redirect + respond) on a fresh builder
436    /// and assert the rule list grew to length 3. Verifies the chain wiring
437    /// without touching the actor (Task 6) or CDP dispatch (Task 7).
438    #[tokio::test]
439    async fn three_rules_register_and_count() {
440        let (_mock, conn) = MockConnection::pair();
441        let sess = SessionHandle::new(conn.clone(), "S1");
442
443        let builder = InterceptBuilder::new(&sess)
444            .block("*/ads/*")
445            .unwrap()
446            .redirect("*/old/*", "https://example.com/new/")
447            .unwrap()
448            .respond(
449                "*/api/health",
450                200,
451                vec![("content-type".into(), "application/json".into())],
452                br#"{"ok":true}"#.to_vec(),
453            )
454            .unwrap();
455
456        assert_eq!(builder.rules_count(), 3);
457        conn.shutdown();
458    }
459
460    /// End-to-end on the rule-driven `start()` path: register a Block rule,
461    /// spawn the actor via `start()`, observe `Fetch.enable`, emit a matching
462    /// `Fetch.requestPaused`, and assert `Fetch.failRequest` is dispatched.
463    ///
464    /// This is the actor test from T6 reframed through the `start()` entry
465    /// point — proves the builder properly forwards rules + patterns to
466    /// `run_actor` (and that `start()` actually spawns the task).
467    #[tokio::test]
468    async fn start_spawns_actor_with_rules() {
469        let (mut mock, conn) = MockConnection::pair();
470        let sess = SessionHandle::new(conn.clone(), "S1");
471
472        let handle = InterceptBuilder::new(&sess)
473            .block("*/blocked/*")
474            .unwrap()
475            .pattern("*")
476            .start();
477
478        // The actor's `Fetch.enable` side-task fires fire-and-forget; wait
479        // for it to land so the `Fetch.requestPaused` subscription is in
480        // place before we emit an event.
481        let enable_id =
482            tokio::time::timeout(Duration::from_secs(2), mock.expect_cmd("Fetch.enable"))
483                .await
484                .expect("actor did not send Fetch.enable within 2s");
485        let enable_params = mock.last_sent()["params"].clone();
486        assert_eq!(enable_params["handleAuthRequests"], false);
487        assert_eq!(enable_params["patterns"][0]["urlPattern"], "*");
488        mock.reply(enable_id, json!({})).await;
489
490        // Emit a paused-event whose URL matches the Block rule.
491        mock.emit_event_for_session(
492            "Fetch.requestPaused",
493            json!({
494                "requestId": "REQ-1",
495                "request": {
496                    "url": "https://example.test/blocked/banner.png",
497                    "method": "GET",
498                    "headers": {},
499                },
500                "resourceType": "Image",
501            }),
502            "S1",
503        )
504        .await;
505
506        // Actor should dispatch Fetch.failRequest with BlockedByClient.
507        let fail_id =
508            tokio::time::timeout(Duration::from_secs(2), mock.expect_cmd("Fetch.failRequest"))
509                .await
510                .expect("actor did not send Fetch.failRequest within 2s");
511        let fail_params = mock.last_sent()["params"].clone();
512        assert_eq!(fail_params["requestId"], "REQ-1");
513        assert_eq!(fail_params["errorReason"], "BlockedByClient");
514        mock.reply(fail_id, json!({})).await;
515
516        // Teardown via the handle: stop() cancels + awaits the oneshot the
517        // actor signals after Fetch.disable lands.
518        let stop_fut = tokio::spawn(handle.stop());
519        let disable_id =
520            tokio::time::timeout(Duration::from_secs(2), mock.expect_cmd("Fetch.disable"))
521                .await
522                .expect("actor did not send Fetch.disable on stop()");
523        mock.reply(disable_id, json!({})).await;
524        stop_fut
525            .await
526            .expect("stop() task panicked")
527            .expect("stop() returned Err");
528        conn.shutdown();
529    }
530
531    /// `start()` injects a match-all `"*"` pattern when the caller did not
532    /// add any via [`pattern`](InterceptBuilder::pattern) — otherwise
533    /// `Fetch.enable` would arrive with an empty patterns array and Chrome
534    /// would silently pause nothing.
535    #[tokio::test]
536    async fn start_defaults_to_match_all_pattern_when_none_registered() {
537        let (mut mock, conn) = MockConnection::pair();
538        let sess = SessionHandle::new(conn.clone(), "S1");
539
540        let handle = InterceptBuilder::new(&sess)
541            .block("*/blocked/*")
542            .unwrap()
543            .start();
544
545        let enable_id =
546            tokio::time::timeout(Duration::from_secs(2), mock.expect_cmd("Fetch.enable"))
547                .await
548                .expect("actor did not send Fetch.enable within 2s");
549        let patterns = mock.last_sent()["params"]["patterns"].clone();
550        let arr = patterns.as_array().expect("patterns must be a JSON array");
551        assert_eq!(arr.len(), 1);
552        assert_eq!(arr[0]["urlPattern"], "*");
553        mock.reply(enable_id, json!({})).await;
554
555        // Drop the handle to tear down; we don't need to observe the disable.
556        drop(handle);
557        conn.shutdown();
558    }
559
560    /// On the `subscribe()` path: each `Fetch.requestPaused` event becomes
561    /// a `PausedRequest` yielded from the stream, with the request payload
562    /// decoded into [`RequestInfo`].
563    #[tokio::test]
564    async fn subscribe_yields_paused_request_per_event() {
565        let (mut mock, conn) = MockConnection::pair();
566        let sess = SessionHandle::new(conn.clone(), "S1");
567
568        let mut stream = Box::pin(InterceptBuilder::new(&sess).subscribe());
569
570        // Wait for the side-task's Fetch.enable to land so the subscription
571        // is in place before we emit.
572        let enable_id =
573            tokio::time::timeout(Duration::from_secs(2), mock.expect_cmd("Fetch.enable"))
574                .await
575                .expect("subscribe() did not send Fetch.enable within 2s");
576        mock.reply(enable_id, json!({})).await;
577
578        mock.emit_event_for_session(
579            "Fetch.requestPaused",
580            json!({
581                "requestId": "REQ-1",
582                "request": {
583                    "url": "https://example.test/widget.json",
584                    "method": "GET",
585                    "headers": {"accept": "application/json"},
586                },
587                "resourceType": "XHR",
588            }),
589            "S1",
590        )
591        .await;
592
593        let paused = tokio::time::timeout(Duration::from_secs(2), stream.next())
594            .await
595            .expect("subscribe() stream did not yield within 2s")
596            .expect("subscribe() stream closed before yielding");
597        assert_eq!(paused.request_id, "REQ-1");
598        assert_eq!(paused.request.url, "https://example.test/widget.json");
599        assert_eq!(paused.request.method, "GET");
600        assert_eq!(
601            paused
602                .request
603                .headers
604                .iter()
605                .find(|(k, _)| k == "accept")
606                .map(|(_, v)| v.as_str()),
607            Some("application/json"),
608        );
609        assert!(
610            paused.response.is_none(),
611            "request-stage event has no response"
612        );
613
614        drop(stream);
615        conn.shutdown();
616    }
617}