http_uri 1.0.1

This crate provides types for representing http uris and their invariants.
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
//! This crate provides types for representing http uris and their invariants.
//!

#![warn(missing_docs)]
#![deny(unused_qualifications)]

use std::{
    fmt::{Debug, Display},
    ops::Deref,
    str::FromStr,
    sync::Arc,
};

use iri_string::{
    components::AuthorityComponents,
    format::ToDedicatedString,
    types::{UriReferenceStr, UriStr, UriString},
};
use unicase::Ascii;

#[cfg(feature = "invariants")]
pub mod invariant;
#[cfg(feature = "invariants")]
pub mod predicate;

pub mod security;

/// Http scheme.
pub const HTTP_SCHEME: Ascii<&'static str> = Ascii::new("http");
/// Https scheme.
pub const HTTPS_SCHEME: Ascii<&'static str> = Ascii::new("https");

/// Default port for http scheme.
pub const HTTP_DEFAULT_PORT: &str = "80";
/// Default port for https scheme.
pub const HTTPS_DEFAULT_PORT: &str = "443";

/// A O(1) clonable struct for representing http uri.
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct HttpUri {
    uri: Arc<UriStr>,
}

impl Debug for HttpUri {
    #[inline]
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "HttpUri({})", self.uri.as_str())
    }
}

impl Display for HttpUri {
    #[inline]
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Display::fmt(self.uri.as_str(), f)
    }
}

#[cfg(feature = "serde")]
impl serde::Serialize for HttpUri {
    #[inline]
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        self.uri.serialize(serializer)
    }
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for HttpUri {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        struct Expected;

        impl serde::de::Expected for Expected {
            fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                write!(formatter, "valid http uri")
            }
        }

        let uri = UriString::deserialize(deserializer)?;

        Self::try_from(uri.as_slice()).map_err(|_| {
            <D::Error as serde::de::Error>::invalid_value(
                serde::de::Unexpected::Other("invalid http uri"),
                &Expected,
            )
        })
    }
}

#[derive(Debug, Clone, thiserror::Error, PartialEq, Eq)]
/// Error of invalid http uri.
pub enum InvalidHttpUri {
    /// Invalid uri.
    #[error("Given source is not a valid uri")]
    InvalidUri,

    /// Empty host in uri.
    #[error("Given source uri has empty host")]
    EmptyHost,

    /// Non http scheme in uri.
    #[error("Given source uri has non http scheme")]
    NonHttpScheme,
}

impl TryFrom<&UriStr> for HttpUri {
    type Error = InvalidHttpUri;

    fn try_from(uri: &UriStr) -> Result<Self, Self::Error> {
        // Ensure scheme is http/https
        let scheme = Ascii::new(uri.scheme_str());
        if scheme != HTTP_SCHEME && scheme != HTTPS_SCHEME {
            return Err(InvalidHttpUri::NonHttpScheme);
        }

        // Ensure host is non-empty.
        // > A sender MUST NOT generate an "http(s)" URI with an empty host identifier.
        // > A recipient that processes such a URI reference MUST reject it as invalid.
        let is_empty_host = uri
            .authority_components()
            .map(|a| a.host().is_empty())
            .unwrap_or(true);
        if is_empty_host {
            return Err(InvalidHttpUri::EmptyHost);
        }
        Ok(Self {
            uri: Arc::from(uri),
        })
    }
}

impl TryFrom<&str> for HttpUri {
    type Error = InvalidHttpUri;

    #[inline]
    fn try_from(uri_str: &str) -> Result<Self, Self::Error> {
        let uri: &UriStr = uri_str.try_into().map_err(|_| InvalidHttpUri::InvalidUri)?;
        uri.try_into()
    }
}

impl FromStr for HttpUri {
    type Err = InvalidHttpUri;

    #[inline]
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        s.try_into()
    }
}

impl Deref for HttpUri {
    type Target = UriStr;

    #[inline]
    fn deref(&self) -> &Self::Target {
        self.uri.as_ref()
    }
}

impl AsRef<UriStr> for HttpUri {
    #[inline]
    fn as_ref(&self) -> &UriStr {
        self.uri.as_ref()
    }
}

impl AsRef<UriReferenceStr> for HttpUri {
    #[inline]
    fn as_ref(&self) -> &UriReferenceStr {
        self.uri.as_ref().as_ref()
    }
}

impl AsRef<str> for HttpUri {
    #[inline]
    fn as_ref(&self) -> &str {
        self.uri.as_str()
    }
}

impl From<HttpUri> for UriString {
    #[inline]
    fn from(val: HttpUri) -> Self {
        val.uri.deref().to_owned()
    }
}

impl HttpUri {
    /// Get authority components of http uri
    #[inline]
    pub fn authority_components(&self) -> AuthorityComponents {
        self.uri.authority_components().expect("Checked to be some")
    }

    /// Get authority str of this http uri
    #[inline]
    pub fn authority_str(&self) -> &str {
        self.uri.authority_str().expect("Checked to be some")
    }

    /// Checks if uri is std normalized as per rfc3986
    #[inline]
    pub fn is_rfc3986_normalized(&self) -> bool {
        self.uri.is_normalized_rfc3986()
    }

    /// Returns a std normal http uri. standard normalization follows rfc 3986
    #[inline]
    pub fn normalize_rfc3986(&self) -> HttpUri {
        HttpUri {
            uri: Arc::from(self.uri.normalize().to_dedicated_string().as_ref()),
        }
    }

    /// Check if http uri has explicit http default port.
    fn has_explicit_default_port(&self) -> bool {
        self.authority_components()
            .port()
            .map(|port| {
                let scheme = Ascii::new(self.scheme_str());
                // If port string is empty, but exists with delim.
                port.is_empty()
            // If scheme is http, and port is 80.
                || (scheme == HTTP_SCHEME && port == HTTP_DEFAULT_PORT)
            // If scheme is https, and port is 443.
                || (scheme == HTTPS_SCHEME && port == HTTPS_DEFAULT_PORT)
            })
            .unwrap_or(false)
    }

    /// Checks if uri is http normalized.
    #[inline]
    pub fn is_http_normalized(&self) -> bool {
        // If is normalized as per rfc3986.
        self.is_rfc3986_normalized()
            // Has no explicit default port.
            && !self.has_explicit_default_port()
            // Has non empty path str.
            && !self.uri.path_str().is_empty()
            // Has no non trailing empty segments.
            && !self.uri.path_str().contains("//")
    }

    /// Returns a normal http uri.
    /// This function performs additional http specific normalization along with uri normalization specified by rfc3986.
    ///
    /// Http normalization entails:
    /// - Normalization of port, If default port is explicitly specified,it will be removed.
    /// - Normalization of path: Empty path will be normalized to `/`.
    /// - If there are non-trailing empty segments in path, they will be removed.
    ///
    pub fn http_normalized(&self) -> HttpUri {
        // Perform uri normalization as per rfc3986
        let rfc3986_normalized = self.normalize_rfc3986();

        // Check if uri has explicit default port.
        let has_explicit_default_port = rfc3986_normalized.has_explicit_default_port();

        // Check if uri has empty path.
        let has_empty_path = rfc3986_normalized.path_str().is_empty();

        // Check if uri has non trailing empty segment..
        let has_non_trailing_empty_segment = rfc3986_normalized.path_str().contains("//");

        // If http normalized, return it.
        if !has_explicit_default_port && !has_empty_path && !has_non_trailing_empty_segment {
            return rfc3986_normalized;
        }

        let mut buffer = String::with_capacity(rfc3986_normalized.len());
        // Push scheme
        buffer.push_str(rfc3986_normalized.scheme_str());
        // Push scheme-separator.
        buffer.push_str("://");
        // Push authority
        buffer.push_str(if !has_explicit_default_port {
            rfc3986_normalized.authority_str()
        } else {
            rfc3986_normalized
                .authority_str()
                .rsplit_once(':')
                .expect("Must be some, as port is some")
                .0
        });

        // Push normalized path
        let mut is_prev_slash = false;
        for ch in rfc3986_normalized.path_str().chars() {
            let is_slash = ch == '/';
            if !(is_slash && is_prev_slash) {
                buffer.push(ch);
            }
            is_prev_slash = is_slash;
        }

        // Normalize non empty path..
        if has_empty_path {
            buffer.push('/');
        }

        // Push query
        if let Some(query) = rfc3986_normalized.query_str() {
            buffer.push('?');
            buffer.push_str(query);
        }

        // Push fragment.
        if let Some(fragment) = rfc3986_normalized.fragment() {
            buffer.push('#');
            buffer.push_str(fragment.as_str());
        }

        buffer.as_str().try_into().expect("Must be valid")
    }

    /// Get if uri is https.
    #[inline]
    pub fn is_https(&self) -> bool {
        HTTPS_SCHEME.eq_ignore_ascii_case(self.scheme_str())
    }

    /// Get if http uri's host is localhost.
    pub fn is_localhost(&self) -> bool {
        let host = self.authority_components().host();
        // Localhost or subdomains.
        host == "localhost" || host.ends_with(".localhost")
    }

    /// Get uri as string.
    #[inline]
    pub fn as_str(&self) -> &str {
        self.uri.as_str()
    }

    /// Get inner uri.
    #[inline]
    pub fn inner(&self) -> &UriStr {
        self.uri.as_ref()
    }
}

#[cfg(feature = "sophia")]
impl sophia_api::term::Term for HttpUri {
    type BorrowTerm<'x> = &'x Self;

    #[inline]
    fn kind(&self) -> sophia_api::term::TermKind {
        sophia_api::term::TermKind::Iri
    }

    #[inline]
    fn borrow_term(&self) -> Self::BorrowTerm<'_> {
        self
    }

    #[inline]
    fn iri(&self) -> Option<sophia_api::term::IriRef<sophia_api::MownStr>> {
        Some(sophia_api::term::IriRef::new_unchecked(
            sophia_api::MownStr::from_str(self.as_str()),
        ))
    }
}

#[cfg(feature = "sophia")]
impl From<&HttpUri> for sophia_api::prelude::Iri<String> {
    fn from(value: &HttpUri) -> Self {
        sophia_api::prelude::Iri::new_unchecked(value.uri.as_str().to_owned())
    }
}

#[cfg(feature = "sophia")]
impl From<&HttpUri> for sophia_api::prelude::Iri<Arc<str>> {
    fn from(value: &HttpUri) -> Self {
        sophia_api::prelude::Iri::new_unchecked(Arc::from(value.uri.as_str()))
    }
}

#[cfg(test)]
mod tests_convert {
    use claims::{assert_err_eq, assert_ok};
    use rstest::*;

    use super::*;

    #[rstest]
    #[case::invalid_char1("http://pod1.example.org/path/to/a b")]
    #[case::invalid_char2("http://pod1.example.org/a\nb")]
    #[case::invalid_char3("http://pod1.example.org/?a\\b")]
    #[case::invalid_char3("http://pod1.example.org/a%b")]
    #[case::invalid_char4("http://pod1.example.org/rama<>sita/doc")]
    #[case::invalid_non_ascii1("http://pod1.example.org/राम")]
    #[case::invalid_non_ascii2("http://pod1.example.org/అయోధ్య")]
    #[case::invalid_gen_delim3("http://pod1.example.org/a/b[c")]
    #[case::invalid_gen_delim4("http://pod1.example.org/a/b]c")]
    fn invalid_uri_will_be_rejected(#[case] uri_str: &str) {
        assert_err_eq!(HttpUri::try_from(uri_str), InvalidHttpUri::InvalidUri);
    }

    #[rstest]
    #[case::ftp("ftp://pod1.example.org/path/to/a")]
    #[case::urn("urn:pod1.example.org::ab")]
    #[case::data("data://pod1.example.org/?ab")]
    #[case::file("file://pod1.example.org/ab")]
    fn uri_with_non_http_scheme_will_be_rejected(#[case] uri_str: &'static str) {
        assert_err_eq!(HttpUri::try_from(uri_str), InvalidHttpUri::NonHttpScheme);
    }

    #[rstest]
    #[case::http_no_authority("http:/path/to/a")]
    #[case::https_no_authority("https:/ab")]
    #[case::http_empty_host("http:///a/b?q")]
    #[case::https_empty_host("https:///a/b?q")]
    fn uri_with_invalid_host_will_be_rejected(#[case] uri_str: &'static str) {
        assert_err_eq!(HttpUri::try_from(uri_str), InvalidHttpUri::EmptyHost);
    }

    #[rstest]
    #[case::origin_only("http://pod1.example.org")]
    #[case::explicit_root_path("http://pod1.example.org/")]
    #[case::un_normalized_scheme("HTTP://pod1.example.org/")]
    #[case::un_normalized_authority("http://pod1.EXAMPLE.org/")]
    #[case::ip_authority("http://127.0.0.1/")]
    #[case::localhost("http://localhost/")]
    #[case::explicit_default_port("http://pod1.example.org:80/")]
    #[case::https("https://pod1.example.org/")]
    #[case::explicit_default_port_2("https://pod1.example.org:443/")]
    #[case::with_query("http://pod1.example.org/a/b?q")]
    #[case::un_normalized_path_1("http://pod1.example.org/a//b")]
    #[case::un_normalized_path_2("http://pod1.example.org/a/b%41c")]
    #[case::un_normalized_path_3("http://pod1.example.org/c%2fd")]
    #[case("http://pod1.example.org/a/b")]
    #[case::with_fragment("http://pod1.example.org/a#bc")]
    #[case::with_query_fragment("http://pod1.example.org/a?b#c")]
    fn valid_http_uri_will_be_accepted(#[case] uri_str: &'static str) {
        assert_ok!(HttpUri::try_from(uri_str));
    }
}

#[cfg(test)]
mod tests_normal_check {
    use rstest::*;

    use super::*;

    pub fn check_if_http_normal(uri_str: &str) -> bool {
        HttpUri::try_from(uri_str)
            .expect("Claimed valid")
            .is_http_normalized()
    }

    #[rstest]
    #[case::a("http://pod1.example.org/kosala/%61yodhya")]
    #[case::a_q("http://pod1.example.org/kosala?%61yodhya")]
    #[case::a_cap("http://pod1.example.org/kosala/%41yodhya")]
    #[case::dig_1("http://pod1.example.org/kosala/raghu%31")]
    #[case::hyphen("http://pod1.example.org/rama%2Drajya")]
    #[case::tilde("http://pod1.example.org/rama%7Esita")]
    #[case::tilde_q("http://pod1.example.org/?rama%7Esita")]
    #[case::period("http://pod1.example.org/kosala%2Eayodhya")]
    #[case::period_2("http://pod1.example.org/path/to/%2E%2E/c")]
    #[case::underscore("http://pod1.example.org/rama%5Flakshmana")]
    fn http_uri_un_normal_with_un_reserved_char_encoded_will_be_non_normal(
        #[case] uri_str: &'static str,
    ) {
        assert!(
            !check_if_http_normal(uri_str),
            "normalcy of uri \"{}\" is computed incorrectly ",
            uri_str
        );
    }

    #[rstest]
    #[case("http://pod1.example.org/rama%3ddharma")]
    #[case("http://pod1.example.org/?rama%3ddharma")]
    #[case("http://pod1.example.org/rama%2blakshmana")]
    #[case("http://pod1.example.org/rama%2clakshmana")]
    #[case("http://pod1.example.org/%e0%A4%b0%E0%A4%BE%E0%A4%AE")]
    #[case("http://pod1.example.org/the?%e0%A4%b0%E0%A4%BE%E0%A4%AE")]
    #[case("http://pod1.example.org/kosala%2fayodhya")]
    fn http_uri_un_normal_with_lowercase_pct_encoded_will_be_non_normal(
        #[case] uri_str: &'static str,
    ) {
        assert!(
            !check_if_http_normal(uri_str),
            "normalcy of uri \"{}\" is computed incorrectly ",
            uri_str
        );
    }

    #[rstest]
    // #[case("HTTP://pod1.example.org/rama")] ; scheme is auto normalized by parser
    #[case("http://pod1.EXAMPLE.org/")]
    #[case("http://pod1.example.OEG/rama%2blakshmana")]
    #[case("http://pod1.ExAmple.org/rama%2clakshmana")]
    fn http_uri_un_normal_with_uppercase_origin_char_will_be_non_normal(
        #[case] uri_str: &'static str,
    ) {
        assert!(
            !check_if_http_normal(uri_str),
            "normalcy of uri \"{}\" is computed incorrectly ",
            uri_str
        );
    }

    #[rstest]
    #[case("http://pod1.example.org/.")]
    #[case("http://pod1.example.org/..")]
    #[case("http://pod1.example.org/./path/to/a")]
    #[case("http://pod1.example.org/../a")]
    #[case("http://pod1.example.org/path/to/a/../b/")]
    #[case("http://pod1.example.org/path/to/c/.")]
    #[case("http://pod1.example.org/path/to/c/./")]
    #[case("http://pod1.example.org/path/to/c/..")]
    #[case("http://pod1.example.org/path/to/c/../")]
    fn http_uri_un_normal_with_dot_segments_will_be_non_normal(#[case] uri_str: &'static str) {
        assert!(
            !check_if_http_normal(uri_str),
            "normalcy of uri \"{}\" is computed incorrectly ",
            uri_str
        );
    }

    #[rstest]
    #[case::http_80("http://pod1.example.org:80/path/to/a")]
    #[case::https_443("https://pod1.example.org:443/a?b")]
    #[case::http_localhost_80("http://localhost:80/")]
    fn http_uri_with_explicit_default_port_will_be_non_normal(#[case] uri_str: &'static str) {
        assert!(
            !check_if_http_normal(uri_str),
            "normalcy of uri \"{}\" is computed incorrectly ",
            uri_str
        );
    }

    #[rstest]
    #[case("http://pod1.example.org/path/to//a")]
    #[case("http://pod1.example.org/path/to//")]
    fn http_uri_with_non_trailing_empty_segment_will_be_non_normal(#[case] uri_str: &'static str) {
        assert!(
            !check_if_http_normal(uri_str),
            "normalcy of uri \"{}\" is computed incorrectly ",
            uri_str
        );
    }

    #[rstest]
    #[case("http://pod1.example.org")]
    #[case("http://localhost")]
    fn http_uri_with_empty_path_will_be_non_normal(#[case] uri_str: &'static str) {
        assert!(
            !check_if_http_normal(uri_str),
            "normalcy of uri \"{}\" is computed incorrectly ",
            uri_str
        );
    }

    #[rstest]
    #[case::root("http://pod1.example.org/")]
    #[case::localhost_authority("http://localhost/a/b/c")]
    #[case::ip_authority("http://127.0.0.1/a/b/c")]
    #[case::explicit_port1("http://pod1.example.org:8000/a/b/c")]
    #[case::explicit_port2("http://pod1.example.org:8443/")]
    #[case::explicit_port2("http://pod1.example.org:8443/")]
    #[case::non_relative_period("http://pod1.example.org/a.acl")]
    #[case("http://pod1.example.org/kosala/ayodhya")]
    #[case("http://pod1.example.org/path/to/container/")]
    #[case::un_reserved_unencoded_1("http://pod1.example.org/path/to/container/.acl")]
    #[case::un_reserved_unencoded_1_q("http://pod1.example.org/path/to/container/?.acl")]
    #[case::un_reserved_unencoded_2("http://pod1.example.org/path/to/container/~acl")]
    #[case::un_reserved_unencoded_3("http://pod1.example.org/path/to/container/_acl")]
    #[case::un_reserved_unencoded_4("http://pod1.example.org/path/to/container/-acl")]
    #[case::sub_delim_unencoded_1("http://pod1.example.org/path/to/container/$acl")]
    #[case::sub_delim_unencoded_2("http://pod1.example.org/rama=dharma")]
    #[case::sub_delim_unencoded_3("http://pod1.example.org/rama+lakshmana")]
    #[case::sub_delim_unencoded_4("http://pod1.example.org/rama,lakshmana")]
    #[case::sub_delim_unencoded_5("http://pod1.example.org/rama&lakshmana")]
    #[case::sub_delim_encoded_1("http://pod1.example.org/path/to/container/%24acl")]
    #[case::sub_delim_encoded_2("http://pod1.example.org/rama%3Ddharma")]
    #[case::sub_delim_encoded_3("http://pod1.example.org/rama%2Blakshmana")]
    #[case::sub_delim_encoded_4("http://pod1.example.org/rama%2Clakshmana")]
    #[case::sub_delim_encoded_5("http://pod1.example.org/rama%26lakshmana")]
    #[case::excepted_gen_delim_unencoded_1("http://pod1.example.org/a:b")]
    #[case::expected_gen_delim_unencoded_2("http://pod1.example.org/a@b")]
    #[case::excepted_gen_delim_encoded_1("http://pod1.example.org/a%3Ab")]
    #[case::expected_gen_delim_encoded_2("http://pod1.example.org/a%40b")]
    #[case::gen_delim_encoded_1("http://pod1.example.org/a/b%2Fc")]
    #[case::gen_delim_encoded_2("http://pod1.example.org/a/b%3Fc")]
    #[case::gen_delim_encoded_3("http://pod1.example.org/a/b%23c")]
    #[case::gen_delim_encoded_4("http://pod1.example.org/a/b%5B%5Dc")]
    #[case::non_ascii_pct_encoded("http://pod1.example.org/ramayana/%E0%A4%B0%E0%A4%BE%E0%A4%AE")]
    #[case::non_ascii_pct_encoded(
        "http://pod1.example.org/%E0%B0%85%E0%B0%AF%E0%B1%8B%E0%B0%A7%E0%B1%8D%E0%B0%AF"
    )]
    fn normalized_http_uri_will_be_normal(#[case] uri_str: &'static str) {
        assert!(
            check_if_http_normal(uri_str),
            "normalcy of uri \"{}\" is computed incorrectly ",
            uri_str
        );
    }
}

#[cfg(test)]
mod tests_normalization {
    use claims::assert_ok;
    use rstest::*;

    use super::*;

    fn assert_correct_normalization(source_uri_str: &'static str, expected_uri_str: &'static str) {
        let http_uri = assert_ok!(HttpUri::try_from(source_uri_str));
        let normal_http_uri = HttpUri::http_normalized(&http_uri);
        assert_eq!(normal_http_uri.as_str(), expected_uri_str);
    }

    #[rstest]
    #[case::a(
        "http://pod1.example.org/kosala/%61yodhya",
        "http://pod1.example.org/kosala/ayodhya"
    )]
    #[case::a_q(
        "http://pod1.example.org/kosala?%61yodhya",
        "http://pod1.example.org/kosala?ayodhya"
    )]
    #[case::a_cap(
        "http://pod1.example.org/kosala/%41yodhya",
        "http://pod1.example.org/kosala/Ayodhya"
    )]
    #[case::dig_1(
        "http://pod1.example.org/kosala/raghu%31",
        "http://pod1.example.org/kosala/raghu1"
    )]
    #[case::hyphen(
        "http://pod1.example.org/rama%2Drajya",
        "http://pod1.example.org/rama-rajya"
    )]
    #[case::tilde(
        "http://pod1.example.org/rama%7Esita",
        "http://pod1.example.org/rama~sita"
    )]
    #[case::period(
        "http://pod1.example.org/kosala%2Eayodhya",
        "http://pod1.example.org/kosala.ayodhya"
    )]
    #[case::period_2(
        "http://pod1.example.org/path/to/%2E%2E/c",
        "http://pod1.example.org/path/c"
    )]
    #[case::underscore(
        "http://pod1.example.org/rama%5Flakshmana",
        "http://pod1.example.org/rama_lakshmana"
    )]
    fn unreserved_char_encoding_will_be_normalized_correctly(
        #[case] source_uri_str: &'static str,
        #[case] expected_uri_str: &'static str,
    ) {
        assert_correct_normalization(source_uri_str, expected_uri_str);
    }

    #[rstest]
    #[case(
        "http://pod1.example.org/rama%3ddharma",
        "http://pod1.example.org/rama%3Ddharma"
    )]
    #[case(
        "http://pod1.example.org/?rama%3ddharma",
        "http://pod1.example.org/?rama%3Ddharma"
    )]
    #[case(
        "http://pod1.example.org/rama%2blakshmana",
        "http://pod1.example.org/rama%2Blakshmana"
    )]
    #[case(
        "http://pod1.example.org/rama%2clakshmana",
        "http://pod1.example.org/rama%2Clakshmana"
    )]
    #[case(
        "http://pod1.example.org/%e0%A4%b0%E0%A4%BE%E0%A4%AE",
        "http://pod1.example.org/%E0%A4%B0%E0%A4%BE%E0%A4%AE"
    )]
    #[case(
        "http://pod1.example.org/kosala%2fayodhya",
        "http://pod1.example.org/kosala%2Fayodhya"
    )]
    fn pct_encoded_octets_case_will_be_normalized_correctly(
        #[case] source_uri_str: &'static str,
        #[case] expected_uri_str: &'static str,
    ) {
        assert_correct_normalization(source_uri_str, expected_uri_str);
    }

    #[rstest]
    #[case("http://pod1.example.org/.", "http://pod1.example.org/")]
    #[case("http://pod1.example.org/..", "http://pod1.example.org/")]
    #[case(
        "http://pod1.example.org/./path/to/a",
        "http://pod1.example.org/path/to/a"
    )]
    #[case("http://pod1.example.org/../a", "http://pod1.example.org/a")]
    #[case("http://pod1.example.org/./a:b", "http://pod1.example.org/a:b")]
    #[case(
        "http://pod1.example.org/path/to/a/../b/",
        "http://pod1.example.org/path/to/b/"
    )]
    #[case(
        "http://pod1.example.org/path/to/c/.",
        "http://pod1.example.org/path/to/c/"
    )]
    #[case(
        "http://pod1.example.org/path/to/c/./",
        "http://pod1.example.org/path/to/c/"
    )]
    #[case(
        "http://pod1.example.org/path/to/c/..",
        "http://pod1.example.org/path/to/"
    )]
    #[case(
        "http://pod1.example.org/path/to/c/../",
        "http://pod1.example.org/path/to/"
    )]
    #[case::pct_encoded_dot_segments(
        "http://pod1.example.org/path/to/%2E%2E/c",
        "http://pod1.example.org/path/c"
    )]
    fn dot_segments_will_be_normalized_correctly(
        #[case] source_uri_str: &'static str,
        #[case] expected_uri_str: &'static str,
    ) {
        assert_correct_normalization(source_uri_str, expected_uri_str);
    }

    #[rstest]
    #[case::http("http://pod1.example.org/a/b?q", "http://pod1.example.org/a/b?q")]
    #[case::http_80("http://pod1.example.org:80/a/b?q", "http://pod1.example.org/a/b?q")]
    #[case::http_8000(
        "http://pod1.example.org:8000/a/b?q",
        "http://pod1.example.org:8000/a/b?q"
    )]
    #[case::https("https://pod1.example.org/a/b?q", "https://pod1.example.org/a/b?q")]
    #[case::https_443("https://pod1.example.org:443/a/b?q", "https://pod1.example.org/a/b?q")]
    #[case::https_743(
        "https://pod1.example.org:743/a/b?q",
        "https://pod1.example.org:743/a/b?q"
    )]
    fn port_will_be_normalized_correctly(
        #[case] source_uri_str: &'static str,
        #[case] expected_uri_str: &'static str,
    ) {
        assert_correct_normalization(source_uri_str, expected_uri_str);
    }

    #[rstest]
    #[case(
        "http://pod1.example.org/path/to//a",
        "http://pod1.example.org/path/to/a"
    )]
    #[case(
        "http://pod1.example.org/path/to//",
        "http://pod1.example.org/path/to/"
    )]
    fn non_trailing_empty_segments_will_be_normalized_correctly(
        #[case] source_uri_str: &'static str,
        #[case] expected_uri_str: &'static str,
    ) {
        assert_correct_normalization(source_uri_str, expected_uri_str);
    }

    #[rstest]
    #[case("http://pod1.example.org", "http://pod1.example.org/")]
    #[case("http://localhost", "http://localhost/")]
    fn empty_path_will_be_normalized_correctly(
        #[case] source_uri_str: &'static str,
        #[case] expected_uri_str: &'static str,
    ) {
        assert_correct_normalization(source_uri_str, expected_uri_str);
    }
}