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
//! Sentry custom transport implementation.
//!
//! This can be used to send data to an upstream Sentry service in lieue of the
//! built-in transports provided by the sentry-native library itself.

#[cfg(doc)]
use crate::Event;
use crate::{ffi, Options, Ownership, Value};
use std::{
    mem::ManuallyDrop,
    os::raw::{c_char, c_int, c_void},
    process, slice, thread,
    time::Duration,
};
#[cfg(doc)]
use std::{process::abort, sync::Mutex};
pub use sys::SDK_USER_AGENT;
#[cfg(feature = "transport-custom")]
use ::{
    http::{HeaderMap, HeaderValue, Request as HttpRequest},
    std::{
        convert::{Infallible, TryFrom, TryInto},
        str::FromStr,
    },
    thiserror::Error,
    url::{ParseError, Url},
};

/// Sentry errors.
#[cfg(feature = "transport-custom")]
#[derive(Debug, Error, PartialEq)]
pub enum Error {
    /// Failed to parse DSN URL.
    #[error("failed to parse DSN URL")]
    UrlParse(#[from] ParseError),
    /// DSN doesn't have a http(s) scheme.
    #[error("DSN doesn't have a http(s) scheme")]
    Scheme,
    /// DSN has no username.
    #[error("DSN has no username")]
    Username,
    /// DSN has no project ID.
    #[error("DSN has no project ID")]
    ProjectId,
    /// DSN has no host.
    #[error("DSN has no host")]
    Host,
}

#[cfg(feature = "transport-custom")]
impl From<Infallible> for Error {
    fn from(from: Infallible) -> Self {
        match from {}
    }
}

/// The [`http::Request`] request your [`Transport`] is expected to send.
#[cfg(feature = "transport-custom")]
pub type Request = HttpRequest<Envelope>;

/// The MIME type for Sentry envelopes.
pub const ENVELOPE_MIME: &str = "application/x-sentry-envelope";
/// Version of the Sentry API we can communicate with, AFAICT this is just
/// hardcoded into sentry-native, so ... two can play at that game!
pub const API_VERSION: i8 = 7;

/// The return from [`Transport::shutdown`], which determines if we tell
/// the Sentry SDK if we were able to send all requests to the remote service
/// or not in the allotted time.
#[derive(Copy, Clone, Debug, Hash, Eq, Ord, PartialEq, PartialOrd)]
pub enum Shutdown {
    /// The custom transport was able to send all requests in the allotted time.
    Success,
    /// One or more requests could not be sent in the specified time frame.
    TimedOut,
}

impl Shutdown {
    /// Converts [`Shutdown`] into [`c_int`].
    const fn into_raw(self) -> c_int {
        match self {
            Self::Success => 0,
            Self::TimedOut => 1,
        }
    }
}

/// Trait used to define a custom transport that Sentry can use to send events
/// to a Sentry service.
///
/// # Examples
/// ```
/// # /*
/// #![cfg(feature = "transport-custom")]
///
/// # */
/// # fn main() -> anyhow::Result<()> {
/// # #[cfg(feature = "transport-custom")]
/// # {
/// # use sentry_contrib_native::{Dsn, Event, Options, RawEnvelope, test, Transport};
/// # use std::convert::TryInto;
/// # test::set_hook();
/// use reqwest::blocking::Client;
///
/// struct CustomTransport {
///     dsn: Dsn,
///     client: Client,
/// };
///
/// impl CustomTransport {
///     fn new(options: &Options) -> Result<Self, ()> {
///         Ok(CustomTransport {
///             dsn: options.dsn().and_then(|dsn| Dsn::new(dsn).ok()).ok_or(())?,
///             client: Client::new(),
///         })
///     }
/// }
///
/// impl Transport for CustomTransport {
///     fn send(&self, envelope: RawEnvelope) {
///         let dsn = self.dsn.clone();
///         let client = self.client.clone();
///
///         // in a correct implementation envelopes have to be sent in order for sessions to work
///         std::thread::spawn(move || {
///             let request = envelope
///                 .to_request(dsn)
///                 .map(|body| body.as_bytes().to_vec());
///             client
///                 .execute(request.try_into().unwrap())
///                 .expect("failed to send envelope")
///         });
///     }
/// }
///
/// let dsn = "https://public_key_1234@organization_1234.ingest.sentry.io/project_id_1234";
///
/// let mut options = Options::new();
/// options.set_dsn(dsn);
/// options.set_transport(CustomTransport::new);
/// # let shutdown = options.init()?;
/// # Event::new().capture();
/// # shutdown.shutdown();
/// # test::verify_panics();
/// # } Ok(()) }
/// ```
/// See the
/// [`transport-custom`](https://github.com/daxpedda/sentry-contrib-native/blob/master/examples/custom-transport.rs)
/// example for a more sophisticated implementation.
pub trait Transport: 'static + Send + Sync {
    /// Sends the specified envelope to a Sentry service.
    ///
    /// It is **required** to send envelopes in order for sessions to work
    /// correctly.
    ///
    /// It is **highly** recommended to not block in this method, but rather
    /// to enqueue the worker to another thread.
    fn send(&self, envelope: RawEnvelope);

    /// Shuts down the transport worker. The worker should try to flush all
    /// of the pending requests to Sentry before shutdown. If the worker is
    /// successfully able to empty its queue and shutdown before the specified
    /// timeout duration, it should return [`Shutdown::Success`],
    /// otherwise it should return [`Shutdown::TimedOut`].
    ///
    /// The default implementation will block the thread for `timeout` duration
    /// and always return [`Shutdown::TimedOut`], it has to be adjusted to
    /// work correctly.
    #[must_use]
    #[allow(clippy::boxed_local)]
    fn shutdown(self: Box<Self>, timeout: Duration) -> Shutdown {
        thread::sleep(timeout);
        Shutdown::TimedOut
    }
}

impl<T: Fn(RawEnvelope) + 'static + Send + Sync> Transport for T {
    fn send(&self, envelope: RawEnvelope) {
        self(envelope);
    }
}

/// Type used to store the startup function.
type Startup =
    Box<dyn (FnOnce(&Options) -> Result<Box<dyn Transport>, ()>) + 'static + Send + Sync>;

/// Internal state of the [`Transport`].
pub enum State {
    /// [`Transport`] is in the startup phase.
    Startup(Startup),
    /// [`Transport`] is in the sending phase.
    Send(Box<dyn Transport>),
}

/// Function to pass to [`sys::transport_set_startup_func`], which in turn calls
/// the user defined one.
///
/// `state` is mutably thread-safe, because this function is only called once
/// during [`Options::init`], which is blocked with our global [`Mutex`],
/// preventing [`Event::capture`] or [`shutdown`](crate::shutdown), the only
/// functions that interfere.
///
/// This function will catch any unwinding panics and [`abort`] if any occured.
pub extern "C" fn startup(options: *const sys::Options, state: *mut c_void) -> c_int {
    let options = Options::from_sys(Ownership::Borrowed(options));

    let state = unsafe { Box::from_raw(state.cast::<Option<State>>()) };
    let mut state = ManuallyDrop::new(state);

    if let Some(State::Startup(startup)) = state.take() {
        if let Ok(transport) = ffi::catch(|| startup(&options)) {
            state.replace(State::Send(transport));

            0
        } else {
            1
        }
    } else {
        process::abort();
    }
}

/// Function to pass to [`sys::transport_new`], which in turn calls the user
/// defined one.
///
/// This function will catch any unwinding panics and [`abort`] if any occured.
pub extern "C" fn send(envelope: *mut sys::Envelope, state: *mut c_void) {
    let envelope = RawEnvelope(envelope);

    let state = unsafe { Box::from_raw(state.cast::<Option<State>>()) };
    let state = ManuallyDrop::new(state);

    if let Some(State::Send(transport)) = state.as_ref() {
        ffi::catch(|| transport.send(envelope));
    } else {
        process::abort();
    }
}

/// Function to pass to [`sys::transport_set_shutdown_func`], which in turn
/// calls the user defined one.
///
/// `state` is ownership thread-safe, because this function is only called once
/// during [`shutdown`](crate::shutdown), which is blocked with our global
/// [`Mutex`], preventing [`Options::init`] or [`Event::capture`], the only
/// functions that interfere.
///
/// This function will catch any unwinding panics and [`abort`] if any occured.
pub extern "C" fn shutdown(timeout: u64, state: *mut c_void) -> c_int {
    let timeout = Duration::from_millis(timeout);
    let mut state = unsafe { Box::from_raw(state.cast::<Option<State>>()) };

    if let Some(State::Send(transport)) = state.take() {
        ffi::catch(|| transport.shutdown(timeout)).into_raw()
    } else {
        process::abort();
    }
}

/// Wrapper for the raw envelope that we should send to Sentry.
///
/// # Examples
/// ```
/// # #[cfg(feature = "transport-custom")]
/// # use sentry_contrib_native::{Dsn, Request};
/// # use sentry_contrib_native::{Envelope, RawEnvelope, Transport, Value};
/// struct CustomTransport {
///     #[cfg(feature = "transport-custom")]
///     dsn: Dsn,
/// };
///
/// impl Transport for CustomTransport {
///     fn send(&self, raw_envelope: RawEnvelope) {
///         // get the `Event` that is being sent
///         let event: Value = raw_envelope.event();
///         // serialize it, maybe move this to another thread to prevent blocking
///         let envelope: Envelope = raw_envelope.serialize();
///         // or convert it into a `Request` right away!
///         #[cfg(feature = "transport-custom")]
///         let request: Request = raw_envelope.to_request(self.dsn.clone());
///     }
/// }
/// ```
#[derive(Debug, Hash, Eq, Ord, PartialEq, PartialOrd)]
pub struct RawEnvelope(*mut sys::Envelope);

unsafe impl Send for RawEnvelope {}
unsafe impl Sync for RawEnvelope {}

impl Drop for RawEnvelope {
    fn drop(&mut self) {
        unsafe { sys::envelope_free(self.0) }
    }
}

impl RawEnvelope {
    /// Serialize a [`RawEnvelope`] into an [`Envelope`].
    #[must_use = "`RawEnvelope::serialize` only converts it to an `Envelope`, this doesn't do anything until it is sent"]
    pub fn serialize(&self) -> Envelope {
        let mut envelope_size = 0;
        let serialized_envelope = unsafe { sys::envelope_serialize(self.0, &mut envelope_size) };

        Envelope {
            data: serialized_envelope,
            len: envelope_size,
        }
    }

    /// Yields the event that is being sent in the form of a [`Value`].
    #[must_use]
    pub fn event(&self) -> Value {
        Value::from_raw_borrowed(unsafe { sys::envelope_get_event(self.0) })
    }

    /// Constructs a HTTP request for the provided [`RawEnvelope`] with a
    /// [`Dsn`].
    ///
    /// For more information see [`Envelope::into_request`].
    #[cfg(feature = "transport-custom")]
    #[must_use = "`Request` doesn't do anything until it is sent"]
    pub fn to_request(&self, dsn: Dsn) -> Request {
        self.serialize().into_request(dsn)
    }
}

/// The actual body which transports send to Sentry.
///
/// # Examples
/// ```
/// # #[cfg(feature = "transport-custom")]
/// # use sentry_contrib_native::{Dsn, Request};
/// # use sentry_contrib_native::{Envelope, RawEnvelope, Transport, Value};
/// struct CustomTransport {
///     #[cfg(feature = "transport-custom")]
///     dsn: Dsn,
/// };
///
/// impl Transport for CustomTransport {
///     fn send(&self, raw_envelope: RawEnvelope) {
///         // serialize it, maybe move this to another thread to prevent blocking
///         let envelope: Envelope = raw_envelope.serialize();
///         // look at that body!
///         println!("{:?}", envelope.as_bytes());
///         // let's build the whole `Request`
///         #[cfg(feature = "transport-custom")]
///         let request: Request = envelope.into_request(self.dsn.clone());
///     }
/// }
/// ```
#[derive(Debug, Hash, Eq, Ord, PartialEq, PartialOrd)]
pub struct Envelope {
    /// The raw bytes of the serialized envelope, which is the actual data to
    /// send as the body of a request.
    data: *const c_char,
    /// The length in bytes of the serialized data.
    len: usize,
}

unsafe impl Send for Envelope {}
unsafe impl Sync for Envelope {}

impl Drop for Envelope {
    fn drop(&mut self) {
        unsafe { sys::free(self.data as _) }
    }
}

impl AsRef<[u8]> for Envelope {
    fn as_ref(&self) -> &[u8] {
        self.as_bytes()
    }
}

impl Envelope {
    /// Get underlying data as `&[u8]`.
    #[must_use]
    pub fn as_bytes(&self) -> &[u8] {
        unsafe { slice::from_raw_parts(self.data.cast(), self.len) }
    }

    /// Constructs a HTTP request for the provided [`sys::Envelope`] with the
    /// DSN that was registered with the SDK.
    ///
    /// The return value has all of the necessary pieces of data to create a
    /// HTTP request with the HTTP client of your choice:
    ///
    /// * The URL to send the request to.
    /// * The headers that must be set.
    /// * The body of the request.
    ///
    /// The `content-length` header is already set for you, though some HTTP
    /// clients will automatically overwrite it, which should be fine.
    ///
    /// The `body` in the request is an [`Envelope`], which implements
    /// `AsRef<[u8]>` to retrieve the actual bytes that should be sent as the
    /// body.
    #[cfg(feature = "transport-custom")]
    #[must_use = "`Request` doesn't do anything until it is sent"]
    pub fn into_request(self, dsn: Dsn) -> Request {
        let mut request = HttpRequest::builder();
        *request.headers_mut().expect("failed to build headers") = dsn.to_headers();
        request
            .method("POST")
            .uri(dsn.url)
            .header("content-length", self.as_bytes().len())
            .body(self)
            .expect("failed to build request")
    }
}

/// Contains the pieces that are needed to build correct headers for a request
/// based on the given DSN.
///
/// # Examples
/// ```
/// # /*
/// #![cfg(feature = "transport-custom")]
///
/// # */
/// # fn main() -> anyhow::Result<()> {
/// # #[cfg(feature = "transport-custom")]
/// # {
/// # use sentry_contrib_native::{Dsn, Event, http::HeaderMap, Options, RawEnvelope, test, Transport};
///
/// struct CustomTransport {
///     dsn: Dsn,
/// };
///
/// impl CustomTransport {
///     fn new(options: &Options) -> Result<Self, ()> {
///         Ok(CustomTransport {
///             // we can also get the DSN here
///             dsn: options.dsn().and_then(|dsn| Dsn::new(dsn).ok()).ok_or(())?,
///         })
///     }
/// }
///
/// impl Transport for CustomTransport {
///     fn send(&self, envelope: RawEnvelope) {
///         // we need `Dsn` to build the `Request`!
///         envelope.to_request(self.dsn.clone());
///         // or build your own request with the help of a URL, `HeaderMap` and body.
///         let (url, headers, body): (&str, HeaderMap, &[u8]) = (self.dsn.url(), self.dsn.to_headers(), envelope.serialize().as_bytes());
///     }
/// }
///
/// let dsn = "https://public_key_1234@organization_1234.ingest.sentry.io/project_id_1234";
///
/// let mut options = Options::new();
/// options.set_dsn(dsn);
/// // we can take the `dsn` right here
/// let custom_transport = CustomTransport {
///     dsn: Dsn::new(dsn)?,
/// };
/// options.set_transport(move |_| Ok(custom_transport));
/// // this is also possible
/// options.set_transport(|options| Ok(CustomTransport {
///     dsn: options.dsn().and_then(|dsn| Dsn::new(dsn).ok()).ok_or(())?,
/// }));
/// // or use a method more directly
/// options.set_transport(CustomTransport::new);
/// # } Ok(()) }
/// ```
#[cfg(feature = "transport-custom")]
#[derive(Clone, Debug, Hash, Eq, Ord, PartialEq, PartialOrd)]
pub struct Dsn {
    /// The auth header value
    auth: String,
    /// The full URL to send envelopes to
    url: String,
}

#[cfg(feature = "transport-custom")]
impl Dsn {
    /// Creates a new [`Dsn`] from a [`str`].
    ///
    /// # Errors
    /// Fails with [`Error::Transport`](crate::Error::Transport) if the DSN is
    /// invalid.
    pub fn new(dsn: &str) -> Result<Self, crate::Error> {
        // a sentry DSN contains the following components:
        // <https://<username>@<host>/<path>>
        // * username = public key
        // * host = obviously, the host, sentry.io in the case of the hosted service
        // * path = the project ID
        let dsn_url = Url::parse(dsn).map_err(Error::from)?;

        // do some basic checking that the DSN is remotely valid
        if !dsn_url.scheme().starts_with("http") {
            return Err(Error::Scheme.into());
        }

        if dsn_url.username().is_empty() {
            return Err(Error::Username.into());
        }

        if dsn_url.path().is_empty() || dsn_url.path() == "/" {
            return Err(Error::ProjectId.into());
        }

        match dsn_url.host_str() {
            None => Err(Error::Host.into()),
            Some(host) => {
                let mut auth = format!(
                    "Sentry sentry_key={}, sentry_version={}, sentry_client={}",
                    dsn_url.username(),
                    API_VERSION,
                    SDK_USER_AGENT
                );

                if let Some(password) = dsn_url.password() {
                    auth.push_str(", sentry_secret=");
                    auth.push_str(password);
                }

                let host = dsn_url
                    .port()
                    .map_or_else(|| host.to_owned(), |port| format!("{}:{}", host, port));

                let url = format!(
                    "{}://{}/api/{}/envelope/",
                    dsn_url.scheme(),
                    host,
                    &dsn_url.path()[1..]
                );

                Ok(Self { auth, url })
            }
        }
    }

    /// The auth header value.
    #[must_use]
    pub fn auth(&self) -> &str {
        &self.auth
    }

    /// The full URL to send envelopes to.
    #[must_use]
    pub fn url(&self) -> &str {
        &self.url
    }

    /// Consume [`Dsn`] and return it's parts.
    #[must_use]
    #[allow(clippy::missing_const_for_fn)]
    pub fn into_parts(self) -> Parts {
        Parts {
            auth: self.auth,
            url: self.url,
        }
    }

    /// Yields a [`HeaderMap`] to build a correct HTTP request with this
    /// [`Dsn`].
    #[cfg(feature = "transport-custom")]
    #[must_use]
    pub fn to_headers(&self) -> HeaderMap {
        let mut headers = HeaderMap::new();
        headers.insert("user-agent", HeaderValue::from_static(SDK_USER_AGENT));
        headers.insert("content-type", HeaderValue::from_static(ENVELOPE_MIME));
        headers.insert("accept", HeaderValue::from_static("*/*"));
        headers.insert(
            "x-sentry-auth",
            (&self.auth)
                .try_into()
                .expect("failed to insert `x-sentry-auth`"),
        );
        headers
    }
}

#[cfg(feature = "transport-custom")]
impl FromStr for Dsn {
    type Err = crate::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::new(s)
    }
}

#[cfg(feature = "transport-custom")]
impl TryFrom<&str> for Dsn {
    type Error = crate::Error;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        Self::new(value)
    }
}

/// [`Parts`] aquired from [`Dsn::into_parts`].
#[cfg(feature = "transport-custom")]
#[derive(Clone, Debug, Hash, Eq, Ord, PartialEq, PartialOrd)]
pub struct Parts {
    /// The auth header value
    pub auth: String,
    /// The full URL to send envelopes to
    pub url: String,
}

#[cfg(all(test, feature = "transport-custom"))]
#[rusty_fork::fork_test(timeout_ms = 60000)]
fn transport() -> anyhow::Result<()> {
    use crate::Event;
    use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};

    struct CustomTransport {
        dsn: Dsn,
    }

    impl CustomTransport {
        fn new(dsn: Dsn, options: &Options) -> Self {
            assert!(!STARTUP.swap(true, Ordering::SeqCst));
            assert_eq!(dsn, Dsn::new(options.dsn().unwrap()).unwrap());

            Self { dsn }
        }
    }

    impl Transport for CustomTransport {
        fn send(&self, envelope: RawEnvelope) {
            SEND.fetch_add(1, Ordering::SeqCst);

            let _event = envelope.event();
            let request_1 = envelope.to_request(self.dsn.clone());

            let envelope = envelope.serialize();
            let request_2 = envelope.into_request(self.dsn.clone());

            assert_eq!(request_1.uri(), request_2.uri());
            assert_eq!(request_1.headers(), request_2.headers());
            assert_eq!(request_1.body().as_bytes(), request_2.body().as_bytes());
        }

        fn shutdown(self: Box<Self>, _: Duration) -> Shutdown {
            assert!(!SHUTDOWN.swap(true, Ordering::SeqCst));
            Shutdown::Success
        }
    }

    static STARTUP: AtomicBool = AtomicBool::new(false);
    static SEND: AtomicUsize = AtomicUsize::new(0);
    static SHUTDOWN: AtomicBool = AtomicBool::new(false);

    let mut options = Options::new();
    let dsn = Dsn::new(options.dsn().unwrap())?;
    let _event = dsn.to_headers();
    options.set_transport(|options| Ok(CustomTransport::new(dsn, options)));
    let shutdown = options.init()?;

    Event::new().capture();
    Event::new().capture();
    Event::new().capture();

    shutdown.shutdown();

    assert!(STARTUP.load(Ordering::SeqCst));
    assert_eq!(3, SEND.load(Ordering::SeqCst));
    assert!(SHUTDOWN.load(Ordering::SeqCst));

    Ok(())
}

#[cfg(all(test, feature = "transport-custom"))]
#[rusty_fork::fork_test(timeout_ms = 60000)]
fn dsn() {
    use crate::Event;

    #[allow(clippy::needless_pass_by_value)]
    fn send(envelope: RawEnvelope) {
        {
            let dsn = Dsn::new(
                "https://a0b1c2d3e4f5678910abcdeffedcba12@o209016.ingest.sentry.io/0123456",
            )
            .unwrap();
            let request = envelope.to_request(dsn);

            assert_eq!(
                request.uri(),
                "https://o209016.ingest.sentry.io/api/0123456/envelope/"
            );
            let headers = request.headers();
            assert_eq!(headers.get("x-sentry-auth").unwrap(), &format!("Sentry sentry_key=a0b1c2d3e4f5678910abcdeffedcba12, sentry_version={}, sentry_client={}", API_VERSION, SDK_USER_AGENT));
        }

        {
            let dsn = Dsn::new("http://a0b1c2d3e4f5678910abcdeffedcba12@192.168.1.1:9000/0123456")
                .unwrap();
            let request = envelope.to_request(dsn);

            assert_eq!(
                request.uri(),
                "http://192.168.1.1:9000/api/0123456/envelope/"
            );
            let headers = request.headers();
            assert_eq!(headers.get("x-sentry-auth").unwrap(), &format!("Sentry sentry_key=a0b1c2d3e4f5678910abcdeffedcba12, sentry_version={}, sentry_client={}", API_VERSION, SDK_USER_AGENT));
        }
    }

    let mut options = Options::new();
    options.set_transport(|_| Ok(send));
    let _shutdown = options.init();

    Event::new().capture();
}