feedparser-rs 0.8.0

High-performance RSS/Atom/JSON Feed parser
Documentation
//! #634: typed fetch errors. No network: every URL here is rejected before any request.
//! Fails to compile until `FeedError::Fetch`, `FetchError`, `HttpStatus` and `SsrfRejection` exist.
//! Assumed paths: `feedparser_rs::http::{FetchError, HttpStatus}` and `feedparser_rs::SsrfRejection`.

#![cfg(feature = "http")]

use feedparser_rs::http::{FetchError, HttpStatus, validate_url};
use feedparser_rs::{FeedError, ParserLimits, SsrfRejection, parse_url, parse_url_with_limits};

#[test]
fn reserved_ip_literal_is_a_blocked_address() {
    let err = parse_url("http://127.0.0.1/feed.xml", None, None, None).err();
    assert!(
        matches!(
            err,
            Some(FeedError::Fetch(FetchError::BlockedAddress {
                reason: SsrfRejection::ReservedIp { .. }
            }))
        ),
        "{err:?}"
    );
}

#[test]
fn blocked_address_is_reported_by_the_limits_entry_point_too() {
    let err = parse_url_with_limits(
        "http://10.0.0.1/",
        None,
        None,
        None,
        ParserLimits::default(),
    )
    .err();
    assert!(
        matches!(
            err,
            Some(FeedError::Fetch(FetchError::BlockedAddress { .. }))
        ),
        "{err:?}"
    );
}

#[test]
fn unsupported_scheme_is_a_blocked_address() {
    let err = parse_url("ftp://example.com/feed", None, None, None).err();
    assert!(
        matches!(
            err,
            Some(FeedError::Fetch(FetchError::BlockedAddress {
                reason: SsrfRejection::UnsupportedScheme { .. }
            }))
        ),
        "{err:?}"
    );
}

#[test]
fn unparseable_url_is_an_invalid_url() {
    let err = parse_url("not a url", None, None, None).err();
    assert!(
        matches!(err, Some(FeedError::Fetch(FetchError::InvalidUrl(_)))),
        "{err:?}"
    );
}

#[test]
fn validate_url_returns_typed_errors() {
    assert!(matches!(
        validate_url("http://127.0.0.1/"),
        Err(FetchError::BlockedAddress { .. })
    ));
    assert!(matches!(
        validate_url("::::"),
        Err(FetchError::InvalidUrl(_))
    ));
}

#[test]
fn http_status_accepts_exactly_400_to_999() {
    assert!(HttpStatus::new(399).is_none());
    assert!(HttpStatus::new(200).is_none());
    assert_eq!(HttpStatus::new(400).map(HttpStatus::as_u16), Some(400));
    assert_eq!(HttpStatus::new(410).map(HttpStatus::as_u16), Some(410));
    assert_eq!(HttpStatus::new(999).map(HttpStatus::as_u16), Some(999));
    assert!(HttpStatus::new(1000).is_none());
}

#[test]
fn status_error_display_names_the_status_and_url() {
    let text = HttpStatus::new(410).map(|status| {
        FeedError::Fetch(FetchError::Status {
            status,
            url: "https://example.com/gone".into(),
        })
        .to_string()
    });
    assert!(
        text.as_deref()
            .is_some_and(|t| t.contains("410") && t.contains("https://example.com/gone")),
        "{text:?}"
    );
}