thirtyfour 0.37.0

Thirtyfour is a Selenium / WebDriver library for Rust, for automated website UI testing. Tested on Chrome and Firefox, but any webdriver-capable browser should work.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
//! `network.*` — observe, intercept, and modify HTTP traffic.
//!
//! This module covers four overlapping use-cases:
//!
//! 1. **Observation** — subscribe to [`BeforeRequestSent`][brs],
//!    [`ResponseStarted`][rs], [`ResponseCompleted`][rc], or
//!    [`FetchError`][fe] to watch every request the page makes
//!    (including subresources) without altering it.
//! 2. **Interception** — register an [`AddIntercept`][ai] to pause
//!    requests in selected phases, then continue, fail, or synthesize
//!    a response using [`ContinueRequest`][creq] / [`FailRequest`][fr]
//!    / [`ProvideResponse`][pr] / [`ContinueResponse`][cresp] /
//!    [`ContinueWithAuth`][cwa].
//! 3. **Body capture** — register an [`AddDataCollector`][adc] to
//!    retain request and/or response bodies, then read them back via
//!    [`GetData`][gd].
//! 4. **Header injection / cache control** — apply session-level
//!    [`SetExtraHeaders`][seh] and [`SetCacheBehavior`][scb] without
//!    touching individual requests.
//!
//! See the [W3C `network` module specification][spec] for the canonical
//! definitions, including [URL pattern matching][spec-urlpattern] (used by
//! intercepts) and the request/response data shapes.
//!
//! [spec]: https://w3c.github.io/webdriver-bidi/#module-network
//! [spec-urlpattern]: https://w3c.github.io/webdriver-bidi/#type-network-UrlPattern
//! [brs]: crate::bidi::modules::network::events::BeforeRequestSent
//! [rs]: crate::bidi::modules::network::events::ResponseStarted
//! [rc]: crate::bidi::modules::network::events::ResponseCompleted
//! [fe]: crate::bidi::modules::network::events::FetchError
//! [ai]: crate::bidi::modules::network::AddIntercept
//! [creq]: crate::bidi::modules::network::ContinueRequest
//! [fr]: crate::bidi::modules::network::FailRequest
//! [pr]: crate::bidi::modules::network::ProvideResponse
//! [cresp]: crate::bidi::modules::network::ContinueResponse
//! [cwa]: crate::bidi::modules::network::ContinueWithAuth
//! [adc]: crate::bidi::modules::network::AddDataCollector
//! [gd]: crate::bidi::modules::network::GetData
//! [seh]: crate::bidi::modules::network::SetExtraHeaders
//! [scb]: crate::bidi::modules::network::SetCacheBehavior

use serde::{Deserialize, Serialize};

use crate::bidi::BiDi;
use crate::bidi::command::{BidiCommand, BidiEvent, Empty};
use crate::bidi::error::BidiError;
use crate::bidi::ids::{BrowsingContextId, CollectorId, InterceptId, RequestId, UserContextId};
use crate::common::protocol::string_enum;

string_enum! {
    /// Phase that an [`AddIntercept`] should pause on.
    ///
    /// Mirrors the spec's [`network.InterceptPhase`][spec] enumeration.
    ///
    /// [spec]: https://w3c.github.io/webdriver-bidi/#type-network-InterceptPhase
    pub enum InterceptPhase {
        /// Pause the request before it leaves the network stack. The
        /// matching [`events::BeforeRequestSent`] event arrives with
        /// `is_blocked = true`. Resume with [`ContinueRequest`],
        /// abandon with [`FailRequest`], or fake a response with
        /// [`ProvideResponse`].
        BeforeRequestSent = "beforeRequestSent",
        /// Pause the response when its headers arrive but before it is
        /// delivered to the page. The matching
        /// [`events::ResponseStarted`] event arrives with
        /// `is_blocked = true`. Resume with [`ContinueResponse`].
        ResponseStarted = "responseStarted",
        /// Pause on HTTP 401 / 407 challenges. The matching
        /// [`events::AuthRequired`] event arrives with
        /// `is_blocked = true`. Resume with [`ContinueWithAuth`].
        AuthRequired = "authRequired",
    }
}

string_enum! {
    /// HTTP cache mode for [`SetCacheBehavior`]. Mirrors the spec's
    /// `cacheBehavior` enumeration on
    /// [`network.setCacheBehavior`][spec].
    ///
    /// [spec]: https://w3c.github.io/webdriver-bidi/#command-network-setCacheBehavior
    pub enum CacheBehavior {
        /// Honour cache headers (the browser's default behaviour).
        Default = "default",
        /// Bypass any in-browser cache.
        Bypass = "bypass",
    }
}

string_enum! {
    /// Body kind for [`AddDataCollector::data_types`] and
    /// [`GetData::data_type`].
    ///
    /// Mirrors the spec's [`network.DataType`][spec] type.
    ///
    /// [spec]: https://w3c.github.io/webdriver-bidi/#type-network-DataType
    pub enum DataType {
        /// Request bodies.
        Request = "request",
        /// Response bodies.
        Response = "response",
    }
}

string_enum! {
    /// Storage backing for an [`AddDataCollector`].
    ///
    /// Currently only `blob` is defined; the enum exists for forward
    /// compatibility (the spec mentions a future `stream` collector
    /// type).
    pub enum CollectorType {
        /// In-memory blob.
        Blob = "blob",
    }
}

/// [`network.addIntercept`][spec] — register an interception that pauses
/// requests in the named phases.
///
/// `phases` selects which lifecycle stages will pause. `url_patterns`
/// scopes the intercept to URLs matching any of the supplied
/// [`network.UrlPattern`s][pattern] (string-or-object form, passed through
/// as JSON); empty/None matches every URL. `contexts` scopes to specific
/// top-level browsing contexts.
///
/// Prefer the convenience [`NetworkModule::add_intercept`] facade — it
/// returns an [`InterceptGuard`] that auto-removes the intercept on drop.
///
/// [spec]: https://w3c.github.io/webdriver-bidi/#command-network-addIntercept
/// [pattern]: https://w3c.github.io/webdriver-bidi/#type-network-UrlPattern
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct AddIntercept {
    /// Phase(s) to intercept.
    pub phases: Vec<InterceptPhase>,
    /// Restrict to specific top-level browsing contexts. Empty = any.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub contexts: Vec<BrowsingContextId>,
    /// URL match patterns. Each entry is a [`network.UrlPattern`][pattern]
    /// JSON value (string variant: `{"type":"string","pattern":"…"}`,
    /// object variant: `{"type":"pattern","hostname":"…",…}`).
    ///
    /// [pattern]: https://w3c.github.io/webdriver-bidi/#type-network-UrlPattern
    #[serde(skip_serializing_if = "Option::is_none")]
    pub url_patterns: Option<Vec<serde_json::Value>>,
}

impl BidiCommand for AddIntercept {
    const METHOD: &'static str = "network.addIntercept";
    type Returns = AddInterceptResult;
}

/// Response for [`AddIntercept`].
#[derive(Debug, Clone, Deserialize)]
pub struct AddInterceptResult {
    /// Server-assigned intercept id. Pass to [`RemoveIntercept`] to drop
    /// the interception.
    pub intercept: InterceptId,
}

/// [`network.removeIntercept`][spec] — drop a previously-registered
/// intercept.
///
/// Already-paused requests are unaffected — only future requests stop
/// matching. Use [`InterceptGuard`] to manage this automatically.
///
/// [spec]: https://w3c.github.io/webdriver-bidi/#command-network-removeIntercept
#[derive(Debug, Clone, Serialize)]
pub struct RemoveIntercept {
    /// Intercept id returned by [`AddIntercept`].
    pub intercept: InterceptId,
}

impl BidiCommand for RemoveIntercept {
    const METHOD: &'static str = "network.removeIntercept";
    type Returns = Empty;
}

/// [`network.continueRequest`][spec] — release a paused request,
/// optionally with modifications.
///
/// Only valid for requests paused in the
/// [`InterceptPhase::BeforeRequestSent`] phase. Each optional field
/// overrides the corresponding part of the original request. Bytes-typed
/// fields use the spec's [`network.BytesValue`][bytes]
/// (`{"type":"string","value":…}` or `{"type":"base64","value":…}`).
///
/// [spec]: https://w3c.github.io/webdriver-bidi/#command-network-continueRequest
/// [bytes]: https://w3c.github.io/webdriver-bidi/#type-network-BytesValue
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ContinueRequest {
    /// Request to continue.
    pub request: RequestId,
    /// Override request body (BiDi [`network.BytesValue`][bytes]).
    ///
    /// [bytes]: https://w3c.github.io/webdriver-bidi/#type-network-BytesValue
    #[serde(skip_serializing_if = "Option::is_none")]
    pub body: Option<serde_json::Value>,
    /// Override cookies — array of
    /// [`network.CookieHeader`][cookie] JSON values.
    ///
    /// [cookie]: https://w3c.github.io/webdriver-bidi/#type-network-CookieHeader
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cookies: Option<Vec<serde_json::Value>>,
    /// Override headers — array of
    /// [`network.Header`][header] JSON values.
    ///
    /// [header]: https://w3c.github.io/webdriver-bidi/#type-network-Header
    #[serde(skip_serializing_if = "Option::is_none")]
    pub headers: Option<Vec<serde_json::Value>>,
    /// Override the HTTP method (e.g. `"POST"`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub method: Option<String>,
    /// Override the request URL (must be an absolute URL).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub url: Option<String>,
}

impl BidiCommand for ContinueRequest {
    const METHOD: &'static str = "network.continueRequest";
    type Returns = Empty;
}

/// [`network.continueResponse`][spec] — release a paused response,
/// optionally with modifications.
///
/// Only valid for requests paused in
/// [`InterceptPhase::ResponseStarted`]. The response body is delivered
/// from the network as-is; only the status, headers, cookies, and
/// (for an `authRequired` follow-up) credentials can be overridden.
///
/// [spec]: https://w3c.github.io/webdriver-bidi/#command-network-continueResponse
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ContinueResponse {
    /// Request whose response is being continued.
    pub request: RequestId,
    /// Override cookies — array of
    /// [`network.SetCookieHeader`][cookie] JSON values.
    ///
    /// [cookie]: https://w3c.github.io/webdriver-bidi/#type-network-SetCookieHeader
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cookies: Option<Vec<serde_json::Value>>,
    /// Override credentials (BiDi [`network.AuthCredentials`][creds]).
    ///
    /// [creds]: https://w3c.github.io/webdriver-bidi/#type-network-AuthCredentials
    #[serde(skip_serializing_if = "Option::is_none")]
    pub credentials: Option<serde_json::Value>,
    /// Override headers — array of [`network.Header`][header] JSON values.
    ///
    /// [header]: https://w3c.github.io/webdriver-bidi/#type-network-Header
    #[serde(skip_serializing_if = "Option::is_none")]
    pub headers: Option<Vec<serde_json::Value>>,
    /// Override the status reason phrase (e.g. `"OK"`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reason_phrase: Option<String>,
    /// Override the status code (e.g. `200`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status_code: Option<u16>,
}

impl BidiCommand for ContinueResponse {
    const METHOD: &'static str = "network.continueResponse";
    type Returns = Empty;
}

/// [`network.continueWithAuth`][spec] — answer an `authRequired` intercept.
///
/// Use the [`AuthAction`] enum to pick provide-credentials, cancel the
/// challenge, or fall back to the browser's default behaviour (which is
/// to prompt the user).
///
/// [spec]: https://w3c.github.io/webdriver-bidi/#command-network-continueWithAuth
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ContinueWithAuth {
    /// Request to continue.
    pub request: RequestId,
    /// Action and credentials.
    #[serde(flatten)]
    pub action: AuthAction,
}

/// Action variant for [`ContinueWithAuth`].
///
/// Mirrors the spec's `network.ContinueWithAuthCredentials` /
/// `network.ContinueWithAuthNoCredentials` union.
#[derive(Debug, Clone, Serialize)]
#[serde(tag = "action", rename_all = "lowercase")]
pub enum AuthAction {
    /// Provide credentials to satisfy the challenge.
    ProvideCredentials {
        /// Credentials object.
        credentials: AuthCredentials,
    },
    /// Cancel the auth request — the user-agent treats this as if the
    /// user dismissed the dialog.
    Cancel,
    /// Default — fall back to the browser's normal behaviour
    /// (typically prompts the user).
    Default,
}

/// Basic-auth credentials. Mirrors
/// [`network.AuthCredentials`][spec].
///
/// [spec]: https://w3c.github.io/webdriver-bidi/#type-network-AuthCredentials
#[derive(Debug, Clone, Serialize)]
pub struct AuthCredentials {
    /// Auth scheme. The spec only defines `"password"`.
    pub r#type: String,
    /// Username.
    pub username: String,
    /// Password.
    pub password: String,
}

impl AuthCredentials {
    /// Construct a basic-auth ("password") credentials object.
    pub fn basic(username: impl Into<String>, password: impl Into<String>) -> Self {
        Self {
            r#type: "password".to_string(),
            username: username.into(),
            password: password.into(),
        }
    }
}

impl BidiCommand for ContinueWithAuth {
    const METHOD: &'static str = "network.continueWithAuth";
    type Returns = Empty;
}

/// [`network.failRequest`][spec] — fail an intercepted request.
///
/// Only valid in [`InterceptPhase::BeforeRequestSent`]. The page sees the
/// request as a network error.
///
/// [spec]: https://w3c.github.io/webdriver-bidi/#command-network-failRequest
#[derive(Debug, Clone, Serialize)]
pub struct FailRequest {
    /// Request to fail.
    pub request: RequestId,
}

impl BidiCommand for FailRequest {
    const METHOD: &'static str = "network.failRequest";
    type Returns = Empty;
}

/// [`network.provideResponse`][spec] — supply a synthetic response for
/// an intercepted request (the actual network request is skipped).
///
/// Most useful in [`InterceptPhase::BeforeRequestSent`] for stubbing
/// fetches in tests. The response progresses through the rest of the
/// lifecycle as if it had come from the wire.
///
/// [spec]: https://w3c.github.io/webdriver-bidi/#command-network-provideResponse
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ProvideResponse {
    /// Request to respond to.
    pub request: RequestId,
    /// Response body — a [`network.BytesValue`][bytes].
    ///
    /// [bytes]: https://w3c.github.io/webdriver-bidi/#type-network-BytesValue
    #[serde(skip_serializing_if = "Option::is_none")]
    pub body: Option<serde_json::Value>,
    /// `Set-Cookie` headers — array of
    /// [`network.SetCookieHeader`][cookie] JSON values.
    ///
    /// [cookie]: https://w3c.github.io/webdriver-bidi/#type-network-SetCookieHeader
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cookies: Option<Vec<serde_json::Value>>,
    /// Response headers — array of [`network.Header`][header] JSON values.
    ///
    /// [header]: https://w3c.github.io/webdriver-bidi/#type-network-Header
    #[serde(skip_serializing_if = "Option::is_none")]
    pub headers: Option<Vec<serde_json::Value>>,
    /// Reason phrase (e.g. `"OK"`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reason_phrase: Option<String>,
    /// Status code (defaults to `200` if omitted).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status_code: Option<u16>,
}

impl BidiCommand for ProvideResponse {
    const METHOD: &'static str = "network.provideResponse";
    type Returns = Empty;
}

/// [`network.setCacheBehavior`][spec] — switch HTTP-cache behaviour
/// globally or for specific contexts.
///
/// `Bypass` disables the in-browser cache for matching requests
/// (analogous to Chrome's "Disable cache" checkbox).
///
/// [spec]: https://w3c.github.io/webdriver-bidi/#command-network-setCacheBehavior
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SetCacheBehavior {
    /// New cache mode.
    pub cache_behavior: CacheBehavior,
    /// Restrict to specific browsing contexts. Empty = applies as the
    /// new default.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub contexts: Vec<BrowsingContextId>,
    /// Restrict to specific user contexts. Empty = applies as the new
    /// default.
    #[serde(rename = "userContexts", skip_serializing_if = "Vec::is_empty")]
    pub user_contexts: Vec<UserContextId>,
}

impl BidiCommand for SetCacheBehavior {
    const METHOD: &'static str = "network.setCacheBehavior";
    type Returns = Empty;
}

/// [`network.addDataCollector`][spec] — start retaining request and/or
/// response bodies for later retrieval via [`GetData`].
///
/// `max_encoded_data_size` is a per-item byte limit; bodies that exceed
/// it are still observed but not retained. Scope is mutually exclusive:
/// supply at most one of `contexts` / `user_contexts`. Either matches a
/// set of top-level traversables; an unscoped collector retains data from
/// every navigation.
///
/// [spec]: https://w3c.github.io/webdriver-bidi/#command-network-addDataCollector
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct AddDataCollector {
    /// Body kinds to retain.
    pub data_types: Vec<DataType>,
    /// Per-item maximum encoded size in bytes. Items larger than this
    /// are not retained.
    pub max_encoded_data_size: u64,
    /// Backing storage type (defaults to [`CollectorType::Blob`]).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub collector_type: Option<CollectorType>,
    /// Restrict to specific top-level browsing contexts. Mutually
    /// exclusive with [`user_contexts`][Self::user_contexts].
    #[serde(skip_serializing_if = "Option::is_none")]
    pub contexts: Option<Vec<BrowsingContextId>>,
    /// Restrict to specific user contexts. Mutually exclusive with
    /// [`contexts`][Self::contexts].
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_contexts: Option<Vec<UserContextId>>,
}

impl BidiCommand for AddDataCollector {
    const METHOD: &'static str = "network.addDataCollector";
    type Returns = AddDataCollectorResult;
}

/// Response for [`AddDataCollector`].
#[derive(Debug, Clone, Deserialize)]
pub struct AddDataCollectorResult {
    /// Server-assigned collector id. Pass to [`GetData`],
    /// [`DisownData`], or [`RemoveDataCollector`].
    pub collector: CollectorId,
}

/// [`network.removeDataCollector`][spec] — drop a data collector and
/// release every request's retained data for that collector.
///
/// [spec]: https://w3c.github.io/webdriver-bidi/#command-network-removeDataCollector
#[derive(Debug, Clone, Serialize)]
pub struct RemoveDataCollector {
    /// Collector id to remove.
    pub collector: CollectorId,
}

impl BidiCommand for RemoveDataCollector {
    const METHOD: &'static str = "network.removeDataCollector";
    type Returns = Empty;
}

/// [`network.getData`][spec] — fetch retained body data for a request.
///
/// `collector` is optional: if omitted, the driver returns the data from
/// any collector that retained it. `disown: true` releases the data
/// after returning it (saves memory) — but requires `collector` to be
/// supplied so the driver knows which collector to release from.
///
/// Errors:
/// - `no such network collector` — `collector` references an unknown id.
/// - `no such network data` — no collector retained body data for the
///   `(request, data_type)` pair.
/// - `unavailable network data` — data was retained but is no longer
///   available (typically because it exceeded `max_encoded_data_size`).
///
/// [spec]: https://w3c.github.io/webdriver-bidi/#command-network-getData
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GetData {
    /// Body kind to retrieve.
    pub data_type: DataType,
    /// Request to look up.
    pub request: RequestId,
    /// Restrict to a specific collector.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub collector: Option<CollectorId>,
    /// If `true`, release the data after fetching (requires
    /// `collector`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub disown: Option<bool>,
}

impl BidiCommand for GetData {
    const METHOD: &'static str = "network.getData";
    type Returns = GetDataResult;
}

/// Response for [`GetData`].
#[derive(Debug, Clone, Deserialize)]
pub struct GetDataResult {
    /// Body payload as a [`network.BytesValue`][spec]
    /// (`{"type":"string","value":…}` or
    /// `{"type":"base64","value":…}`).
    ///
    /// [spec]: https://w3c.github.io/webdriver-bidi/#type-network-BytesValue
    pub bytes: serde_json::Value,
}

/// [`network.disownData`][spec] — release retained body data for one
/// collector without removing the collector itself.
///
/// Useful when you want to keep collecting future data but explicitly
/// drop one captured request's payload.
///
/// [spec]: https://w3c.github.io/webdriver-bidi/#command-network-disownData
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DisownData {
    /// Body kind to release.
    pub data_type: DataType,
    /// Collector to disown for.
    pub collector: CollectorId,
    /// Request to release data for.
    pub request: RequestId,
}

impl BidiCommand for DisownData {
    const METHOD: &'static str = "network.disownData";
    type Returns = Empty;
}

/// [`network.setExtraHeaders`][spec] — install headers that the driver
/// adds to every outgoing request (overwriting matching headers).
///
/// Scope:
/// - No `contexts` / `user_contexts` → set the default extra headers.
/// - `contexts` → scope to those top-level traversables.
/// - `user_contexts` → scope to those user contexts.
///
/// `contexts` and `user_contexts` are mutually exclusive.
///
/// [spec]: https://w3c.github.io/webdriver-bidi/#command-network-setExtraHeaders
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SetExtraHeaders {
    /// Headers to install. Each entry is a [`network.Header`][header]
    /// JSON value (`{"name":"…","value":{"type":"string","value":"…"}}`).
    ///
    /// [header]: https://w3c.github.io/webdriver-bidi/#type-network-Header
    pub headers: Vec<serde_json::Value>,
    /// Restrict to these top-level browsing contexts.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub contexts: Option<Vec<BrowsingContextId>>,
    /// Restrict to these user contexts.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_contexts: Option<Vec<UserContextId>>,
}

impl BidiCommand for SetExtraHeaders {
    const METHOD: &'static str = "network.setExtraHeaders";
    type Returns = Empty;
}

// ---------------------------------------------------------------------------
// Events
// ---------------------------------------------------------------------------

/// Events surfaced by the `network.*` module.
pub(crate) mod events {
    use super::*;

    /// [`network.beforeRequestSent`][spec] — fires just before a request
    /// leaves the network stack.
    ///
    /// If `is_blocked` is `true`, an intercept paused the request — call
    /// [`ContinueRequest`], [`FailRequest`], or [`ProvideResponse`] with
    /// `request.id`.
    ///
    /// [spec]: https://w3c.github.io/webdriver-bidi/#event-network-beforeRequestSent
    #[derive(Debug, Clone, Deserialize)]
    #[serde(rename_all = "camelCase")]
    pub struct BeforeRequestSent {
        /// Browsing context the request belongs to. `None` for requests
        /// without an associated navigable (e.g. service workers).
        pub context: Option<BrowsingContextId>,
        /// Underlying request data.
        pub request: RequestData,
        /// Driver timestamp (ms since epoch).
        pub timestamp: u64,
        /// Optional initiator info ([`network.Initiator`][init]).
        ///
        /// [init]: https://w3c.github.io/webdriver-bidi/#type-network-Initiator
        #[serde(default)]
        pub initiator: Option<serde_json::Value>,
        /// Whether the request is currently blocked by an intercept.
        #[serde(default)]
        pub is_blocked: bool,
        /// Intercept ids that match this request (only populated when
        /// `is_blocked` is `true`).
        #[serde(default)]
        pub intercepts: Vec<InterceptId>,
    }

    impl BidiEvent for BeforeRequestSent {
        const METHOD: &'static str = "network.beforeRequestSent";
    }

    /// [`network.responseStarted`][spec] — fires when response headers
    /// arrive but before the body is delivered.
    ///
    /// If `is_blocked` is `true`, an intercept paused the response —
    /// call [`ContinueResponse`] with `request.id`.
    ///
    /// [spec]: https://w3c.github.io/webdriver-bidi/#event-network-responseStarted
    #[derive(Debug, Clone, Deserialize)]
    #[serde(rename_all = "camelCase")]
    pub struct ResponseStarted {
        /// Browsing context.
        pub context: Option<BrowsingContextId>,
        /// Request data.
        pub request: RequestData,
        /// Response data (headers, status, …).
        pub response: ResponseData,
        /// Driver timestamp.
        pub timestamp: u64,
        /// Whether the response is currently blocked by an intercept.
        #[serde(default)]
        pub is_blocked: bool,
        /// Intercept ids that match this response.
        #[serde(default)]
        pub intercepts: Vec<InterceptId>,
    }

    impl BidiEvent for ResponseStarted {
        const METHOD: &'static str = "network.responseStarted";
    }

    /// [`network.responseCompleted`][spec] — fires when the response is
    /// fully received (or the request is cancelled).
    ///
    /// [spec]: https://w3c.github.io/webdriver-bidi/#event-network-responseCompleted
    #[derive(Debug, Clone, Deserialize)]
    #[serde(rename_all = "camelCase")]
    pub struct ResponseCompleted {
        /// Browsing context.
        pub context: Option<BrowsingContextId>,
        /// Request data.
        pub request: RequestData,
        /// Response data.
        pub response: ResponseData,
        /// Driver timestamp.
        pub timestamp: u64,
    }

    impl BidiEvent for ResponseCompleted {
        const METHOD: &'static str = "network.responseCompleted";
    }

    /// [`network.fetchError`][spec] — fires when a request fails before
    /// completing (network error, DNS failure, blocked by extension, …).
    ///
    /// [spec]: https://w3c.github.io/webdriver-bidi/#event-network-fetchError
    #[derive(Debug, Clone, Deserialize)]
    #[serde(rename_all = "camelCase")]
    pub struct FetchError {
        /// Browsing context.
        pub context: Option<BrowsingContextId>,
        /// Request data.
        pub request: RequestData,
        /// Driver timestamp.
        pub timestamp: u64,
        /// Browser-defined error text.
        pub error_text: String,
    }

    impl BidiEvent for FetchError {
        const METHOD: &'static str = "network.fetchError";
    }

    /// [`network.authRequired`][spec] — fires on an HTTP 401 / 407
    /// challenge. Pair with [`ContinueWithAuth`] when an
    /// [`InterceptPhase::AuthRequired`] intercept matches.
    ///
    /// [spec]: https://w3c.github.io/webdriver-bidi/#event-network-authRequired
    #[derive(Debug, Clone, Deserialize)]
    #[serde(rename_all = "camelCase")]
    pub struct AuthRequired {
        /// Browsing context.
        pub context: Option<BrowsingContextId>,
        /// Request data.
        pub request: RequestData,
        /// Response data (status will be 401 or 407; `authChallenges` is
        /// populated).
        pub response: ResponseData,
        /// Driver timestamp.
        pub timestamp: u64,
        /// Whether the response is currently blocked by an intercept.
        #[serde(default)]
        pub is_blocked: bool,
    }

    impl BidiEvent for AuthRequired {
        const METHOD: &'static str = "network.authRequired";
    }
}

// ---------------------------------------------------------------------------
// Wire-shape types reused by events.
// ---------------------------------------------------------------------------

/// Strongly-typed subset of BiDi's [`network.RequestData`][spec].
///
/// Vendor-specific or rarely-used fields (timing breakdown, body size,
/// destination, initiator type, …) flow through into
/// [`RequestData::extra`]. Strongly-typed extras can be added later
/// without breaking the wire shape.
///
/// [spec]: https://w3c.github.io/webdriver-bidi/#type-network-RequestData
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RequestData {
    /// Request id. The BiDi wire shape names this field `request` — we
    /// rename it to `id` so callers can write
    /// `event.request.id` instead of the doubly-nested
    /// `event.request.request`.
    #[serde(rename = "request")]
    pub id: RequestId,
    /// HTTP method (`"GET"`, `"POST"`, …).
    pub method: String,
    /// Full request URL.
    pub url: String,
    /// Headers — array of [`network.Header`][hdr].
    ///
    /// [hdr]: https://w3c.github.io/webdriver-bidi/#type-network-Header
    #[serde(default)]
    pub headers: Vec<serde_json::Value>,
    /// Cookies sent on the request — array of
    /// [`network.Cookie`][cookie].
    ///
    /// [cookie]: https://w3c.github.io/webdriver-bidi/#type-network-Cookie
    #[serde(default)]
    pub cookies: Vec<serde_json::Value>,
    /// Other fields from the wire shape (timing, body size, destination,
    /// initiator type, …).
    #[serde(flatten)]
    pub extra: serde_json::Map<String, serde_json::Value>,
}

/// Strongly-typed subset of BiDi's [`network.ResponseData`][spec].
///
/// [spec]: https://w3c.github.io/webdriver-bidi/#type-network-ResponseData
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ResponseData {
    /// Final URL after redirects.
    pub url: String,
    /// HTTP protocol (`"http/1.1"`, `"h2"`, …).
    #[serde(default)]
    pub protocol: Option<String>,
    /// HTTP status code.
    pub status: u16,
    /// Status reason phrase.
    pub status_text: String,
    /// Headers — array of [`network.Header`][hdr].
    ///
    /// [hdr]: https://w3c.github.io/webdriver-bidi/#type-network-Header
    #[serde(default)]
    pub headers: Vec<serde_json::Value>,
    /// Other fields from the wire shape (mime type, encoded vs decoded
    /// size, `authChallenges`, …).
    #[serde(flatten)]
    pub extra: serde_json::Map<String, serde_json::Value>,
}

/// RAII guard around an active `network.addIntercept` registration.
///
/// Returned by [`NetworkModule::add_intercept`]. Holds the
/// [`InterceptId`] so you can pass it explicitly to
/// [`NetworkModule::remove_intercept`] or call [`Self::remove`]
/// directly. If the guard is dropped without calling `remove`, a
/// best-effort `network.removeIntercept` is spawned on the current
/// tokio runtime — errors are swallowed because this is a safety net,
/// not a primary cleanup path.
///
/// Prefer the explicit `.remove().await` form when you want errors.
#[derive(Debug)]
pub struct InterceptGuard {
    bidi: BiDi,
    intercept: Option<InterceptId>,
}

impl InterceptGuard {
    pub(crate) fn new(bidi: BiDi, intercept: InterceptId) -> Self {
        Self {
            bidi,
            intercept: Some(intercept),
        }
    }

    /// The underlying server-assigned intercept id.
    pub fn id(&self) -> &InterceptId {
        // Always Some until `remove` is called and consumes the guard.
        self.intercept.as_ref().expect("InterceptGuard already removed")
    }

    /// Remove the intercept now, returning any error from the wire call.
    /// After this, the guard's `Drop` is a no-op.
    pub async fn remove(mut self) -> Result<(), BidiError> {
        if let Some(intercept) = self.intercept.take() {
            self.bidi
                .send(RemoveIntercept {
                    intercept,
                })
                .await?;
        }
        Ok(())
    }
}

impl Drop for InterceptGuard {
    fn drop(&mut self) {
        let Some(intercept) = self.intercept.take() else {
            return;
        };
        // Best-effort: spawn the removal on the current tokio runtime.
        // If no runtime is current, the intercept stays registered for
        // the rest of the session.
        if let Ok(handle) = tokio::runtime::Handle::try_current() {
            let bidi = self.bidi.clone();
            handle.spawn(async move {
                let _ = bidi
                    .send(RemoveIntercept {
                        intercept,
                    })
                    .await;
            });
        }
    }
}

/// Convenience facade for the `network.*` module.
///
/// Returned by [`BiDi::network`](crate::bidi::BiDi::network). For
/// scoped variants of these commands (per-context cache mode, scoped
/// data collectors, scoped extra headers) build the command struct
/// directly and send it via [`BiDi::send`](crate::bidi::BiDi::send).
#[derive(Debug)]
pub struct NetworkModule<'a> {
    bidi: &'a BiDi,
}

impl<'a> NetworkModule<'a> {
    pub(crate) fn new(bidi: &'a BiDi) -> Self {
        Self {
            bidi,
        }
    }

    /// Register a global intercept and return an [`InterceptGuard`].
    ///
    /// Wraps [`network.addIntercept`][spec]. The returned guard removes
    /// the intercept on drop (best-effort; spawned on the current tokio
    /// runtime); call [`InterceptGuard::remove`] for explicit error
    /// handling.
    ///
    /// [spec]: https://w3c.github.io/webdriver-bidi/#command-network-addIntercept
    pub async fn add_intercept(
        &self,
        phases: Vec<InterceptPhase>,
        url_patterns: Option<Vec<serde_json::Value>>,
    ) -> Result<InterceptGuard, BidiError> {
        let result = self
            .bidi
            .send(AddIntercept {
                phases,
                contexts: vec![],
                url_patterns,
            })
            .await?;
        Ok(InterceptGuard::new(self.bidi.clone(), result.intercept))
    }

    /// Drop an intercept by id via [`network.removeIntercept`][spec].
    ///
    /// [spec]: https://w3c.github.io/webdriver-bidi/#command-network-removeIntercept
    pub async fn remove_intercept(&self, intercept: InterceptId) -> Result<(), BidiError> {
        self.bidi
            .send(RemoveIntercept {
                intercept,
            })
            .await?;
        Ok(())
    }

    /// Release a paused request unmodified via
    /// [`network.continueRequest`][spec].
    ///
    /// To modify the request first (rewrite URL, headers, body), build a
    /// [`ContinueRequest`] struct directly.
    ///
    /// [spec]: https://w3c.github.io/webdriver-bidi/#command-network-continueRequest
    pub async fn continue_request(&self, request: RequestId) -> Result<(), BidiError> {
        self.bidi
            .send(ContinueRequest {
                request,
                body: None,
                cookies: None,
                headers: None,
                method: None,
                url: None,
            })
            .await?;
        Ok(())
    }

    /// Release a paused response unmodified via
    /// [`network.continueResponse`][spec].
    ///
    /// [spec]: https://w3c.github.io/webdriver-bidi/#command-network-continueResponse
    pub async fn continue_response(&self, request: RequestId) -> Result<(), BidiError> {
        self.bidi
            .send(ContinueResponse {
                request,
                cookies: None,
                credentials: None,
                headers: None,
                reason_phrase: None,
                status_code: None,
            })
            .await?;
        Ok(())
    }

    /// Fail a paused request via [`network.failRequest`][spec].
    ///
    /// [spec]: https://w3c.github.io/webdriver-bidi/#command-network-failRequest
    pub async fn fail_request(&self, request: RequestId) -> Result<(), BidiError> {
        self.bidi
            .send(FailRequest {
                request,
            })
            .await?;
        Ok(())
    }

    /// Set the global cache mode via [`network.setCacheBehavior`][spec].
    ///
    /// [spec]: https://w3c.github.io/webdriver-bidi/#command-network-setCacheBehavior
    pub async fn set_cache_behavior(&self, mode: CacheBehavior) -> Result<(), BidiError> {
        self.bidi
            .send(SetCacheBehavior {
                cache_behavior: mode,
                contexts: vec![],
                user_contexts: vec![],
            })
            .await?;
        Ok(())
    }

    /// Register a global data collector via
    /// [`network.addDataCollector`][spec]. Pair with [`get_data`](Self::get_data).
    ///
    /// [spec]: https://w3c.github.io/webdriver-bidi/#command-network-addDataCollector
    pub async fn add_data_collector(
        &self,
        data_types: Vec<DataType>,
        max_encoded_data_size: u64,
    ) -> Result<AddDataCollectorResult, BidiError> {
        self.bidi
            .send(AddDataCollector {
                data_types,
                max_encoded_data_size,
                collector_type: None,
                contexts: None,
                user_contexts: None,
            })
            .await
    }

    /// Drop a data collector via [`network.removeDataCollector`][spec].
    ///
    /// [spec]: https://w3c.github.io/webdriver-bidi/#command-network-removeDataCollector
    pub async fn remove_data_collector(&self, collector: CollectorId) -> Result<(), BidiError> {
        self.bidi
            .send(RemoveDataCollector {
                collector,
            })
            .await?;
        Ok(())
    }

    /// Fetch retained body data for `request` via
    /// [`network.getData`][spec].
    ///
    /// To restrict the lookup to one collector or to disown after read,
    /// build the [`GetData`] struct directly.
    ///
    /// [spec]: https://w3c.github.io/webdriver-bidi/#command-network-getData
    pub async fn get_data(
        &self,
        data_type: DataType,
        request: RequestId,
    ) -> Result<GetDataResult, BidiError> {
        self.bidi
            .send(GetData {
                data_type,
                request,
                collector: None,
                disown: None,
            })
            .await
    }

    /// Release retained data for one collector via
    /// [`network.disownData`][spec].
    ///
    /// [spec]: https://w3c.github.io/webdriver-bidi/#command-network-disownData
    pub async fn disown_data(
        &self,
        data_type: DataType,
        collector: CollectorId,
        request: RequestId,
    ) -> Result<(), BidiError> {
        self.bidi
            .send(DisownData {
                data_type,
                collector,
                request,
            })
            .await?;
        Ok(())
    }

    /// Apply extra request headers globally via
    /// [`network.setExtraHeaders`][spec].
    ///
    /// To scope to specific contexts/user-contexts build a
    /// [`SetExtraHeaders`] struct directly.
    ///
    /// [spec]: https://w3c.github.io/webdriver-bidi/#command-network-setExtraHeaders
    pub async fn set_extra_headers(
        &self,
        headers: Vec<serde_json::Value>,
    ) -> Result<(), BidiError> {
        self.bidi
            .send(SetExtraHeaders {
                headers,
                contexts: None,
                user_contexts: None,
            })
            .await?;
        Ok(())
    }
}